MSA(Microservices Architecture)와 같은 분산 시스템에서는 서버 클라이언트 구성에 필요한 설정 정보(application.yml 등)를 외부 시스템에서 관리할 수 있다.
이를 통해 서비스를 다시 빌드하지 않고 바로 적용할 수 있으며, 배포 파이프라인을 통해 DEV, UAT, PROD 환경에 맞는 구성 정보를 사용할 수 있다.
1️⃣ 관리해야 할 YML 파일 생성
설정 파일을 Git Repository로 관리하는 방법이다. 예를 들어, ecommerce.yml 파일을 생성하여 아래와 같이 관리한다.
token:
expiration_time: 86400000
secret: usertokenforjwtsforlongnumberwherecanitbegothrough
gateway:
ip: 127.0.0.1
2️⃣ Spring Cloud Config 서비스 생성
Spring Cloud Config Server를 설정하여, 외부 저장소에서 설정 파일을 가져올 수 있도록 한다.
1) ConfigServiceApplication 클래스
@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
2) application.yml 설정
1) Local Git으로 관리하는 경우
server:
port: 8888
spring:
application:
name: config-service
cloud:
config:
server:
git:
uri: file://C:\devProjects\private\MSA\git-local-repo // Local Git Repository의 주소
uri: https://github.com/주소 // 원격 Git Repository 주소
2) Native File로 관리하는 경우
spring:
application:
name: config-service
profiles:
active: native
cloud:
config:
server:
native:
search-locations: file:///C:/devProjects/private/MSA/native-file-repo
native 방식으로 설정 파일을 로컬 디렉토리에서 가져올 수 있다.
✅ 예를들어
- http://127.0.0.1:8888/ecommerce/native -> ecommerce.yml 파일을 가져온다.
- http://127.0.0.1:8888/user-service/native -> user-service.yml 파일을 가져온다.
즉, http://127.0.0.1:8888/ecommerce/default 로 주소 이동하게 되면
{
"name": "ecommerce",
"profiles": [
"default"
],
"label": null,
"version": "37c682c01af8cc76fc8eeaffded7daf3c1179911",
"state": "",
"propertySources": [
{
"name": "file://C:\\devProjects\\private\\MSA\\git-local-repo/ecommerce.yml",
"source": {
"token.expiration_time": 86400000,
"token.secret": "usertokenforjwtsforlongnumberwherecanitbegothrough",
"gateway.ip": "127.0.0.1"
}
}
]
}
이와 같은 ecommerce에 대한 json형태의 파일 정보를 확인할 수 있다.
3️⃣ Spring Cloud Config를 사용하는 마이크로 서비스 설정
1) Spring Cloud Config 의존성 추가
spring-cloud-starter-config 의존성을 추가하여 Spring Cloud Config를 사용할 수 있도록 한다.
2) bootstrap.yml 파일 설정
spring:
cloud:
config:
uri: http://127.0.0.1:8888
name: ecommerce # 가져올 YML 파일 이름
uri에 Spring Cloud Config Server의 주소를 설정하고, name에 사용할 설정 파일 이름을 입력한다.
3) 서버 구동 시 확인되는 로그
서버가 구동되면 다음과 같은 로그가 출력된다.
2025-01-14T10:53:16.556+09:00 INFO 25004 --- [user-service] [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://127.0.0.1:8888
2025-01-14T10:53:16.716+09:00 INFO 25004 --- [user-service] [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=ecommerce, profiles=[default], label=null, version=37c682c01af8cc76fc8eeaffded7daf3c1179911, state=
2025-01-14T10:53:16.717+09:00 INFO 25004 --- [user-service] [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-file://C:\devProjects\private\MSA\git-local-repo/ecommerce.yml'}]
Config Server에서 설정 파일을 성공적으로 가져오는 과정을 확인할 수 있다.
4️⃣ 기존의 property 접근법과 Spring Cloud Config 적용
기존의 Spring 방식처럼 Environment나 @Value를 통해 설정 값을 가져올 수 있다. 예를 들어, ecommerce.yml에 설정된 값은 다음과 같이 가져올 수 있다.
@Value("${token.expiration_time}")
private Long tokenExpirationTime;
⚠️ 단점과 해결책
Spring Cloud Config를 사용하는 마이크로 서비스는 재기동해야만 config 정보를 업데이트할 수 있다는 단점이 있다.
이를 해결하기 위해 Spring boot Actuator, Spring Cloud Bus나 Spring Cloud Config Client와 같은 기술을 사용하여 동적으로 설정 정보를 갱신할 수 있다.
다음 글에서는 Spring Boot Actuator를 사용하여 수동 재기동 없이 config 정보를 자동으로 업데이트하는 방법을 알아보도록 하겠다.
'백엔드 Backend > 서버 Server' 카테고리의 다른 글
Spring Cloud Bus (0) | 2025.01.15 |
---|---|
Spring Cloud Config 자동으로 반영하기 (feat. Acturator) (0) | 2025.01.14 |
Eureka + Spring Cloud Gateway 구현하기 (0) | 2025.01.07 |
API 게이트웨이 서비스 - (2) Spring Cloud Gateway (0) | 2025.01.06 |
API 게이트웨이 서비스 - (1) Netfilx Zuul (1) | 2025.01.06 |
MSA(Microservices Architecture)와 같은 분산 시스템에서는 서버 클라이언트 구성에 필요한 설정 정보(application.yml 등)를 외부 시스템에서 관리할 수 있다.
이를 통해 서비스를 다시 빌드하지 않고 바로 적용할 수 있으며, 배포 파이프라인을 통해 DEV, UAT, PROD 환경에 맞는 구성 정보를 사용할 수 있다.
1️⃣ 관리해야 할 YML 파일 생성
설정 파일을 Git Repository로 관리하는 방법이다. 예를 들어, ecommerce.yml 파일을 생성하여 아래와 같이 관리한다.
token:
expiration_time: 86400000
secret: usertokenforjwtsforlongnumberwherecanitbegothrough
gateway:
ip: 127.0.0.1
2️⃣ Spring Cloud Config 서비스 생성
Spring Cloud Config Server를 설정하여, 외부 저장소에서 설정 파일을 가져올 수 있도록 한다.
1) ConfigServiceApplication 클래스
@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
2) application.yml 설정
1) Local Git으로 관리하는 경우
server:
port: 8888
spring:
application:
name: config-service
cloud:
config:
server:
git:
uri: file://C:\devProjects\private\MSA\git-local-repo // Local Git Repository의 주소
uri: https://github.com/주소 // 원격 Git Repository 주소
2) Native File로 관리하는 경우
spring:
application:
name: config-service
profiles:
active: native
cloud:
config:
server:
native:
search-locations: file:///C:/devProjects/private/MSA/native-file-repo
native 방식으로 설정 파일을 로컬 디렉토리에서 가져올 수 있다.
✅ 예를들어
- http://127.0.0.1:8888/ecommerce/native -> ecommerce.yml 파일을 가져온다.
- http://127.0.0.1:8888/user-service/native -> user-service.yml 파일을 가져온다.
즉, http://127.0.0.1:8888/ecommerce/default 로 주소 이동하게 되면
{
"name": "ecommerce",
"profiles": [
"default"
],
"label": null,
"version": "37c682c01af8cc76fc8eeaffded7daf3c1179911",
"state": "",
"propertySources": [
{
"name": "file://C:\\devProjects\\private\\MSA\\git-local-repo/ecommerce.yml",
"source": {
"token.expiration_time": 86400000,
"token.secret": "usertokenforjwtsforlongnumberwherecanitbegothrough",
"gateway.ip": "127.0.0.1"
}
}
]
}
이와 같은 ecommerce에 대한 json형태의 파일 정보를 확인할 수 있다.
3️⃣ Spring Cloud Config를 사용하는 마이크로 서비스 설정
1) Spring Cloud Config 의존성 추가
spring-cloud-starter-config 의존성을 추가하여 Spring Cloud Config를 사용할 수 있도록 한다.
2) bootstrap.yml 파일 설정
spring:
cloud:
config:
uri: http://127.0.0.1:8888
name: ecommerce # 가져올 YML 파일 이름
uri에 Spring Cloud Config Server의 주소를 설정하고, name에 사용할 설정 파일 이름을 입력한다.
3) 서버 구동 시 확인되는 로그
서버가 구동되면 다음과 같은 로그가 출력된다.
2025-01-14T10:53:16.556+09:00 INFO 25004 --- [user-service] [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://127.0.0.1:8888
2025-01-14T10:53:16.716+09:00 INFO 25004 --- [user-service] [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=ecommerce, profiles=[default], label=null, version=37c682c01af8cc76fc8eeaffded7daf3c1179911, state=
2025-01-14T10:53:16.717+09:00 INFO 25004 --- [user-service] [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-file://C:\devProjects\private\MSA\git-local-repo/ecommerce.yml'}]
Config Server에서 설정 파일을 성공적으로 가져오는 과정을 확인할 수 있다.
4️⃣ 기존의 property 접근법과 Spring Cloud Config 적용
기존의 Spring 방식처럼 Environment나 @Value를 통해 설정 값을 가져올 수 있다. 예를 들어, ecommerce.yml에 설정된 값은 다음과 같이 가져올 수 있다.
@Value("${token.expiration_time}")
private Long tokenExpirationTime;
⚠️ 단점과 해결책
Spring Cloud Config를 사용하는 마이크로 서비스는 재기동해야만 config 정보를 업데이트할 수 있다는 단점이 있다.
이를 해결하기 위해 Spring boot Actuator, Spring Cloud Bus나 Spring Cloud Config Client와 같은 기술을 사용하여 동적으로 설정 정보를 갱신할 수 있다.
다음 글에서는 Spring Boot Actuator를 사용하여 수동 재기동 없이 config 정보를 자동으로 업데이트하는 방법을 알아보도록 하겠다.
'백엔드 Backend > 서버 Server' 카테고리의 다른 글
Spring Cloud Bus (0) | 2025.01.15 |
---|---|
Spring Cloud Config 자동으로 반영하기 (feat. Acturator) (0) | 2025.01.14 |
Eureka + Spring Cloud Gateway 구현하기 (0) | 2025.01.07 |
API 게이트웨이 서비스 - (2) Spring Cloud Gateway (0) | 2025.01.06 |
API 게이트웨이 서비스 - (1) Netfilx Zuul (1) | 2025.01.06 |