운영 환경에서 Spring Cloud Config Server를 의존하지 않으려면

2025. 1. 21. 16:29· Spring
목차
  1. 1. 문제 상황
  2. application-dev.yml
  3. application-prod.yml
  4. 2. 원인 분석
  5. 3. 해결
  6. 4. 결론
  7. References

1. 문제 상황

개발 환경에서는 Config Server 에서 데이터 소스를 가져다가 사용하고

운영 환경에서는 github secrets, variales 에서 환경변수를 주입해서 EC2에 배포를 한다.

 

개발 환경의 yml을 보면,

application-dev.yml

spring:
  application:
    name: auth-service
  config:
    import: "configserver:"
  cloud:
    config:
      discovery:
        service-id: config-service
        enabled: true

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:19000/eureka/

위 처럼 config-service 에서 auth-service에서 필요한 설정들 가져 오는 것이다.

 

config-service 에는 auth-service.yml 이라는 auth-service의 설정파일을 가지고 있다.

auth-service.yml

server:
  port: 19010

spring:
  application:
    name: auth-service
  config:
    import: classpath:config-repo/application-key.yml
  datasource:
    driver-class-name: org.postgresql.Driver
    url: ${DB_URL}/user_db
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      format-sql: true
      use_sql_comments: true
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
  data:
    redis:
      host: ${SHARED_REDIS_HOSTNAME}
      port: ${SHARED_REDIS_PORT}
  jackson:
    property-naming-strategy: SNAKE_CASE

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:19000/eureka/

service:
  jwt:
    access-expiration: 360000
    secret-key: ${SECRET_KEY}

management:
  endpoints:
    web:
      exposure:
        include: "*"

 

application-prod.yml

server:
  port: ${AUTH_SERVER_PORT:19010}

spring:
  application:
    name: auth-service
  config:
    activate:
      on-profile: prod
  datasource:
    driver-class-name: org.postgresql.Driver
    url: ${DB_URL}/user_db
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: false
    properties:
      format-sql: false
      use_sql_comments: false
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
  data:
    redis:
      host: ${REDIS_HOST}
      port: ${SHARED_REDIS_PORT:6379}
  jackson:
    property-naming-strategy: SNAKE_CASE

eureka:
  client:
    serviceUrl:
      defaultZone: http://${EUREKA_HOSTNAME}:19000/eureka/

service:
  jwt:
    access-expiration: 360000
    secret-key: ${AUTH_SECRET_KEY}

management:
  endpoints:
    web:
      exposure:
        include: "*"

운영 환경에서 사용할 설정 파일은 config-service에서 가져오지 않고, 직접 설정파일을 들고 있는 형식이다.

 

이러한 상황에서 배포를 진행했더니 문제가 발생하였다.

배포는 github actions 를 사용하여 CI/CD 파이프라인에 의해 EC2에 배포되는 형식이다.

github actions 의 로그를 보면,

 

configserver 에서 데이터 소스를 가져올 수 없다면서 서버 실행이 실패하는 모습을 확인할 수 있었다.

 

2. 원인 분석

Spring Boot 2.4 버전 부터 Spring Cloud Config Server를 의존하고 있는 프로젝트는 

Config Server와 연결할 수 없는 경우, 서버 구동이 되지 않는다고 한다.

Config Server와 연결하지 않더라도 서버를 구동 시키려면 optional 설정을 추가해야 한다.

spring:
  config:
    import: "optional:configserver:"

 

3. 해결

application-prod.yml 에 적용한 모습

server:
  port: ${AUTH_SERVER_PORT:19010}

spring:
  application:
    name: auth-service
  config:
    activate:
      on-profile: prod
    import: "optional:configserver:"
  datasource:
    driver-class-name: org.postgresql.Driver
    url: ${DB_URL}/user_db
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: false
    properties:
      format-sql: false
      use_sql_comments: false
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
  data:
    redis:
      host: ${REDIS_HOST}
      port: ${SHARED_REDIS_PORT:6379}
  jackson:
    property-naming-strategy: SNAKE_CASE

eureka:
  client:
    serviceUrl:
      defaultZone: http://${EUREKA_HOSTNAME}:19000/eureka/

service:
  jwt:
    access-expiration: 360000
    secret-key: ${AUTH_SECRET_KEY}

management:
  endpoints:
    web:
      exposure:
        include: "*"

management:
  zipkin:
    tracing:
      endpoint: ${ZIPKIN_URL}
  tracing:
    sampling:
      probability: 1.0

 

4. 결론

 

application-prod.yml 을 수정 후 github actions 에 의해 CI/CD 파이프라인을 태웠고,

정상적으로 배포가 진행된 것을 확인할 수 있었다.

 

References

https://madplay.github.io/post/changes-in-spring-cloud-config-from-spring-boot-2-4

 

'Spring' 카테고리의 다른 글

Spring AOP를 활용해서 권한 체크 하기  (0) 2025.03.06
LocalDateTime Jackson 직렬화 오류 해결하기  (1) 2025.01.22
Spring Boot 에서 Snake Case 형식으로 JSON 응답하기  (0) 2025.01.08
  1. 1. 문제 상황
  2. application-dev.yml
  3. application-prod.yml
  4. 2. 원인 분석
  5. 3. 해결
  6. 4. 결론
  7. References
'Spring' 카테고리의 다른 글
  • Spring AOP를 활용해서 권한 체크 하기
  • LocalDateTime Jackson 직렬화 오류 해결하기
  • Spring Boot 에서 Snake Case 형식으로 JSON 응답하기
Griotold
Griotold
Griotold
Griotold's Olive Oil
Griotold
전체
오늘
어제
  • 분류 전체보기 (89)
    • 테스트 (4)
      • Spock (1)
      • Junit (3)
    • 디자인 패턴 (1)
    • 깃 (2)
    • 리팩토링 (4)
    • 항해플러스 백엔드 5기 (3)
    • 인프런 워밍업 클럽 스터디 2기 백엔드 (4)
    • 코딩테스트 (10)
    • 자바 심화 2기 (7)
    • 백엔드 면접 질문 (19)
    • 인프라 (17)
      • docker (5)
      • CI, CD (5)
      • Monitoring (6)
      • AWS (1)
    • 데이터베이스 (1)
      • Redis (1)
    • 메시지큐 (3)
      • rabbitMQ (0)
      • kafka (3)
    • MSA (7)
    • JPA (1)
    • Spring (5)
      • Spring AI (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 글쓰기

공지사항

인기 글

태그

  • actuator
  • grafana
  • 항해99
  • backend
  • DATABASE
  • 99클럽
  • prometheus
  • java
  • 읽기좋은코드
  • MSA
  • 이분탐색
  • Spring
  • 프로그래머스
  • junit5
  • 오블완
  • docker
  • 티스토리챌린지
  • micrometer
  • github
  • githubactions
  • JPA
  • CICD
  • 백엔드
  • 코딩테스트준비
  • 리팩터링
  • 백준
  • 자바
  • 개발자취업
  • 배포
  • TIL

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
Griotold
운영 환경에서 Spring Cloud Config Server를 의존하지 않으려면
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.