Spring Cloud Bus connects distributed nodes with lightweight message proxies.It can be used to broadcast profile changes or communication between services, or to monitor.This article describes using Spring Cloud Bus to implement configuration file changes that notify the microservice architecture.
I. Preparations
This article is based on the previous one.According to the official documentation, we only need to configure spring-cloud-starter-bus-amqp in the configuration file; there is an additional requirement to understand the spring cloud architecture: 35362272599, which means we need to install rabbitMq and click on the rabbitmq download.As to how to use rabbitmq, under the search engine.
2. Rebuilding config-client
Add the starting dependency on spring-cloud-starter-bus-amqp to the pom file, and the complete configuration file is as follows:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Add the configuration of RabbitMq to the profile application.properties, including the address, port, username, and password of RabbitMq.Three configurations of spring.cloud.bus need to be added as follows:
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.cloud.bus.enabled=true spring.cloud.bus.trace.enabled=true management.endpoints.web.exposure.include=bus-refresh
The ConfigClientApplication startup class code is as follows:
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @RestController @RefreshScope public class ConfigClientApplication { /** * http://localhost:8881/actuator/bus-refresh */ public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${foo}") String foo; @RequestMapping(value = "/hi") public String hi(){ return foo; } }
Start eureka-server, confg-cserver in turn, and start two config-client s with ports 8881 and 8882.
Visit http://localhost:8881/hi or http://localhost:8882/hi browser to display:
foo version 3
At this point we go to the code repository and change the value of foo to "foo version 4", that is, change the value of the configuration file foo.If it is a traditional practice, the service needs to be restarted to update the configuration file.At this point, we only need to send a post request: http://localhost:8881/actuator/bus-refresh, you will find that config-client reads the configuration file again
Re-read configuration file:
Then we visit http://localhost:8881/hi or http://localhost:8882/hi browser display:
foo version 4
In addition, the / actuator/bus-refresh interface can specify services, even if the'destination'parameter is used, such as'/actuator/bus-refresh?destination=customers:**' to refresh all services whose service name is customers.
3. Analysis
Architecture diagram at this time:
When the git file changes, send a request/bus/refresh/ via post to the config-client with port 8882 on the pc side; at this point, port 8882 sends a message, which is passed by the message bus to other services, enabling the entire micro-service cluster to update the configuration file.