Amateur Grass Springcloud Tutorial | Eighth article: Message bus (Spring Cloud Bus) (Finchley version)

Source: Internet
Author: User
Tags rabbitmq download rabbitmq

Spring Cloud Bus connects the distributed nodes with a lightweight message broker. It can be used to broadcast configuration file changes or communication between services, and can also be used for monitoring. This article is about changes to the configuration file that notifies the MicroServices architecture with spring Cloud bus.

First, the preparatory work

This article is also 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, which means we need to install RABBITMQ, click Rabbitmq Download. As for how to use RABBITMQ, search engine under.

Second, the transformation of config-client

In the Pom file plus the start dependent SPRING-CLOUD-STARTER-BUS-AMQP, the complete configuration file is as follows:

1<dependencies>2<dependency>3<groupId></groupId>4<artifactId>spring-cloud-starter-config</artifactId>5</dependency>6  7<dependency>8<groupId>org.springframework.boot</groupId>9<artifactId>spring-boot-starter-web</artifactId>Ten</dependency> One   A<dependency> -<groupId></groupId> -<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> the</dependency> -   -<dependency> -<groupId></groupId> +<artifactId>spring-cloud-starter-bus-amqp</artifactId> -</dependency> +   A<dependency> at<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-actuator</artifactId> -</dependency>

In the configuration file, add RABBITMQ configuration, including RABBITMQ address, port, user name, password. and need to add three configuration, the following:

1  2 spring.rabbitmq.port=56724 Spring.rabbitmq.username=Guest5 spring.rabbitmq.password=guest6  7 Management.endpoints.web.exposure.include=bus-refresh

The Configclientapplication startup class code is as follows:

1 @SpringBootApplication2 @EnableEurekaClient3 @EnableDiscoveryClient4 @RestController5 @RefreshScope6  Public classconfigclientapplication {7  8     /**9      * http://localhost: 8881/actuator/bus-refreshTen      */ One   A      Public Static voidMain (string[] args) { (configclientapplication.class, args); -     } the   -@Value ("${foo}") - String foo; -   +@RequestMapping (value = "/hi") -      PublicString hi () { +         returnfoo; A     } at}

Start Eureka-server, Confg-cserver, start two config-client, the port is: 8881, 8882.

Access Http://localhost:8881/hi or Http://localhost:8882/hi browser display:

Foo Version 3

At this point we go to the code warehouse to change the value of foo to "Foo version 4", which changes the value of the config file foo. If it is a traditional practice, the service needs to be restarted in order to reach the update of the configuration file. At this point, we only need to send the POST request: Http://localhost:8881/actuator/bus-refresh, you will find that config-client will re-read the configuration file

To re-read the configuration file:

We then visit Http://localhost:8881/hi or Http://localhost:8882/hi browser to display:

Foo version 4

In addition, the/actuator/bus-refresh interface can specify a service that uses "destination" parameters, such as "/actuator/bus-refresh?destination=customers:**" That is, all services named Customers are refreshed.

Third, analysis

The architecture diagram at this point:

When the git file changes, through the PC side with post to port 8882 config-client send request/bus/refresh/, 8882 Port will send a message, by the message bus to other services, so that the entire microservices cluster to reach the update configuration file.

This article source code download:


V. References


Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!

Amateur Grass Springcloud Tutorial | Eighth article: Message bus (Spring Cloud Bus) (Finchley version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.