Spring Cloud Config 서버/클라이언트 구성하기
application.yaml 로 설정을 하게 되면,
설정이 변경될 때마다 서버를 재시작해야 하는 문제가 발생한다.
이를 해결하기 위해 Spring Boot 2.4 부터는 Config Server 가 생겼다.
Config Server 에 설정을 해두고,
서버에서 Config Server 에 설정을 갱신하도록 하면 서버 재시작 없이
설정이 갱신된다.
Config Server 구성하기
아래 의존성만 있으면 된다.
dependencies {
implementation ('org.springframework.cloud:spring-cloud-config-server')
}
@EnableConfigServer
만 추가하는 것으로 설정이 끝난다.
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
설정파일 관리하기
설정파일은 별도의 git repo 에 올려놓고 가져올 수도 있다.
https://github.com/XXXXXX/spring-cloud-config-example 라는 private repo 가 있다고 가정하자.
위 repo 에 아래 두개의 파일을 올리자.
config-example-default.yml
example:
phase: "default"
config-example-release.yml
example:
phase: "release"
Config Server 의 application.yml 을 열고,
아래 내용을 추가해 준다.
spring:
cloud:
config:
server:
git:
uri: git@github.com:XXXXXX/spring-cloud-config-example.git
인증 방식은 Personal Access Token 으로 해준다.(참조)
접속확인
http://localhost:8080/config-example/default 에 접속해 본다.
http://XXX.XXX.XXX.XXX:8888/default,elastic,token,rabbitmq,redis,eureka/local
Config Client 구성하기
application.yml
spring:
application:
name: config-example
Config Client 는 디폴트로 Config Server 를 http://localhost:8888 에서 찾는다.
호스트나 포트를 변경했다면 아래 파일을 생성하고 내용을 입력해 준다.
bootstrap.yml
spring:
cloud:
config:
uri: http://localhost:8080