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
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId> Com.forezp</groupid> <artifactId>config-client</artifactId> <version>0.0.1-snapshot</ version> <packaging>jar</packaging> <name>config-client</name> <description>demo Project for Spring boot</description> <parent> <groupid>org.springframework.boot</groupid& Gt <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <PR Oject.build.sourceencoding>utf-8</project.build.souRceencoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <j ava.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency& Gt <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-web</artifacti d> </dependency> <dependency> <groupid>org.springframework.boot</groupid& Gt <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eureka </artifactId> </dependency> <dependency> <groupid>org.springframework.boo T</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test< ;/scope> </dependency> <dependency> <groupid>org.springframework.cloud</g Roupid> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring -boot-starter-actuator</artifactid> </dependency> </dependencies> <dependencymanagement& Gt <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>dalston.rc1</version> ; <type>pom</type> <scope>import</scope> </dependency> </d ependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven-plugin& lt;/artifactid> </plugin> </plugins> </build> <repositories> < ;repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <ENABLED&G t;false</enabled> </snapshots> </repository> </repositories></project>
In the configuration file application.properties add RABBITMQ configuration, including RABBITMQ address, port, user name, password, the code is as follows:
spring.rabbitmq.host=localhostspring.rabbitmq.port=5672# spring.rabbitmq.username=# spring.rabbitmq.password=
If RABBITMQ has a username and password, you can enter it.
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/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/bus/refresh interface can specify a service that uses the "destination" parameter, such as "/bus/refresh?destination=customers:**", which refreshes all services named Customers, regardless of IP. SOURCE Source
Springcloud Tutorial (a) message bus (Spring Cloud bus)