Spring

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

Griotold 2025. 1. 21. 16:29

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