SpringCloud Bus简介
Spring Cloud Bus使用轻量级消息代理连接分布式系统的各个节点,这样做可以用于系统状态变更广播(例如,配置变更)或其他管理指令。总线就像是一个Spring Boot应用程序扩展出来的分布式执行器,也可以用作应用程序之间的通信通道。引用 Spring Cloud Bus 官方文档说明:
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.
换句话说,当多个微服务的配置信息发生变更的时候,不需要我们多次调用每个服务的端点接口 actuator/refresh 来让服务获取最新的配置信息。现在只需要调用一次就可以通过消息总线广播到其他微服务来主动获取最新的配置。为了更方便的理解消息总线的作用,整体架构图如下:
如何使用SpringCloud Bus
1、项目搭建说明
- 基于JDBC的配置中心
Spring Cloud Config:基于JDBC搭建配置中心 - Spring Cloud Bus依赖消息中间件Kafka
Window下安装kafka
2、搭建过程
以 provider-demo 搭建为例,总体包结构如下图所示:
- pom文件添加 spring-cloud-starter-bus-kafka 消息总线依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>provider-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version></