SpringCloud distributed micro-cloud infrastructure services Section 8: Message Bus (Spring Cloud Bus

Spring Cloud Bus nodes with distributed lightweight message broker connected. It can be used for communication between broadcast service or change the configuration file, it can also be used for monitoring. This article is about the implementation changes to the configuration file notice of micro-services architecture with Spring Cloud Bus.

First, the preparatory work
paper or based on an article to achieve. According to the official document, we only need to configure in the configuration file springcloud-starter-bus-amqp; understand springcloud architecture can be added to beg: 3536247259, which means that we need to install rabbitMq, click rabbitmq download. As for how to use the next rabbitmq, search engines.

Second, the transformation config-client
in the pom file with start-dependent spring-cloud-starter-bus- amqp, complete configuration file 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>

Plus RabbitMq configuration in the configuration file application.properties, including RabbitMq address, port, user name and password. And the need to add three spring.cloud.bus configuration, 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

ConfigClientApplication startup class code 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;
    }
}

In turn starts eureka-server, confg-cserver, start two config-client, port: 8881,8882.

Visit http: // localhost: 8881 / hi or http: // localhost: 8882 / hi browser shows:

foo version 3

Then we go to code repository will change the value of foo "foo version 4", that is, change the value of the configuration file foo. If it is a traditional practice, we need to restart the service in order to achieve update the configuration file. At this point, we only need to send post request: HTTP: // localhost: 8881 / Actuator / Bus-Refresh, you will find the config-client will re-reads the configuration file

SpringCloud distributed micro-cloud infrastructure services Section 8: Message Bus (Spring Cloud Bus

Reread the configuration file:
SpringCloud distributed micro-cloud infrastructure services Section 8: Message Bus (Spring Cloud Bus
Then we'll visit http: // localhost: 8881 / hi or http: // localhost: 8882 / hi browser shows:

foo version 4

In addition, / actuator / bus-refresh service interface can be specified that the use of "destination" parameter, such as "/ actuator / bus-refresh destination = customers:? **" refresh service that is named customers of all services.

Third, the analysis
at this time architecture diagram:

SpringCloud distributed micro-cloud infrastructure services Section 8: Message Bus (Spring Cloud Bus

When git file changes, the post with through pc-config-client port 8882 of the transmission request / bus / refresh /; port 8882 at this time will send a message, a message delivered by the bus to other services, so that the entire microService clusters have reached update the configuration file.

Guess you like

Origin blog.51cto.com/14622290/2459409