For reprinting, please indicate the source:
Spring Cloud Bus connects distributed nodes to lightweight message brokers. This can be used for broadcasting configuration file changes or other management work. One of the key ideas is that message bus can monitor micro services or communicate with each other as applications. This article describes the use of AMQP to notify the configuration file changes of the micro-service architecture.
I. Preparations
This article is based on the previous article. According to the official document, we only need to configure spring-cloud-starter-bus-amqp in the configuration file; that is, we need to install rabbitMq and click rabbitmq Download. As for how to use rabbitmq, under the search engine.
2. Reconstructing config-client
Add spring-cloud-starter-bus-amqp to the pom file. The complete configuration file is as follows:
<?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> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
Add:
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 # spring.rabbitmq.username= # spring.rabbitmq.password=
If rabbitmq has a username password, enter it.
Start eureka-server and confg-cserver in turn, and start two config-client s with ports 8881 and 8882.
Visit http://localhost:8881/hi perhaps http://localhost:8882/hi Browser display:
foo version 3
Then we go Code Warehouse Change the value of foo to "foo version 4", that is, change the value of the configuration file foo. If it is a traditional practice, you can restart the service to update the configuration file. At this point, we just need to post the request: http://localhost:8881/bus/refresh:
Read the configuration file again:
Then we will visit again. http://localhost:8881/hi perhaps http://localhost:8882/hi Browser display:
foo version 4
In addition, the / bus/refresh interface can specify services, even with the "destination" parameter, such as "/ bus/refresh?destination=customers:**", which refreshes all services named customers, regardless of ip.
III. Analysis
The architecture diagram at this time:
When the git file changes, the request/bus/refresh/ is sent to the config-client with port 8882 by post on the pc side; at this time, the 8882 port sends a message, which is transferred from the message bus to other services, so that the whole micro-service cluster can update the configuration file.
IV. OTHER
It can be used as a custom Mesage Broker, just spring-cloud-starter-bus-amqp, and then write the configuration file on it, as above.
Tracing Bus Events:
Need to set: spring.cloud.bus.trace.enabled=true, if you do, then Spring Boot Trace Repository (if it exists) will display all events and all ack sent by each service instance, such as: (from the official website)
{ "timestamp": "2015-11-26T10:24:44.411+0000", "info": { "signal": "spring.cloud.bus.ack", "type": "RefreshRemoteApplicationEvent", "id": "c4d374b7-58ea-4928-a312-31984def293b", "origin": "stores:8081", "destination": "*:**" } }, { "timestamp": "2015-11-26T10:24:41.864+0000", "info": { "signal": "spring.cloud.bus.sent", "type": "RefreshRemoteApplicationEvent", "id": "c4d374b7-58ea-4928-a312-31984def293b", "origin": "customers:9000", "destination": "*:**" } }, { "timestamp": "2015-11-26T10:24:41.862+0000", "info": { "signal": "spring.cloud.bus.ack", "type": "RefreshRemoteApplicationEvent", "id": "c4d374b7-58ea-4928-a312-31984def293b", "origin": "customers:9000", "destination": "*:**" } }
Download the source code of this article:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter8