Spring Cloud Config 개요
분산 시스템에서 외부 구성을 관리하기 위한 솔루션으로, Config Server와 Config Client로 구성됩니다. Config Server는 Git 저장소에서 설정을 로드하여 마이크로서비스에 제공하며, 환경별 구성 분리와 중앙 집중식 관리를 지원합니다. @EnableConfigServer 어노테이션으로 Spring Boot 애플리케이션에 통합 가능하며, 암호화 기능을 통해 보안성을 확보합니다.
로컬 저장소 구성 설정
Config Server 설정
- Maven 종속성 추가:
- 메인 클래스에 @EnableConfigServer 추가
- application.yml 구성:
server: port: 8001 spring: application: name: config-service profiles: active: native cloud: config: server: native: search-locations: classpath:/configurations/ - resource/configurations/ 디렉터리에 client-app-dev.yml 생성
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>3.0.3</version>
</dependency>
Config Client 설정
- Client 종속성 추가:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>3.0.3</version> </dependency> - bootstrap.yml 설정:
spring: application: name: client-app cloud: config: uri: http://localhost:8001 profile: dev - 컨트롤러 구현:
@RestController public class ConfigController { @Value("${app.message}") String message; @GetMapping("/info") public String getConfig() { return message; } }
Git 저장소 연동
- Git 저장소에 config-repo/client-app-dev.yml 파일 생성
- Config Server 설정 변경:
spring: cloud: config: server: git: uri: https://gitee.com/your-repo username: your-username password: your-password search-paths: config-repo
고가용성 구성
Eureka 서버 설정
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryApp {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApp.class, args);
}
}
Config Server 클러스터링
- 여러 Config Server 인스턴스 생성 (다른 포트 사용)
- Client bootstrap.yml 설정:
spring: cloud: config: discovery: enabled: true service-id: config-service
구성 파일 차이점
bootstrap.yml: 애플리케이션 시작 초기에 로드되며, Config Server 위치 등 기본 설정 정의
application.yml: 애플리케이션 구성을 위한 매개변수 정의, bootstrap 설정 오버라이드 불가