Construction and use of springcloud-bus message bus

For the entire project, please go to gitee to view https://gitee.com/xwb1056481167/spring-cloud to view

The project used cloud-config-center-3344,

Message bus

In a system with a microservice architecture, a lightweight message broker is usually used to build a common message topic, and other microservice instances in the system are connected, and
the messages generated in the topic will be monitored and consumed by all instances , In each instance on the bus, you can easily broadcast some news that needs to be known to other forces linked to the subject.

principle

The ConfigClient instances all listen to the same topic in MQ. The default is springCloudBus. When a service refreshes data, she will put the message in the topic, so that other services that listen to the same topic can be notified, and then update its own Configuration.

The bus currently only supports RabbitMQ and Kafka

Install RabbitMQ

1. Install erlang first

Download link: http://erlang.org/download/otp_win64_21.3.exe

Or other address http://erlang.org/download/

2. Download RabbitMQ&install

1. Download and install the
installation version: https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.14/rabbitmq-server-3.7.14.exe
decompression version: https://github.com/ rabbitmq/rabbitmq-server/releases/download/v3.7.14/rabbitmq-server-windows-3.7.14.zip

2. Enter the sbin directory, and execute rabbitmq-plugins enable rabbitmq_management in cmd

rabbitmq-plugins enable rabbitmq_management

The following message appears and the installation is complete

The following installation information appears under windows to check whether the installation is successful

http://localhost:15672/ 

Default password guest: guest

 

Broadcast notification two architectural ideas


The first type: (use the message bus to trigger a client /bus/refresh without refreshing the configuration of all other clients)

The second type: (using the message bus to trigger a configServer's /bus/refresh) breakpoint, and refresh the configuration of all clients

 

Configuration in two modes

SpringCloud Bus dynamically refreshes the global broadcast

The server is 3344, and the two clients are 3355 and 3366. After modifying github, you only need to notify 3344, and then 3355 and 3366 will automatically get the latest content through the springCloudBus news subscribed by rabbitMQ.

1. Message master control 3344 configuration

Cloud-config-center-3344 configuration

1. Integrate rabbitMQ in the master control message center

<!-- 添加消息总线RabbitMQ的支持(包含了bus和rabbitMQ) -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2. yml configures the configuration of rabbitMQ

spring:
  #RabbitMQ的配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
# rabbitmq相关配置,暴露bus刷新配置的断点
management:
  endpoints: #暴露bus刷新配置的断点
    web:
      exposure:
        include:  'bus-refresh'

Two, configure the client 3355, 3366

Configure cloud-config-client-3355, 3366

1. Pom adds rabbitMQ support

<!-- 添加rabbitmq的支持 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

spring-cloud-starter-bus-amqp integrates bus and RabbitMQ

2. yml configuration

spring:
  #rabbitmq的配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#暴露监控断点
management:
  endpoints:
    web:
      exposure:
        include: "*" #*表示健康等其他参数,统一用*代替了

The configuration of the 3366 is the same as that of the 3355. Omitted here

test

Start sequence 7001, 3344, 3355, 3366 to  perform the bus-refresh refresh of the master control.
If there is no curl command, you need to download https://curl.haxx.se/download/curl-7.73.0.zip

url -X POST "http://localhost:3344/actuator/bus-refresh"

Effect: Modified the github file, 3344 accesses the latest in time, after manually executing the 3344 master control command, 3355 and 3366 are both the latest configuration files.

SpringCloud Bus dynamic refresh fixed-point notification

Note: Two config (3355, 3366) clients at the same time, only want to notify 3355, but don't want to notify 3366

Execute the following command

curl -X POST "http://localhost:3344/actuator/bus-refresh/{destination}"

Note: The /bus/refresh request is not sent to a specific server instance, but is sent to the config server and specifies the service or instance whose configuration needs to be updated through the destination parameter class.
The destination is: service name: port number, that is: spring.application.name:server.port
eg: curl -X POST " http://localhost:3344/actuator/bus-refresh/config-client:3355 "

Guess you like

Origin blog.csdn.net/www1056481167/article/details/113584610