社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
对config-client进行改造,引入spring-cloud-starter-bus-amqp,同时对spring-boot-starter-parent进行降级,从2.1.1.RELEASE到2.0.3.RELEASE,配合Finchley.SR2版本,(否则报错 Endpoint ID 'bus-env' contains invalid characters, please migrate to a valid format.),完整的POM如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sc</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Boot of Spring Cloud Config Client</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</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>
</project>
更改配置文件application.properties中的management.endpoints.web.exposure.include,以及引入spring.rabbitmq属性:
management.endpoints.web.exposure.include=*
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=springcloud
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=/
启动两个程序:
java -jar /Users/r/IdeaProjects/sc/config-client/target/config-client-0.0.1-SNAPSHOT.jar --server.port=7003
java -jar /Users/r/IdeaProjects/sc/config-client/target/config-client-0.0.1-SNAPSHOT.jar --server.port=7002
发现新的接口'actuator/bus-refresh':
访问 curl http://127.0.0.1:7003/fromEnv,得到 git-prod-4.0:
RdeMacBook-Pro:SpringCloud-Learning r$ curl http://127.0.0.1:7003/fromEnv
git-prod-4.0
RdeMacBook-Pro:SpringCloud-Learning r$
更改属性from并且提交到github:
再次访问curl http://127.0.0.1:7003/fromEnv,返回值没有变化。
调用刷新接口curl http://127.0.0.1:7003/actuator/bus-refresh/ -d '' -H 'content-type:application/json',
再次访问curl http://127.0.0.1:7003/fromEnv,以及curl http://127.0.0.1:7002/fromEnv,返回值变成git-prod-5.0:
RdeMacBook-Pro:SpringCloud-Learning r$ curl http://127.0.0.1:7003/fromEnv
git-prod-4.0
RdeMacBook-Pro:SpringCloud-Learning r$ curl http://127.0.0.1:7003/actuator/bus-refresh/ -d '' -H 'content-type:application/json'
RdeMacBook-Pro:SpringCloud-Learning r$ curl http://127.0.0.1:7003/fromEnv
git-prod-5.0
RdeMacBook-Pro:SpringCloud-Learning r$ curl http://127.0.0.1:7002/fromEnv
git-prod-5.0
RdeMacBook-Pro:SpringCloud-Learning r$
这样,就实现了客户端消息总线刷新配置!
观察rabbimq dashboard,发现多了几个queue,以及exchange,绑定关系如下:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!