Spring Cloud -Hoxton.RELEASE(五):配置中心-Bus

Spring Cloud Bus

Bus的官方文档对Bus描述如下:

Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can then be used to broadcast state changes (e.g. configuration changes) or other management instructions. The only implementation currently is with an AMQP broker as the transport, but the same basic feature set (and some more depending on the transport) is on the roadmap for other transports.

Spring Cloud Bus使用轻量级消息代理链接分布式系统的节点。然后,这可以用于广播状态改变(例如,配置改变)或其他管理指令。目前唯一的实现是使用AMQP代理作为传输,但是相同的基本功能集(以及一些更多取决于传输)是其他传输的路线图。

简单来讲官方对Bus的定位是消息传输
本doom利用RabbitMQ来实现消息总线的功能。

添加Bus依赖

config项目添加Bus依赖

config项目添加spring-cloud-starter-bus-amqp依赖:

<?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>com.github.liuzhuoming23</groupId>
<artifactId>config-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>

<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config</name>
<description>Demo project for Spring Cloud</description>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

其中spring-cloud-starter-stream-rabbit依赖为指定实现消息总线的方式为rabbitmq
application.yml配置修改为:

server:
port: 8040

spring:
application:
name: @pom.artifactId@
cloud:
config:
server:
git:
uri: https://gitee.com/liuzhuoming23/config-repo
search-paths: config
username:
password:
bus:
#开启消息总线功能
enabled: true
rabbitmq:
#rabbitmq host
host: localhost
#rabbitmq port
port: 5672
#rabbitmq 帐号
username: guest
#rabbitmq 密码
password: guest

eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8000/eureka/

management:
endpoints:
web:
exposure:
#配置监控endpoint,其中bus-refresh为根据消息总线刷新配置的endpoint
include: bus-refresh,httptrace,info,health

然后重启config项目,访问rabbitmq后台管理页面http://localhost:15672,进入Exchanges选项卡,可以看到Exchanges里面已经增加了名为springCloudBus的topic:
01.jpg

producer-client项目添加Bus依赖

producer-client项目pom.xml修改为:

<?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>com.github.liuzhuoming23</groupId>
<artifactId>server-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>

<artifactId>producer-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>producer-client</name>
<description>Demo project for Spring Cloud</description>

<dependencies>
<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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

application.yml修改为:

server:
port: 8010

spring:
application:
name: @pom.artifactId@
cloud:
bus:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

management:
endpoints:
web:
exposure:
include: refresh,info,health

然后重启8010和8011两个项目,访问rabbitmq后台管理页面http://localhost:15672,进入Exchanges选项卡,打开springCloudBus详情,可以看到springCloudBus主题下绑定了三个实例,其中一个为发布者,另外两个为订阅者(不过我没分辨出哪个是哪个):
02.jpg

根据Bus刷新全部producer-client实例的动态配置

修改git里面的config/producer-client-dev.yml文件为:

config:
name: 国际巨星🍺xj

使用Postman用POST请求访问http://localhost:8040/actuator/bus-refresh/,然后分别访问http://localhost:8010/port/testhttp://localhost:8011/port/test,返回结果都是(因为根据消息总线刷新配置过程稍微久一点,所以在刷新之后个别实例配置并没有更新过来也是正常的,打印的端口都是8010的问题参照上一章):
国际巨星🍺xj | test: client port | 8010
说明根据消息总线刷新全部实例成功。
在刷新配置的时候访问rabbitmq后台管理页面http://localhost:15672,进入Exchanges选项卡,打开springCloudBus详情,会看到已经产生了消息数据的传递:
03.jpg

根据Bus刷新局部producer-client实例的动态配置

修改git里面的config/producer-client-dev.yml文件为:

config:
name: 战术后仰

使用Postman用POST请求访问http://localhost:8040/actuator/bus-refresh/producer-client:8010,然后访问http://localhost:8010/port/test,返回结果:
战术后仰 | 🐕: client port | 8010 | feign
看到8010实例的配置已经改变,然后访问http://localhost:8011/port/test,返回结果:
国际巨星🍺xj | 🐕: client port | 8010 | feign
看到8011实例的配置并未改变,也就证明局部刷新配置成功。