spring-cloud-starter-bus-amqp利用rabbitmq消息总线实现动态刷新配置 - Go语言中文社区

spring-cloud-starter-bus-amqp利用rabbitmq消息总线实现动态刷新配置


对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,绑定关系如下:

 

 

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/sijun1102/article/details/89300383
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-05-07 22:07:34
  • 阅读 ( 2473 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