十一:Spring Cloud 之消息总线-
文章目录
- 1. 简介
- 2. 代码实现
- 2.1 涉及的模块及整体步骤
- 2.2 源代码
- 2.3 eureka-server-singleton
- 2.4 config-server-ha
- 2.5 config-client-bus
- 2.6 config-repository
- 3. 验证
- 3.1 创建SpringBoot启动类
- 3.1.1 EurekaServerSingletonApplication
- 3.1.2 ConfigServerHaApplication-8772
- 3.1.3 ConfigServerHaApplication-8773
- 3.1.4 ConfigClientBusApplication-8775
- 3.1.5 ConfigClientBusApplication-8776
- 3.2 启动
- 3.3查看eureka服务信息界面
- 3.4 读取远程配置信息
- 4. 思考
- 5. 补充
1. 简介
Spring Cloud Bus links the nodes of a distributed system with a lightweight message broker. This broker can then be used to broadcast state changes (such as configuration changes) or other management instructions. A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out. However, it can also be used as a communication channel between apps. This project provides starters for either an AMQP broker or Kafka as the transport.
事件、消息总线,用于在集群(例如,配置变化事件)中传播状态变化,可与Spring Cloud Config联合实现热部署。
基于RabbitMQ实现上一篇笔记中的配置手动刷新,RabbitMQ的安装请自行搜索,本片记录中的RabbitMQ使用的都是默认配置。
2. 代码实现
2.1 涉及的模块及整体步骤
2.1.1 涉及的模块
- eureka-server-singleton:eureka服务发布注册中心
- config-server-ha:配置中心服务端,通过指定不同端口启动两个实例模拟服务端集群
- config-client-bus:新建的通过HA版服务配置中心访问远程配置信息模块,也可以使用原有模块
- config-repository:放置于GitHub的配置,
config-client-bus-test.properties
是对应的本次测试的保存配置信息的文件名称
2.1.2 整体步骤
- GitHub创建存放配置信息的config-repository目录与配置信息
config-client-bus-test.properties
,可通过demo中的config-repository
模块关联GitHub上的配置。 - 实现eureka-server-singleton:eureka服务发布注册中心,与前面没有任何区别
- 实现config-server-ha:关键是启动Spring Cloud Config Server功能,指定配置仓库的配置信息
- 实现config-client-bus:从配置仓库读取配置信息,引入spring-boot-starter-actuator,spring-cloud-starter-bus-amqp
- 通过为config-client-bus指定8775、8776端口实现多实例启动
- 修改Github上远程配置文件config-client-bus-test.properties,通过8775或者8776刷新配置,再次访问两个实例读取的相同配置项的值,观察修改是否生效
- config-repository中添加
config-client-bus-test.properties
:放置4步骤的配置信息
2.2 源代码
2.2.1 Github地址
https://github.com/andyChenHuaYing/spring-cloud-demo
2.2.2 配置信息地址
配置信息在dev-20180827、与master分支都有,但是代码中使用的是dev-20180827分支的代码,如果想修改值验证,记得确认客户端bootstrap.properties中配置信息与想要访问的仓库地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository
2.3 eureka-server-singleton
与Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别
2.4 config-server-ha
与十:Spring Cloud 之配置中心HA版-config 没有任何区别
2.5 config-client-bus
2.5.1 整体实现
- pom.xml文件中引入依赖
spring-cloud-starter-config
、spring-cloud-starter-netflix-eureka-client
、config-client-bus-test.properties
、spring-boot-starter-actuator
- bootstrap.yml中指定Config Server连接信息以及需要访问配置中心的具体配置信息
- application-8775.yml、application-8776.yml指定当前模块的配置信息,通过profile指定不同端口模拟启动多实例
- ConfigClientBusApplication常规Spring Boot启动类
2.5.2 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">
<parent>
<artifactId>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client-bus</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-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-actuator</artifactId>
</dependency>
</dependencies>
</project>
2.5.3 bootstrap.yml
下方信息与配置中心的配置文件的对应关系见后续验证部分
spring:
application:
name: config-client-bus
cloud:
config:
label: dev-20180827
profile: test
discovery:
enabled: true
serviceId: config-server-ha
2.5.4 application-8775.yml
application-8776.yml 只是端口不一样
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
enabled: true
trace:
enabled: true
management:
endpoints:
web:
exposure:
include: bus-refresh
server:
port: 8775
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.6 config-repository
保存配置文件
2.6.1 config-client-bus-test.properties配置文件
foo=config-client-bus foo value updated.
3. 验证
3.1 创建SpringBoot启动类
简单创建Spring Boot启动类即可
3.1.1 EurekaServerSingletonApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
3.1.2 ConfigServerHaApplication-8772
3.1.3 ConfigServerHaApplication-8773
参考上一节,修改Active profiles:8773
3.1.4 ConfigClientBusApplication-8775
参考上一节,指定Active profiles:8775。
3.1.5 ConfigClientBusApplication-8776
参考上一节,指定Active profiles:8776。
3.2 启动
EurekaServerSingletonApplication
ConfigServerHaApplication-8772
ConfigServerHaApplication-8773
ConfigClientBusApplication-8775
ConfigClientBusApplication-8776
3.3查看eureka服务信息界面
3.4 读取远程配置信息
3.4.1 查看指定配置项的值
- 浏览器中输入地址:http://localhost:8775/readFooProp
- 预期:读取的是:https://github.com/andyChenHuaYing/spring-cloud-demo/blob/dev-20180827/config-repository/config-client-bus-test.properties 文件中
foo=config-client-bus foo value updated.
- 实际返回结果:
3.4.2 查看指定配置项修改后的值
-
本地修改资源文件
config-client-bus-test.properties
配置项foo=config-client-bus foo value
,并push到远程仓库,查看 https://github.com/andyChenHuaYing/spring-cloud-demo/blob/dev-20180827/config-repository/config-client-bus-test.properties 有没有成功(这里可以换成自己的Github测试一下)修改后的值foo=config-client-bus foo value updated again 2018-09-20.
-
再次访问 http://localhost:8775/readFooProp,返回值不变,值为:
config-client-bus foo value updated.
-
访问http://localhost:8775/actuator/bus-refresh 手动刷新配置项,可看到后台重新请求Github地址,拉取配置。注意:这里需要使用post请求,并且header中的Content-Type值为application/json。可以使用Intellij 的HTTP Client。
-
观察8775控制台输出的日子,发现重新拉取了远程的配置信息.
-
再次访问 http://localhost:8775/readFooProp,返回值变成修改后的值
config-client-bus foo value
,说明成功读取到新配置值再次访问 http://localhost:8775/readFooProp,返回值变成修改后的值config-client-bus foo value
,说明成功读取到新配置值 -
访问http://localhost:8776/readFooProp,返回值变成修改后的值
config-client-bus foo value
,说明成功读取到新配置值 -
通过消息总线实现变更配置信息同步功能生效
4. 思考
- 消息总线是如何接收消息并广播的
- SpringCloud消息总线还支持哪些消息中间件
- 如果基于消息中线设计一套基于事件驱动的系统架构,需要解决哪些核心关键点
5. 补充
5.1 资料
https://springcloud.cc/spring-cloud-bus.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_bus.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html