티스토리 뷰

MSA

Section 8: Spring Cloud Bus

✨✨✨✨✨✨✨ 2023. 6. 7. 17:37
반응형
  • Spring Cloud Bud
  • RabbitMQ 설치
  • 프로젝트 수정 - Actuator
  • 테스트

Spring cloud bus 사용함으로써, 분산 시스템의 노드(MS)를 경량 메시지 브로커(RabbitMQ)와 연결하여 상태 및 구성에 대한 변경 사항을 연결된 노드에게 전달(Broadcast)할 수 있다.

 

Spring Cloud Bus가 Spring Cloud Config Server 에 변경된 설정 정보를 Push하면 (HTTP POST /busrefresh)

Spring Cloud Config Server 는 설정정보를 update하여 연결된 MS들에게 변경사항을 전달한다.

MS에게 전달해도 전체 반영이된다. 자세한 내용은 실습을 통해 알아볼것이다.

Section7에서처럼 MS 하나씩 refresh할 필요가 없어진다.

Spring Cloud Bus

AMQP (Advanced Message Queuing Protocol)

  • 메시지 지향 미들웨어를 위한 개방형 표준 응용 계층 프로토콜이다.
  • 메시지지향, 큐잉, 라우팅(P2P, Publisher-Subcriber), 신뢰성, 보안
  • RabbitMQ에서 사용한다.

Kafka 프로젝트

  • Apache Software Foundation이 Scalar 언어로 개발한 오픈소스 메시지 브로커 프로젝트
  • 분산형 스트리밍 플랫폼
  • 대용량의 데이터를 처리 가능한 메시징 시스템

 

메시지 큐잉 서비스 대표 2가지

RabbitMQ Kafka
메시지 브로커
초당 20+ 메시지를 소비자들에게 전달
메시지 전달 보장, 시스템 간 메시지 전달
브로커, 소비자 중심이다.
초당 100k+ 이상의 이벤트를 처리
Pub/Sub, Topic에 메시지 전달
Ack를 기다리지 않고 전달 가능하다
생산자 중심

 


RabbitMQ 설치

brew update
brew install rabbitmq
geumbit@gimgeumbich-ui-MacBookPro sbin % brew --prefix rabbitmq
/opt/homebrew/opt/rabbitmq
geumbit@gimgeumbich-ui-MacBookPro sbin % cd /opt/homebrew/opt/rabbitmq/sbin/
geumbit@gimgeumbich-ui-MacBookPro sbin % ./rabbitmq-server

 

Username : guest

Password : guest

 


AMQP (Advanced Message Queuing Protocol)

해당 작업은 아래 리스트들에 추가할 것이다.

  1. Start RabbitMQ Server
  2. Start Spring Cloud Config Service
  3. Start Eureka Discovery Service
  4. Start Spring Cloud Gateway Service
  5. Start User Microservice

config-service

config-service, user-service, apigateway-service - pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

 

config-service, user-service, apigateway-service - application.yml

rabbitmq:
  host: 127.0.0.1
  port: 5672
  username: guest
  password: guest

management:
  endpoints:
    web:
      exposure:
        include: health, busrefresh

 

busrefresh

1. configuration 서버에 변경 요청사항이 들어오게되면,

2. 자신이 rabbitmq에게 요청사항받음을 통보하고

3. rabbitmq에 등록된 연결된 MS들을 일괄적으로 Push해준다.

 

 

그럼 정말 Rabbitmq가 변경을 확인하여 MS들을 일괄로 변경해주는지 확인해본다

 

1. ~/msa-config/application.yml 파일 수정

변경 전

token:
    expiration_hours: 24
    secret: change-value2-default-application

gateway:
    ip: 172.30.1.88
변경 후
token:
    expiration_hours: 24
    secret: change

gateway:
    ip: 172.30.1.88

 

teken.secret값을 바꾸면 실시간으로  config-service 에 내용이 적용이 된다

 

하지만 apigateway-service 의 경우, 실시간으로 적용이 안된다.

 

이를 실시간으로 적용하기 위해서는 apigateway-service 혹은 user-service에 아래와 같이 busrefresh를 날린다.

 

 

그렇게되면 응답으로 204 코드가 표시되면서 userservice에 아래와 같은 로그가 표시된다

