Kubernetes 환경에서는 ConfigMap을 활용하여 설정값을 관리했었는데, Spring Cloud Config를 사용해보고 어떤 차이가 있는지 살펴보기로 한다.
[참고] https://spring.io/projects/spring-cloud-config
Spring Cloud Config
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both
spring.io
Config Server 구성
우선 Sample에 있는 아래 repository를 fork하여 그동안 해온 것처럼 spring-boot 3과 Java 17환경으로 맞춰주었다.(https://github.com/mango-park/configserver)
[참고] https://github.com/spring-cloud-samples/configserver
GitHub - spring-cloud-samples/configserver
Contribute to spring-cloud-samples/configserver development by creating an account on GitHub.
github.com
해당 프로젝트의 docker compose를 실행하면 RabbitMQ가 기동되는데, Config Server까지 정상적으로 띄우면 RabbitMQ Console에 아래와 같이 Connection이 추가되고 springCloudBus라는 Queue가 생성되는 것을 볼 수 있다. MQ를 연동한 Config Server의 기능은 추후 알아보기로..
application.yml 파일을 관리하는 별도의 repository를 생성하고 아래와 같이 Config Server(applicayion.yml)의 설정을 변경한다.
spring:
cloud:
config:
server:
git:
uri: https://github.com/mango-park/config-repo
위 repository는 Client에서 읽을 application.yml 파일을 간단히 생성해두었다.
Client 구성
지난 OpenFeign Sample 프로젝트에 Spring Cloud Config를 적용해보기로 한다. 실제 적용은 application.yml 파일과 pom.xml 파일의 간단한 수정으로 가능하다. https://github.com/mango-park/feign-eureka
pom.xml dependency 추가.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
application.yml 파일 수정
기존 설정은 config-repo에 HelloServer-docker.yml 파일에 옮기고, 아래와 같이 Config Server 연결 정보만 남긴다. 매치되는 패턴은 {spring.application.name}-{spring.profiles.active}.yml 이다.
spring:
application:
name: HelloServer
profiles:
active: docker
config:
import: optional:configserver:http://${CONFIGSERVER.URL:localhost:8888}
서비스 확인
Config Server
config-repo에 있는 application.yml 파일 정보는 아래와 같이 호출 및 확인이 가능하다.
Client
HelloServer가 Spring Config Server와 정상 연동되어 기동되면 config-repo 설정 정보(server.port: 7111)로 Tomcat 기동되고, 연동이 되지 않으면 Default Port(8080)으로 기동된다. Spring Boot 기동시 아래와 같이 Log를 확인할 수도 있다.
-정상 로그
2024-04-05 15:40:57 2024-04-05T06:40:57.502Z INFO 1 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://configserver:8888
2024-04-05 15:40:57 2024-04-05T06:40:57.502Z INFO 1 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Located environment: name=HelloServer, profiles=[docker], label=null, version=e29d1457f1e9711a0ae45ac14a206ff5778bf3e1, state=null
-오류 로그
2024-04-05 15:39:37 2024-04-05T06:39:37.940Z INFO 1 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://configserver:8888
2024-04-05 15:39:37 2024-04-05T06:39:37.941Z INFO 1 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Exception on Url - http://configserver:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://configserver:8888/HelloServer/docker": Connection refused. Will be trying the next url if available
정리
Config를 별도 repository에서 관리할 수 있는 것은 큰 장점이지만 개발자에게 불편을 안겨줄 수도 있을 것 같다. 그래도 설정값들을 프로젝트 외부에서 관리하면 불필요한 빌드/배포도 줄일 수 있고, 굳이 환경변수를 많이 선언할 일도 줄어들 것 같다. 다음에는 MQ를 활용한 Spring Cloud Config 기능을 활용해서 설정 변경을 해볼 예정.
'Software > Spring Cloud' 카테고리의 다른 글
Spring Cloud Gateway를 Docker 환경에서 실행하기 (0) | 2024.03.29 |
---|---|
Resilience4J(Circuit Breaker) 활용하기 (0) | 2024.03.28 |
Spring Cloud Eureka&OpenFeign Docker로 구성하기 (0) | 2024.03.27 |