Config 서버는 설정 파일을 중앙에서 저장하고 제공하는 역할을 합니다.
필요한 의존성을 build.gradle 파일에 추가합니다:
dependencies {
implementation 'org.springframework.cloud:spring-cloud-config-server'
implementation 'org.springframework.boot:spring-boot-starter-web'
}
Spring Boot 애플리케이션에서 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 서버에서 설정을 받아오는 역할을 합니다. 필요한 의존성을 build.gradle 파일에 추가합니다:
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-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/
/actuator/refresh 엔드포인트를 사용하여 수동으로 설정을 갱신할 수 있습니다. 이를 통해 Config 서버에서 변경된 설정을 실시간으로 반영할 수 있습니다:
management:
endpoints:
web:
exposure:
include: refresh
@RefreshScope
@RestController
public class ProductController {
@Value("${message}")
private String message;
@GetMapping("/message")
public String getMessage() {
return this.message;
}
}
Config 서버 설정:
config-repo 폴더를 생성하고, 필요한 yml 파일을 설정합니다.product-service-local.yml 파일을 사용하여 로컬 환경에서의 메시지와 포트 정보를 관리합니다.Product 서비스 설정:
build.gradle 파일에 Config 관련 의존성을 추가합니다.ProductController에서 /actuator/refresh 엔드포인트를 통해 실시간으로 설정을 갱신하고 확인합니다.실시간 구성 변경 확인:
yml 파일을 수정한 후, /actuator/refresh 엔드포인트를 호출하여 변경된 메시지를 확인합니다.