Reprinted Disclaimer: The source of the article is to carry sacks of teenager
Contents of this article:
-
- Write at the beginning
- Environmental description
- 1. Understand Spring Cloud Bus
- 2. Two design ideas of Bus
- 3. Environment setup
- 4.Bus dynamically refresh the global broadcast configuration
-
-
- 4.1 Cluster version client setup
- 4.2 Server configuration center/client pom introduces Bus bus dependency
- 4.3 Modify application.yml of server configuration center (add rabbitmq related configuration)
- 4.4 Modification of client application.yml (also add rabbitmq related configuration)
- 4.5 Start the module and start the test
-
- 5. Bus dynamic refresh fixed-point notification configuration
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:
无法真正实现:一处通知、处处生效;
无法实现精确通知,只通知集群中的某些服务(精确通知,比如有100台机器,只通知前98台)
With the above two questions, let's talk about the Spring Cloud Bus service bus.
Environmental description
Config Server
Server configuration center, port number: 3344- Client, port number: 3355, 3366, (using cluster mode, demonstration of precise notification)
- 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 机制,达到广播的效果。
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/refresh
endpoint. Send messages to the Bus through the client to refresh the configuration of all clients.
2.2 Trigger the server
Using the message bus, triggering a server 's /bus/refresh
endpoint. Send messages to the Bus through the Config Server server to refresh the configuration of all clients.
2.3 How to choose
According to Chart apparently 图二
more appropriate, it is recommended to use 触发服务端 Config Server
way. 图一触发客户端方式
The reasons for unsuitability are as follows:
- 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; - 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) 有一定的局限性。
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_management
Start RabbitMQ while adding the Rabbit visualization plug-in.
Supplement: Initial startup diagram
Such input in the browser http://localhost:15672
will be able to access Rabbit MQ visual interface.
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-3355
to 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:
using MQ broadcasting manner to achieve 一处通知,处处生效
the effect. At this point we landed Rabbit MQ client, in Exchanges
the module, you can see called springCloudBus
a 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.
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-client
name: . Post sent here to request final command: curl -X POST http://localhost:3344/actuator/bus-refresh/config-client:3355
true realization 精确通知
function. The test is shown in the figure:
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