Spring Cloud Bus connects distributed nodes with lightweight message brokers. It can be used to broadcast changes to configuration files or communication between services, as well as for monitoring. This article describes the implementation of the Spring Cloud Bus for notifying microservice architecture configuration file changes.

One, preparation work this article is based on the last article to achieve. According to the official documentation, we only need to configure spring-cloud-starter-bus-AMQp in the configuration file; To learn about the SpringCloud architecture, add: 3, 5, 3, 6, 24, 7, 259, which means we need to install rabbitMq, click rabbitMq to download. For how to use RabbitMQ, search engine.

2. Modify the config-client poM file and start relying on spring-cloud-starter-bus-AMQP. 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>



Copy the code

Add the RabbitMq configuration, including RabbitMq address, port, username and password, to the configuration file application.properties. Three configurations of Spring.cloud. bus are required 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



Copy the code

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() {returnfoo; }}Copy the code

Start eureka-server and Confg-cserver in sequence, and start two config-clients with ports 8881 and 8882.

Visit http://localhost:8881/hi or http://localhost:8882/hi and the browser displays:

foo version 3  

At this point we go to the repository and change the value of foo to “foo Version 4”, that is, change the value of the configuration file foo. In the traditional way, you need to restart the service 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 the config – client will reread the configuration file



Re-read the configuration file:



At this point, we go to http://localhost:8881/hi or http://localhost:8882/hi and see:

 foo version 4

Iii. Analyze the architecture diagram at this time:



When the git file is changed, the PC sends a request /bus/refresh/to the config-client with port 8882 through POST. At this point port 8882 sends a message that is passed by the message bus to other services, enabling the entire microservice cluster to update the configuration file.