Spring CloudBus connects distributed nodes to lightweight message brokers. This can be used to broadcast configuration file changes or for other administrative purposes. A key idea is that the message bus can monitor microservices and also communicate with each other as applications.

First, preparation

This article is based on the previous article to achieve. According to the official documentation, we only need to configure spring-cloud-starter-bus-AMQp in the configuration file; This means we need to install rabbitMq, click rabbitMq to download. For how to use RabbitMQ, search engine.

2. Modify config-client

Add spring-cloud-starter-bus-AMqp to the POM file. 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() {return foo;
    }
    
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

Alternatively, the /bus/refresh interface can specify a service using a “destination” parameter, such as “/bus/refresh? Destination =customers:** “refresh all services with the service name customers, regardless of IP. Welcome to study together with the relevant technology is willing to understand the source code of friends directly beg to exchange technology: 2147775633

Three, the analysis

Architecture diagram at this point:

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.