如需转载,请根据 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 许可,附上本文作者及链接。
本文作者: 执笔成念
作者昵称: zbcn
本文链接: https://1363653611.github.io/zbcn.github.io/2021/01/09/springcloud-09bus%E6%B6%88%E6%81%AF%E6%80%BB%E7%BA%BF/
Spring Cloud Bus:消息总线
Spring Cloud Bus 使用轻量级的消息代理来连接微服务架构中的各个服务,可以将其用于广播状态更改(例如配置中心配置更改)或其他管理指令,本文将对其用法进行详细介绍。
Spring Cloud Bus 简介
我们通常会使用消息代理来构建一个主题,然后把微服务架构中的所有服务都连接到这个主题上去,当我们向该主题发送消息时,所有订阅该主题的服务都会收到消息并进行消费。使用 Spring Cloud Bus 可以方便地构建起这套机制,所以 Spring Cloud Bus 又被称为消息总线。Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新。目前 Spring Cloud Bus 支持两种消息代理:RabbitMQ 和 Kafka,下面以 RabbitMQ 为例来演示下使用Spring Cloud Bus 动态刷新配置的功能。
RabbitMq 服务安装
略
启动:
浏览器访问: http://127.0.0.1:15672/
动态刷新配置
使用 Spring Cloud Bus 动态刷新配置需要配合 Spring Cloud Config 一起使用,我们使用之前创建的config-server、config-client模块来演示下该功能。
给config-server添加消息总线支持
- 在pom.xml 中添加 依赖
1 | <dependency> |
2 | <groupId>org.springframework.cloud</groupId> |
3 | <artifactId>spring-cloud-starter-bus-amqp</artifactId> |
4 | </dependency> |
5 | <dependency> |
6 | <groupId>org.springframework.boot</groupId> |
7 | <artifactId>spring-boot-starter-actuator</artifactId> |
8 | </dependency> |
- 添加配置文件application-amqp.yml,主要是添加了RabbitMQ的配置及暴露了刷新配置的Actuator端点;
1 | server: |
2 | port: 8904 |
3 | spring: |
4 | application: |
5 | name: config-server |
6 | cloud: |
7 | config: |
8 | server: |
9 | git: |
10 | uri: https://github.com/1363653611/config-repo.git |
11 | username: xxxx |
12 | password: xxxx |
13 | clone-on-start: true # 开启启动时直接从git获取配置 |
14 | search-paths: '{application}' |
15 | rabbitmq: |
16 | host: localhost |
17 | port: 5672 |
18 | username: guest |
19 | password: guest |
20 | virtual-host: / |
21 | |
22 | eureka: |
23 | client: |
24 | service-url: |
25 | defaultZone: http://localhost:8000/eureka/ |
26 | # 必须加,否则 其他客户端无法访问改 实例:No instances found of configserver (config-server) |
27 | register-with-eureka: true |
28 | fetch-registry: true |
29 | management: |
30 | endpoints: #暴露bus刷新配置的端点 |
31 | web: |
32 | exposure: |
33 | include: 'bus-refresh' |
修改配置:program arguments
1 | --spring.config.location=classpath:application-amqp.yml |
服务启动报错
1 | ERROR 16640 --- [:0:0:0:0:1:5672] c.r.c.impl.ForgivingExceptionHandler : An unexpected connection driver error occured |
原因: 未配置 virtual-host
1 | rabbitmq: #rabbitmq相关配置 |
2 | host: localhost |
3 | port: 5672 |
4 | username: guest |
5 | password: guest |
6 | virtual-host: / |
给config-client添加消息总线支持
- 在pom.xml中添加相关依赖:
1 | <dependency> |
2 | <groupId>org.springframework.cloud</groupId> |
3 | <artifactId>spring-cloud-starter-bus-amqp</artifactId> |
4 | </dependency> |
- 添加配置文件bootstrap-amqp1.yml及bootstrap-amqp2.yml用于启动两个不同的config-client,两个配置文件只有端口号不同;
1 | server: |
2 | port: 9104 |
3 | spring: |
4 | application: |
5 | name: config-client |
6 | cloud: |
7 | config: |
8 | profile: dev #启用环境名称 |
9 | label: dev #分支名称 |
10 | name: config #配置文件名称 |
11 | discovery: |
12 | enabled: true |
13 | service-id: config-server |
14 | rabbitmq: #rabbitmq相关配置 |
15 | host: localhost |
16 | port: 5672 |
17 | username: guest |
18 | password: guest |
19 | eureka: |
20 | client: |
21 | service-url: |
22 | defaultZone: http://localhost:8000/eureka/ |
23 | management: |
24 | endpoints: |
25 | web: |
26 | exposure: |
27 | include: 'refresh' |
修改配置 :program arguments
1
--spring.config.location=classpath:bootstrap-amqp1.yml
2
--spring.config.location=classpath:bootstrap-amqp2.yml
动态刷新配置演示
- 我们先启动相关服务,启动eureka-server,以application-amqp.yml为配置启动config-server,以bootstrap-amqp1.yml为配置启动config-client,以bootstrap-amqp2.yml为配置再启动一个config-client,启动后注册中心显示如下:
启动所有服务后,我们登录RabbitMQ的控制台可以发现Spring Cloud Bus 创建了一个叫springcloudBus的交换机及三个以 springcloudBus.anonymous开头的队列:
我们先修改Git仓库中dev分支下的config-dev.yml配置文件:
1 | # 修改前信息 |
2 | config: |
3 | info: "config info for dev(dev)" |
4 | # 修改后信息 |
5 | config: |
6 | info: "update config info for dev(dev)" |
调用注册中心的接口刷新所有配置(注意为post 请求):http://localhost:8904/actuator/bus-refresh
刷新后再分别调用http://localhost:9104/config/configInfo 和 http://localhost:9105/config/configInfo 获取配置信息,发现都已经刷新了;
如果只需要刷新指定实例的配置可以使用以下格式进行刷新:http://localhost:8904/actuator/bus-refresh/{destination} ,我们这里以刷新运行在9104端口上的config-client为例http://localhost:8904/actuator/bus-refresh/config-client:9004。
使用到的模块
1 | ZBCN-SERVER |
2 | ├── zbcn-register/eureka-server -- eureka注册中心 |
3 | ├── zbcn-config/ config-client -- 获取配置的客户端服务 |
4 | └── zbcn-config/ config-server -- 配置中心服务 |