0. Spring Cloud Bus
지난 포스팅에선 Actuator를 이용해서 refresh를 통해 configuration 정보들을 가져오는 방법을 살펴보았다.
이 방법을 사용하게 된다면, 설정 정보가 바뀔때 마다 refresh를 해줘야 하는 단점이 있다.
자세한 내용은 공식 문서를 살펴보자.
https://spring.io/projects/spring-cloud-bus/
Spring Cloud Bus
Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can then be used to broadcast state changes (e.g. configuration changes) or other management instructions. AMQP and Kafka broker implementations are included with
spring.io
Spring Cloud Bus의 구조는 위와 같다.
각 CLIENT들은 Microservice들이고, Spring Cloud Config Server와 연결되어 있는 Spring Cloud Bus가 변경된 정보들이 있으면 메세지 브로커에 push하게 되고, microservice들이 subscribe한 메세지를 통해 다시 설정 정보를 가져오는 refresh를 수행하게 된다.
이전 actuator에서 사용했던 refresh와 조금 다른 점은, 어느 client 에게 busrefresh POST호출을 하게 되면, Spring Cloud Bus가 해당하는 설정 정보들을 필요한 서비스들 모두 refresh 해주게 된다.
이전처럼 모든 서비스들을 refresh 일일이 해줄 필요가 없는 것이다.
1. RabbitMQ를 이용한 Cloud Bus
여기서는 RabbitMQ, Kafka와 같은 AMQP 프로토콜을 사용하는 메세지 브로커를 이용해야 한다.
다음 dependency를 추가해주자.
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-bus-amqp'
}
해당 의존성을 주입해 두었다면, 프로젝트에 메세지 브로커를 연결해 두는 것만으로 busrefresh를 이용할 수 있다.
#RabbitMQ 설치
$ brew install rabbimq
#RabbitMQ 서버 시작
$ brew services start rabbitmq
만약 배포 상황이라면, docker 이미지로 실행해도 되겠다.
$ docker run -d rabbitmq
rabbitMQ 서버를 구동했다면, refresh가 필요한 서버에 해당 설정을 추가한다.
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
rabbitMQ의 기본 포트는 5672이고, 기본 id와 password 는 모두 guest이다.
만약 rabbitMQ의 ID와 비밀번호를 바꾸고 싶다면, http://localhost:15672로 들어가서 로그인하고 계정을 설정해주면 되겠다.
마지막으로, 각 서버의 actuator endpoint에 busrefresh를 열어두자.
management:
endpoints:
web:
exposure:
include: busrefresh
설정을 모두 마쳤다면, 설정 정보를 바꾼 뒤 busrefresh를 해보자.
/actuator/busrefresh로 POST 요청을 보내면 204가 오지만,
Spring Cloud Bus가 변경된 설정 정보를 적용할 모든 서비스들에게 정상적으로 refresh를 해준다.
'BackEnd > Spring Cloud' 카테고리의 다른 글
[ Spring Cloud ] Microservice들 간의 통신 구축하기 (1) - WebClient (0) | 2024.02.14 |
---|---|
[Spring Cloud] Spring Cloud Config Server 설정값을 Private Repository 에서 가져오기 (0) | 2024.02.12 |
[Spring Cloud] Actuator를 이용한 Config Server 기동 (0) | 2024.01.29 |
[Spring Cloud] API Gateway, Filter (1) | 2024.01.08 |
[Spring Cloud] Eureka Server와 Micro Service 설정 (0) | 2024.01.08 |