Spring Cloud Bus service bus, realize global broadcast/fixed-point notification

Reprinted Disclaimer: The source of the article is to carry sacks of teenager


Write at the beginning

Continued from the previous article: Spring Cloud Config implements distributed configuration center and dynamic refresh mechanism configuration . For the problems of Spring Cloud Config:

  1. 无法真正实现:一处通知、处处生效;
  2. 无法实现精确通知,只通知集群中的某些服务(精确通知,比如有100台机器,只通知前98台)

With the above two questions, let's talk about the Spring Cloud Bus service bus.

Environmental description

  1. Config Server Server configuration center, port number: 3344
  2. Client, port number: 3355, 3366, (using cluster mode, demonstration of precise notification)
  3. Tips: For environment-related configuration, please refer to the previous article: Spring Cloud Config realizes distributed configuration center and dynamic refresh mechanism configuration

1. Understand Spring Cloud Bus

  Comes with a direct flight ticket: Spring Cloud Bus official website

2.1 Where is Bus sacred (what is Bus)

  In the micro-services architecture systems often use lightweight message broker to build a common theme of the message , and let all the micro-example service systems are connected up. Since the message that the topic will be generated in all instances of listening and consumption, so called message bus . Each instance on the bus can conveniently broadcast some messages that need to be known to other instances connected to the topic.

  Spring Cloud Bus is used to 分布式系统的节点the 轻量级消息系统frame connecting, it integrates functions and event handling mechanism of Java messaging middleware .Spring Cloud Bus 目前仅支持 RabbitMQ 和 Kafka。

2.2 Bus principle

  The Config client example all listen to the same topic in MQ (default is springCloudBus). When a service refreshes the data, it will put this message into the topic, so that other services listening to the same topic can be notified, and then update its configuration.就是通过 MQ 消息队列的 Topic 机制,达到广播的效果。

Insert picture description here

2. Two design ideas of Bus

Choose Spring Cloud Bus to send Topic messages. There are two design ideas in technology selection:

2.1 Trigger the client

  Using the message bus, triggering a client 's /bus/refreshendpoint. Send messages to the Bus through the client to refresh the configuration of all clients.
Insert picture description here

2.2 Trigger the server

  Using the message bus, triggering a server 's /bus/refreshendpoint. Send messages to the Bus through the Config Server server to refresh the configuration of all clients.
Insert picture description here

2.3 How to choose

  According to Chart apparently 图二more appropriate, it is recommended to use 触发服务端 Config Serverway. 图一触发客户端方式The reasons for unsuitability are as follows:

  1. Use the message bus to trigger the client mode, 打破了微服务的职责单一性because the microservice itself is a business module, it should not be responsible for configuration refresh;
  2. Trigger the client mode, 破坏了微服务各个节点之间的对等性(for example: 3355/3366/3377 cluster mode provides services, at this time 3355 also needs message notification, which affects the peering of nodes)
  3. 有一定的局限性。When microservices are migrated, network addresses will often change. If automatic refresh is required at this time, more modifications will be added.

3. Environment setup

  Spring Cloud Bus 目前仅支持 RabbitMQ 和 Kafka。Here select RabbitMQ, Rabbit MQ needs Erlang environment. So let’s install Erlang and Rabbit MQ first

  Erlang download address: http://erlang.org/download/otp_win64_21.3.exe ;
  RabbitMQ download address: https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server- 3.7.14.exe

  If the download is too slow, you can also come here, I will help you download it. Address: https://pan.baidu.com/s/16m20KfU4x6nkCYXz-L_c5Q (Extraction code: yevm)

  Downloaded here is the Window version, fool-proof installation, 下一步下一步完成 , OK. If you need a Linux version, please understand the installation yourself. At this time, the cmd command line enters the Rabbit MQ installation directory sbin, and use the command: rabbitmq-plugins enable rabbitmq_managementStart RabbitMQ while adding the Rabbit visualization plug-in.
Insert picture description here
Supplement: Initial startup diagram
Insert picture description here

  Such input in the browser http://localhost:15672will be able to access Rabbit MQ visual interface.
Insert picture description here

4.Bus dynamically refresh the global broadcast configuration

  According to the above: the Spring Cloud Config implement a distributed configuration center, dynamic refresh mechanism configuration , according cloud-config-center-3355to an identical configuration of the client cloud-config-center-3366. It forms a cluster mode with 3355 to facilitate 定点通知the demonstration of the Bus effect.

4.1 Cluster version client setup

  cloud-config-center-3366 Please refer to 3355, which will not be introduced here.

4.2 Server configuration center/client pom introduces Bus bus dependency

服务端Introduce Bus bus dependency   in configuration center Config Server (3344) and client cluster (3355/3366)

<!--添加消息总线支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

 
  
  

4.3 Modify application.yml of server configuration center (add rabbitmq related configuration)

#添加rabbitmq相关支持(新加)
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#rabbitmq相关配置,暴露bus舒心配置的端点
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'  #为什么配置 bus-refresh,看传染病那张图

 
  
  

4.4 Modification of client application.yml (also add rabbitmq related configuration)

#添加rabbitmq相关支持
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"  #此处有很多选项可以配置,为了省事 ,直接配置 *

 
  
  

4.5 Start the module and start the test

  Startup: Server configuration center Config Server (3344), client cluster (3355/3366), Eureka Server (7001). GitHub modify the configuration parameters, and to 服务端request transmission Post, command: curl -X POST "http://localhost:3344/actuator/bus-refresh",

  After sending a request to the Post Config Server, each instance (client 3355/3366) on the bus is timely 监听和消费configuration changes. Using the broadcast method, the real realization 一处通知,处处生效. As shown in FIG test:
Insert picture description here
  using MQ broadcasting manner to achieve 一处通知,处处生效the effect. At this point we landed Rabbit MQ client, in Exchangesthe module, you can see called springCloudBusa Topic.

  This article 2.2 Bus 原理introduces match: Config client example, are listening to the same topic MQ in the (default springCloudBus). When a service refreshes the data, it will put this message into the topic, so that other services listening to the same topic can be notified, and then update its configuration.
Insert picture description here

5. Bus dynamic refresh fixed-point notification configuration

  If you need to 差异化通知, you do not want to be global broadcast, this time on the use of the Bus 定点通知function.

  This time we demonstrate through the client cluster (3344/3355). After the GitHub remote configuration is modified, differentiated fixed-point notification will be performed, only 3355 is notified, and 3366 is not notified. The command here is a bit different from the global broadcast, the command is:http://配置中心IP:配置中心的端口号/actuator/bus-refresh/{destination}

  By specifying /bus/refresh请求no longer sent to specific service instances, but Config Server and distributed by {destination} 参数the need to update service or instance configuration to specify.

  {destination} 参数= Microservice name: port number . 3355 Micro service config-clientname: . Post sent here to request final command: curl -X POST http://localhost:3344/actuator/bus-refresh/config-client:3355true realization 精确通知function. The test is shown in the figure:
Insert picture description here

The code in this article is integrated and packaged with Spring Cloud Config. The code download address is: Spring Cloud Config + Spring Cloud Bus to achieve unified service configuration, one notification is effective everywhere, and precise notification function (extract code: yevm)
 
Next: Spring Cloud Stream message driver, Easier communication between MQ

Guess you like

Origin blog.csdn.net/m0_37989980/article/details/108559728