Config 서버의 설정 값이 변경될 경우, 마이크로서비스들이 변경된 설정 값을 갱신하기 위해서는 마이크로서비스를 재시작하거나, 위 Config Client 예제의 /actuator/refresh를 호출해주어야 한다. 하지만 마이크로서비스의 개수가 많을 경우 각각의 서비스들을 모두 수동으로 재시작하거나 refresh해주기는 쉽지 않다.
Config 서버에서는 마이크로서비스들에게 refresh를 수행하라는 메시지를 전송해야 한다. 이 메시지를 전송해주는 컴포넌트를 메시지브로커라고 부른다.
대표적으로 많이 사용되는 메시지브로커로는 RabbitMQ, Kafka 등이 있다. 나는 Kafka를 사용해본 적이 있으므로, 본 가이드에서는 RabbitMQ를 기준으로 작성할것이다.
RabbitMQ 설치
RabbitMQ를 설치하는 방법은 각 OS 환경에 따라 다양한 방법이 존재한다. 본 가이드에서는 각 환경에 따른 RabbitMQ 설치방법에 대해서는 생략한다.
macOS : 터미널에서 brew install rabbitmq
windows : https://www.rabbitmq.com/install-windows.html
Installing on Windows — RabbitMQ
Installing on Windows This guide covers RabbitMQ installation on Windows. It focuses on the two recommended installation options: The guide also covers a few post-installation topics in the context of Windows: and more. These topics are covered in more det
www.rabbitmq.com
아래부터는 rabbitmq를 설치하고 실행한 상태라는 가정하에 가이드를 작성한다.
RabbitMQ 관리 URL : http://localhost:15672/
접속해서 guest/guest로 로그인이 가능하다.
Config Client 수정
Spring Cloud Bus(RabbitMQ)를 이용하기 위해서는 Client 어플리케이션을 수정해야 한다.
Config 클라이언트 애플리케이션의 build.gradle에 의존성을 추가하자.
implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
application.properties 파일 수정
Config 클라이언트 애플리케이션의 application.properties 파일을 아래와 같이 수정한다.
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
server.port=9265
#엔드포인트. ex) http://localhost:9265/actuator/refresh
management.endpoints.web.exposure.include=*
#서비스 ID(Config 클라이언트가 어떤 서비스를 조회할 것인지 매핑)
spring.application.name=templateEnterprise
#config 서버 url
spring.config.import=configserver:http://localhost:8888
그다음 Config Client 애플리케이션을 실행시키고 http://localhost:15672/에 접속한 후 Exchanges를 클릭해보면
토픽이 추가된것을 볼 수 있다.
Spring Cloud Bus 테스트
Config 서버의 templateEnterprise-dev.properties 파일의 config.profile 값을 변경한다.
그 다음 config 서버를 재실행하고, 실행되어 있는 Config 클라이언트의 busrefresh 엔드포인트를 호출한다(POST로 호출해야 함!)
실행 명령어(리눅스)
curl -X POST http://localhost:9265/actuator/bus-refresh
busrefresh를 클라이언트 한 곳만 호출하면 자동으로 모든 클라이언트 서비스들에게 전파된다.
실행되어 있는 Config 클라이언트들의 config.profile 값을 확인한다.
'DEV > MSA' 카테고리의 다른 글
MSA 개발 가이드(9) - 마이크로 서비스 배포(Docker) (0) | 2023.06.27 |
---|---|
MSA 개발 가이드(7) - Spring Cloud Config Server/Client (0) | 2023.06.26 |
MSA 개발 가이드(6) - Spring Cloud Gateway(Zuul) (0) | 2023.06.19 |
MSA 개발 가이드(5) - Spring Cloud Eureka(Service Registry) (0) | 2023.06.19 |
MSA 개발 가이드(4) - Resilience4j(Circuit Breaker) (0) | 2023.06.19 |