1. Bus message bus
Spring Cloud Bus is A message bus within the Spring Cloud system, supporting RabbitMQ and Kafka message middleware. The so-called message bus is simply understood as A message center. Many micro service instances can be connected to the bus, and the instances can send or receive information to the message center (by listening). For example, instance A sends A message to the bus, and instance B on the bus can receive information (instance B subscribes to instance A). The message bus acts as an intermediary to decouple instance A and instance B, as shown in the following figure.
Popular definition: bus is called the message bus in spring cloud. It is mainly used to notify all clients to refresh the configuration information by broadcasting when the remote configuration is updated in the micro service system, so as to avoid manually restarting the service.
2. Implementation of refresh configuration principle
3. Set up RabbitMQ service
3.0 download rabbitmq installation package (easier to install with docker)
Official installation package download: https://www.rabbitmq.com/install-rpm.html#downloads
3.1 upload rabbitmq installation package to linux system
erlang-22.0.7-1.el7.x86_64.rpm rabbitmq-server-3.7.18-1.el7.noarch.rpm
3.2 installing Erlang dependency packages
rpm -ivh erlang-22.0.7-1.el7.x86_64.rpm
3.3 install RabbitMQ installation package (Networking required)
yum install -y rabbitmq-server-3.7.18-1.el7.noarch.rpm be careful:After the default installation is completed, the profile template is displayed in the:/usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example In the directory,need Copy profile to/etc/rabbitmq/In the directory,And change the name to rabbitmq.config
3.4 copying configuration files
cp /usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
3.5 view profile location
ls /etc/rabbitmq/rabbitmq.config
3.6 modify the configuration file (see the following figure:)
vim /etc/rabbitmq/rabbitmq.config
Remove%%, and the last comma from the configuration file in the above figure, and modify it to the following figure:
3.7 execute the following command to start the plug-in management in rabbitmq
rabbitmq-plugins enable rabbitmq_management The following description appears: Enabling plugins on node rabbit@localhost: rabbitmq_management The following plugins have been configured: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch Applying plugin configuration to rabbit@localhost... The following plugins have been enabled: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch set 3 plugins. Offline change; changes will take effect at broker restart.
3.8 start RabbitMQ service
systemctl start rabbitmq-server systemctl restart rabbitmq-server systemctl stop rabbitmq-server
3.9 check the service status (see the figure below):
The premise is to turn off the firewall: systemctl stop firewalld
systemctl status rabbitmq-server
● rabbitmq-server.service - RabbitMQ broker
Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled)
Active: active (running) since III 2019-09-25 22:26:35 CST; 7s ago
Main PID: 2904 (beam.smp)
Status: "Initialized"
CGroup: /system.slice/rabbitmq-server.service
├─2904 /usr/lib64/erlang/erts-10.4.4/bin/beam.smp -W w -A 64 -MBas ageffcbf -MHas ageffcbf -
MBlmbcs...
├─3220 erl_child_setup 32768
├─3243 inet_gethost 4
└─3244 inet_gethost 4
...
3.10 accessing the web management interface
This ip is changed according to your virtual machine or host address
192.168.159.22: 15672
3.11 login management interface
username:guest password:guest
4. Configure unified configuration center to connect to MQ service through Bus
4.1 introduction of Bus dependency in unified configuration center
In fact, Bus dependency should be introduced into all services
<!--introduce bus rely on--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
4.2 config server configuration file
#Connect to mq service through Bus component #mq host spring.rabbitmq.host=192.168.159.22 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.virtual-host=/
4.3 restart config server
5. Configure the micro service (config client) to link the MQ service through the Bus
5.1 introduce Bus dependency into all microservices
<!--introduce bus rely on--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
5.2 configure MQ connection configuration in all micro service items, which should be put into remote warehouse management
5.3 an error occurs when restarting all microservices
error cause: the bus dependent startup is introduced, and the mq server is immediately connected according to the bus configuration in the configuration file. However, at this time, the mq configuration information is in the remote warehouse, so the bus cannot connect to mq and directly reports an error, preventing the application from starting.
&emsp**; Solution * *: allow the bus component to connect to mq immediately when the project is started. This fails because the bus component can be initialized with the remote configuration after obtaining the remote configuration
#At startup, when the remote configuration has not been pulled completely, all failures during project startup are allowed spring.cloud.config.fail-fast=true
5.4 automatic configuration update is realized by sending a post request to the unified configuration of config server
Note: / Actor / bus refresh must be exposed in config server:
Add configuration:
#Turn on all web endpoint exposure management.endpoints.web.exposure.include=*
Refresh all services:
http://localhost:8848(configserver address) / Actor / bus refresh
Refresh the specified service:
http://localhost:8848(configserver address) / Actor / bus refresh / service id
6. Summary
there is no problem with this implementation method, but it is still a little insufficient. That is, after the remote configuration is updated, we still need to think that the post request can be sent automatically when the remote configuration is updated? Of course.
Please see this article about the automatic refresh of Bus integrated webhook: https://blog.csdn.net/qq_43753724/article/details/120107658