2025. 3. 4. 14:09ㆍSpring Microservice
🚀 Spring Cloud Config Server 부트스트랩 설정 및 파일 시스템 저장소 구성
🎯 Spring Cloud Config Server의 부트스트랩 클래스
Spring Cloud Config Server는 Spring Boot 기반 애플리케이션이며, 서비스 실행 시 부트스트랩 클래스(bootstrap class) 를 통해 초기화됩니다.
✅ 부트스트랩 클래스의 역할
- main() 메서드를 포함하여 Spring Boot 애플리케이션을 실행
- @SpringBootApplication 애너테이션으로 Spring Boot 자동 구성 활성화
- @EnableConfigServer 애너테이션을 사용하여 Config Server 기능 활성화
📌 📜 Java 코드 예제 (ConfigurationServerApplication.java)
package com.optimagrowth.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication // (1) Spring Boot 애플리케이션으로 설정
@EnableConfigServer // (2) Config Server 활성화
public class ConfigurationServerApplication {
public static void main(String[] args) { // (3) Spring Boot 애플리케이션 실행
SpringApplication.run(ConfigurationServerApplication.class, args);
}
}
✅ (1) @SpringBootApplication
- Spring Boot 애플리케이션으로 설정
- @Configuration, @EnableAutoConfiguration, @ComponentScan 포함
✅ (2) @EnableConfigServer
- Spring Cloud Config Server 활성화
- 이 애너테이션이 있어야 Config Server로 동작 가능
✅ (3) main() 메서드
- SpringApplication.run()을 호출하여 Spring Boot 애플리케이션 실행
🔥 Spring Cloud Config Server와 파일 시스템 연동
Spring Cloud Config Server는 기본적으로 Git 저장소를 사용하여 설정을 관리하지만,
로컬 파일 시스템을 활용하여 설정 파일을 저장하고 제공하는 방식도 가능합니다.
📌 Spring Cloud Config Server에서 파일 시스템을 활용하려면 bootstrap.yml에 다음 설정 추가
📌 1️⃣ bootstrap.yml 설정 (파일 시스템 사용)
spring:
application:
name: config-server
profiles:
active: native # (1) 파일 시스템을 사용하기 위해 "native" 프로필 활성화
cloud:
config:
server:
native:
search-locations: file:///path/to/config-directory # (2) 설정 파일이 저장된 디렉터리 경로
server:
port: 8071 # (3) Config Server의 실행 포트
✅ (1) native 프로필 설정
- native 프로필은 Spring Cloud Config Server가 파일 시스템 또는 클래스패스에서 설정 파일을 로드하도록 설정
✅ (2) search-locations 설정
- file:///path/to/config-directory 경로에 저장된 설정 파일을 로드
- 설정 파일이 저장된 폴더를 Config Server가 자동으로 검색
✅ (3) server.port=8071
- 기본 포트(8888) 대신 8071 포트에서 실행하도록 설정
📌 예제
spring:
cloud:
config:
server:
native:
search-locations: file:///Users/username/config-data # macOS/Linux
# search-locations: file:///C:/Users/username/config-data # Windows
⚠️ Windows 환경에서는 경로를 "file:///C:/Users/username/config-data" 형태로 설정해야 함
🔧 Spring Cloud Config Server의 프로필 시스템
📌 2️⃣ native 프로필이란?
Spring Cloud Config Server는 여러 프로필을 지원하며, native 프로필은 파일 시스템 또는 클래스패스에서 설정을 로드할 때 사용됩니다.
✅ Git 저장소를 사용하지 않고, 로컬 파일 시스템에서 설정을 가져오는 방법
✅ native 프로필을 활성화하면 파일 시스템 또는 클래스패스에서 설정을 검색
📌 클래스패스에서 설정을 가져오려면 다음과 같이 설정
spring:
cloud:
config:
server:
native:
search-locations: classpath:/config
✅ 이 설정을 적용하면 src/main/resources/config 폴더에서 설정 파일을 로드
🛠 클라이언트 애플리케이션에서 설정 가져오기
Spring Cloud Config Server가 설정 파일을 제공하면, 클라이언트 애플리케이션은 이를 가져와야 합니다.
1️⃣ 클라이언트 애플리케이션의 bootstrap.yml 설정
spring:
application:
name: licensing-service # 서비스명
cloud:
config:
uri: http://localhost:8071 # Config Server의 주소
2️⃣ 클라이언트 애플리케이션에서 설정 요청 테스트
curl http://localhost:8071/licensing-service/default
✅ licensing-service 서비스의 기본 설정(default 프로필) 가져오기
🔐 보안이 중요한 설정 보호
✅ Spring Cloud Config Server는 민감한 설정 정보(예: DB 비밀번호, API 키 등) 를 보호할 수 있어야 합니다.
✅ 이를 위해 HashiCorp Vault, AWS Secrets Manager, Azure Key Vault 같은 보안 저장소와 연동 가능
✅ spring.cloud.config.server.vault 설정을 통해 보안 강화를 위한 암호화 설정 가능
📌 Vault 연동 예제
spring:
cloud:
config:
server:
vault:
uri: https://vault.example.com
authentication: TOKEN
token: my-secret-token
✅ 설정 파일이 아닌 보안 저장소에서 민감한 정보를 불러오는 방식 적용 가능
🏆 Spring Cloud Config Server 도입 효과
문제 | Config Server 사용 시 해결 |
설정 변경 시 코드 배포 필요 | 설정 변경 후 애플리케이션 재배포 없이 즉시 반영 |
설정 관리가 복잡함 | 중앙 집중식 관리로 설정 일관성 유지 |
보안 문제 발생 가능 | 민감한 데이터 암호화 및 보안 저장소 활용 가능 |
설정 변경 후 서비스 재시작 필요 | 클라이언트가 자동으로 변경 사항 감지하여 반영 |
🎯 Summary
✅ Spring Cloud Config Server는 설정을 중앙에서 관리하며, 파일 시스템 기반으로도 운영 가능
✅ @EnableConfigServer 애너테이션을 사용하여 Config Server 활성화
✅ native 프로필을 사용하면 Git 없이 파일 시스템 또는 클래스패스에서 설정을 로드 가능
✅ 클라이언트 애플리케이션은 Config Server에서 설정을 자동으로 가져옴
✅ 보안이 중요한 설정 정보는 Vault 등 보안 저장소를 활용하여 보호 가능
💡 이제 Spring Cloud Config Server를 파일 시스템과 연동하여 설정을 중앙 집중식으로 관리해보세요! 🚀
'Spring Microservice' 카테고리의 다른 글
Spring Cloud Config 서비스와 라이선싱 서비스 연동 설정 (0) | 2025.03.07 |
---|---|
Setting up the configuration files for a service (0) | 2025.03.04 |
Integrating Spring Cloud Config with a Spring Boot client (0) | 2025.03.04 |
Building our Spring Cloud Configuration Server (0) | 2025.03.04 |
On managing configuration (0) | 2025.03.04 |