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如下:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.3.RELEASE
		 
	
	com.sc
	config-client
	0.0.1-SNAPSHOT
	config-client
	Demo project for Spring Boot of Spring Cloud Config Client

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.springframework.cloud
			spring-cloud-starter-config
			2.0.2.RELEASE
		

		
			org.springframework.cloud
			spring-cloud-starter-eureka
			1.4.6.RELEASE
		

		
			org.springframework.boot
			spring-boot-starter-actuator
		

		
			org.springframework.cloud
			spring-cloud-starter-bus-amqp
			2.0.0.RELEASE
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.SR2
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


更改配置文件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':

spring-cloud-starter-bus-amqp利用rabbitmq消息总线实现动态刷新配置_第1张图片

访问 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:

spring-cloud-starter-bus-amqp利用rabbitmq消息总线实现动态刷新配置_第2张图片

再次访问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,绑定关系如下:

spring-cloud-starter-bus-amqp利用rabbitmq消息总线实现动态刷新配置_第3张图片

 

 

spring-cloud-starter-bus-amqp利用rabbitmq消息总线实现动态刷新配置_第4张图片

 

spring-cloud-starter-bus-amqp利用rabbitmq消息总线实现动态刷新配置_第5张图片

 

 

你可能感兴趣的:(Spring-boot,spring-cloud,rabbitmq)