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,綁定關係如下:

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章