취미겸생업

[Spring boot] Spring Cloud Config 본문

IT/Spring Boot

[Spring boot] Spring Cloud Config

nohumb 2026. 7. 3. 17:12
SMALL

Spring Cloud Config

Spring Cloud Config란

  • 분산 시스템 환경에서 중앙 집중식 구성 관리를 제공하는 프레임워크로, 설정을 중앙에서 관리하고, 변경 사항을 실시간으로 반영할 수 있음
  • Git, 파일 시스템, JDBC 등 다양한 저장소를 지원

주요 기능

  • 중앙 집중식 구성 관리: 모든 마이크로서비스의 설정을 중앙에서 관리
  • 환경별 구성: 개발, 테스트, 운영 등 환경별로 구성을 분리하여 관리할 수 있음
  • 실시간 구성 변경: 설정 변경 시 애플리케이션을 재시작하지 않고도 실시간으로 반영할 수 있음

Config 서버 설정

  • build.gradle

      dependencies {
          implementation 'org.springframework.cloud:spring-cloud-config-server'
          implementation 'org.springframework.boot:spring-boot-starter-web'
          implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
      }
  • ConfigServerConfig.java

      @Configuration
      @EnableConfigServer
      public class ConfigServerConfig { }
  • application.yml (Git 저장소)

      server:
        port: 18080
    
      spring:
        application:
          name: config-server
        cloud:
          config:
            server:
              git:
                uri: https://github.com/my-config-repo/config-repo # Git을 활용한 Config 저장소
                clone-on-start: true
    
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:19090/eureka/
    • 해당 Repository에 yml 파일을 넣어두면 설정 파일 저장소로 활용 가능
  • application.yml (로컬 저장소)

      server:
        port: 18080
    
      spring:
        profiles:
          active: native # 로컬 저장소를 사용한다고 명시
        application:
          name: config-server
        cloud:
          config:
            server:
              native:
                search-locations: classpath:/config-repo  # 리소스 폴더의 디렉토리 경로
    
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:19090/eureka/
    • 프로젝트의 src/main/resources/config-repo 경로를 로컬 yml 파일 저장소로 활용 가능

Config 클라이언트 설정

  • build.gradle

      dependencies {
          implementation 'org.springframework.cloud:spring-cloud-starter-config'
          implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
      }
  • application.yml

      spring:
        application:
          name: my-config-client
        profiles:
          active: local
        cloud:
          config:
            discovery:
              enabled: true
              service-id: config-server # Eureka 연동하여 서비스 디스커버리를 통해 Config Server 찾음
        config: # spring 바로 하위(2칸 들여쓰기)로 위치 수정
          import: "configserver:" # 이름과 프로파일을 참조해 my-config-client-local.yml 등을 로드
          # import: "configserver:http://my-config-server:8888" # Eureka 연동 없이 서버 주소를 직접 하드코딩할 때 사용
    
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:19090/eureka

실시간 Config Refresh

실시간으로 설정 파일을 반영하는 방법에는 여러 가지가 있으며, 상황에 따라 적절히 선택해 사용할 수 있다.

  • build.gradle (Config 파일을 refresh 하려면 Actuator 의존성이 필수)

      dependencies {
          implementation 'final org.springframework.boot:spring-boot-starter-actuator'
      }
  • 적용 방법

    • 수동으로 엔드포인트 호출 : “/acutator/refresh” 호출
    • Spring Cloud Bus : 메시징 시스템을 통해 실시간으로 설정 변경 사항을 전파
    • Spring Boot DevTools : 주로 개발 환경에서 유용하게 사용됨
    • Git 저장소 : WebHook을 통해 config 파일 업데이트 시 자동으로 refresh

Actuator를 통한 수동 Config Refresh

Actuator의 /actuator/refresh 엔드포인트에 POST를 요청하여 설정 파일을 Refresh 할 수 있다.

  • application.yml

      management:
        endpoints:
          web:
            exposure:
              include: refresh, health # refresh 엔드포인트를 웹에 노출 (쉼표로 추가 가능)
  • MyProvider.java (설정 값을 실시간으로 주입 받아야 하는 Bean)

      @Component
      @RefreshScope // /actuator/refresh 호출 시 이 클래스의 빈을 새로 갱신함
      public class MyProvider {
          @Value("${app.some_value}")
          private String someValue;
    
          public String getSomeValue() {
              return this.someValue;
          }
      }

Spring Cloud Bus를 통한 Config Refresh

이 방법은 Message Broker(RabbitMQ, Kafka 등)을 이용한 방식으로, Event Driven 방식을 소개한 후, 새로 작성할 예정

https://nohumbit.tistory.com/42

 

[Architecture] Event-Driven Archiecture 개요

이벤트 기반 아키텍처(EDA, Event-Driven Architecture)서론본 문서는 이벤트 기반 비동기 아키텍처에 대한 개념을 쉽게 이해할 수 있도록 서술했으며, 개인적인 견해가 포함되어 있습니다. 또한 구체적

nohumbit.tistory.com

 

LIST