2023-05-29 17:18:06.570  INFO 36385 --- [o-auto-1-exec-2] o.s.cloud.bus.event.RefreshListener      : Received remote refresh request.
2023-05-29 17:18:06.614  INFO 36385 --- [o-auto-1-exec-2] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-05-29 17:18:06.677  INFO 36385 --- [o-auto-1-exec-2] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : <http://127.0.0.1:8888>
2023-05-29 17:18:06.712  INFO 36385 --- [o-auto-1-exec-2] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=ecommerce, profiles=[prod], label=null, version=null, state=null
2023-05-29 17:18:06.712  INFO 36385 --- [o-auto-1-exec-2] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-file:/Users/geumbit/study/msa-config/ecommerce-prod.yml'}, BootstrapPropertySource {name='bootstrapProperties-file:/Users/geumbit/study/msa-config/ecommerce.yml'}, BootstrapPropertySource {name='bootstrapProperties-file:/Users/geumbit/study/msa-config/application.yml'}]
2023-05-29 17:18:06.716  INFO 36385 --- [o-auto-1-exec-2] o.s.boot.SpringApplication               : The following profiles are active: prod
2023-05-29 17:18:06.722  INFO 36385 --- [o-auto-1-exec-2] o.s.boot.SpringApplication               : Started application in 0.148 seconds (JVM running for 97.749)
2023-05-29 17:18:06.952  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2023-05-29 17:18:09.965  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2023-05-29 17:18:09.977  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/user-service:39b1f7d8d71b73e4138427701d28b3f4 - deregister  status: 200
2023-05-29 17:18:09.993  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
2023-05-29 17:18:09.997  INFO 36385 --- [o-auto-1-exec-2] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2023-05-29 17:18:10.000  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2023-05-29 17:18:10.000  INFO 36385 --- [o-auto-1-exec-2] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2023-05-29 17:18:10.004  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2023-05-29 17:18:10.004  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2023-05-29 17:18:10.004  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2023-05-29 17:18:10.004  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Application is null : false
2023-05-29 17:18:10.004  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2023-05-29 17:18:10.004  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2023-05-29 17:18:10.004  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2023-05-29 17:18:10.035  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : The response status is 200
2023-05-29 17:18:10.038  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2023-05-29 17:18:10.040  INFO 36385 --- [o-auto-1-exec-2] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2023-05-29 17:18:10.042  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1685348290042 with initial instances count: 2
2023-05-29 17:18:10.044  INFO 36385 --- [o-auto-1-exec-2] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application USER-SERVICE with eureka with status DOWN
2023-05-29 17:18:10.045  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1685348290045, current=DOWN, previous=STARTING]
2023-05-29 17:18:10.046  INFO 36385 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/user-service:1bf83c22248c5dd0f76f8fddbfff9bdb: registering service...
2023-05-29 17:18:10.046  INFO 36385 --- [o-auto-1-exec-2] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application USER-SERVICE with eureka with status UP
2023-05-29 17:18:10.046  INFO 36385 --- [o-auto-1-exec-2] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1685348290046, current=UP, previous=DOWN]
2023-05-29 17:18:10.047  INFO 36385 --- [o-auto-1-exec-2] o.s.cloud.bus.event.RefreshListener      : Keys refreshed []
2023-05-29 17:18:10.063  INFO 36385 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/user-service:1bf83c22248c5dd0f76f8fddbfff9bdb - registration status: 204
2023-05-29 17:18:10.063  INFO 36385 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/user-service:1bf83c22248c5dd0f76f8fddbfff9bdb: registering service...
2023-05-29 17:18:10.066  INFO 36385 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER-SERVICE/user-service:1bf83c22248c5dd0f76f8fddbfff9bdb - registration status: 204

자세히 보면

DiscoveryClient_USER-SERVICE/user-service:1bf83c22248c5dd0f76f8fddbfff9bdb - registration status: 204

라는 문구가 표시되어 있으며 이와함께 apigateway-service에서도 다음과 같이 표시된다.

~~ 생략 ~~
DiscoveryClient_APIGATEWAY-SERVICE/192.168.0.15:apigateway-service:8000 - registration status: 204
~~ 생략 ~~

apigateway-service 역시 204 응답이 받아지면서 application.yml 수정내용이 적용됨을 알 수 있다.

 

반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함