1. Introduction
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.
spring cloud bus optional message agent build includes RABBITMQ, AMQP, and Kafka cloud " to refresh the configuration file for the change micro-service. So you need to install RABBITMQ, click Rabbitmq Download. Please search by yourself for installation and usage.
2, retrofit config-client
1, this function only need to transform the Config-client project, first introduced in Pom.xml based on the RABBITMQ implementation of Spring Cloud bus start dependent SPRING-CLOUD-STARTER-BUS-AMQP, the code is as follows
<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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.lishun</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>Com.lishun</groupId> <Artifactid>Cloud</Artifactid> <version>1.0-snapshot</version> <RelativePath/> <!--Lookup parent from repository - </Parent> <Dependencies> <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> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-starter-config</Artifactid> </Dependency> </Dependencies></Project>
2, in the configuration file bootstrap.properties add RABBITMQ configuration, including RABBITMQ address, port, user name, password. and need to add spring.cloud.bus three configuration, the following:
spring.rabbitmq.host=localhostspring.rabbitmq.port=5672 spring.rabbitmq.username=guestspring.rabbitmq.password= Guestspring.cloud.bus.enabled=truespring.cloud.bus.trace.enabled=truemanagement.endpoints.web.exposure.include =bus-refresh
3, finally , it is necessary to add an @ refreshscope annotation on the updated collocation class, and only add this annotation to update the configuration without restarting the service, the code is as follows
@SpringBootApplication @restcontroller@refreshscope Public class configclientapplication { publicstaticvoid main (string[] args) { Springapplication.run (configclientapplication. class , args); } @Value ("${id}") String ID; = "/hi") public String hi () { return ID;} }
4, start Eureka-server, Confg-cserver, start two config-client, the port is: 8881, 8882.
5, visit Http://localhost:8881/hi or Http://localhost:8882/hi browser display:
Config-test
6, the value of the profile ID on GitHub to config-test-11, if it is a traditional practice, you need to restart the service to achieve the configuration file updates. We only need to send a POST request: Http://localhost:8881/actuator/bus-refresh, in the console you will find that config-client will re-read the configuration file.
7, we then visit Http://localhost:8881/hi or Http://localhost:8882/hi browser display:
Config-test-11
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.
Springcloud Learning-Message bus (Spring Cloud bus)