
Config 서버는 설정 파일을 저장하고 제공하는 역할을 한다.
Spring Boot 애플리케이션에서 @EnableConfigServer 애너테이션을 붙여 Config 서버를 설정한다.
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
application.yml 파일에서 Config 서버 설정을 정의한다.
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/my-config-repo/config-repo
clone-on-start: true
Config 클라이언트는 Config 서버에서 설정을 받아오는 역할을 한다.
클라이언트의 application.yml 파일에서 Config 서버의 설정을 정의한다.
spring:
application:
name: my-config-client
cloud:
config:
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:19090/eureka/
여기서 service-id는 config 서버의 id를 입력한다.
Config 서버는 환경별로 다른 설정 파일을 제공할 수 있다.
application-dev.yml, application-prod.yml 같은 파일을 Git 저장소에 저장하여 환경별 설정을 관리할 수 있다.
Spring Boot 애플리케이션에서는 프로필을 사용하여 환경을 구분할 수 있다.
spring:
profiles:
active: dev
실시간 구성 변경을 반영하는 방법은 여러 가지가 있다.
Spring Cloud Bus를 사용하지 않는 경우, 클라이언트 애플리케이션에서 수동으로 설정을 갱신할 수 있다. 이를 위해 Spring Actuator의 /actuator/refresh 엔드포인트를 사용할 수 있다.
/actuator/refresh 엔드포인트를 POST 요청으로 호출하여 변경된 설정 반영이 방법은 간단하지만, 각 클라이언트 애플리케이션에서 수동으로 엔드포인트를 호출해야 합니다.
Spring Boot DevTools를 사용하면 개발 환경에서 파일 변경을 자동으로 감지하고 애플리케이션을 재시작할 수 있습니다. 이는 classpath 내의 파일 변경도 포함됩니다.