本文我们来介绍下SpringCloud中消息总线BUS。

一、什么是消息总线bus

  SpringCloud Bus集成了市面上常用的消息中间件(rabbit mq,kafka等),连接微服务系统中的所有的节点,当有数据变更的时候,可以通过消息代理广播通知微服务及时变更数据,例如微服务的配置更新。

二、bus解决了什么问题?

  解决了微服务数据变更,及时同步的问题。

刷新客户端服务

SpringCloud-消息总线BUS_maven

刷新服务端服务

SpringCloud-消息总线BUS_maven_02

案例前提需要启动RabbitMQ服务,不太清楚的可参考此文

三、bus-客户端刷新

1.创建项目

SpringCloud-消息总线BUS_bus_03

2.pom文件

<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>1.5.13.RELEASE</version>
</parent>
<groupId>com.bobo</groupId>
<artifactId>config-client-refresh-bus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</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>
</dependency>

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

3.配置文件

spring.application.name=config-client
server.port=9051
#设置服务注册中心地址,指向另一个注册中心
eureka.client.serviceUrl.defaultZone=http://dpb:
#默认 false,这里设置 true,表示开启读取配置中心的配置
spring.cloud.config.discovery.enabled=true
#对应 eureka 中的配置中心 serviceId,默认是 configserver
spring.cloud.config.discovery.serviceId=config-server
#指定环境
spring.cloud.config.profile=dev
#git 标签
spring.cloud.config.label=master

management.security.enabled=false

# 消息队列RabbitMQ的连接配置信息
spring.rabbitmq.host=192.168.88.150
spring.rabbitmq.port=5672
spring.rabbitmq.username=dpb
spring.rabbitmq.password=123
spring.rabbitmq.virtualHost=/

注意:controller的作用域要调整

SpringCloud-消息总线BUS_SpringCloud_04

4.再创建一个客户端项目

SpringCloud-消息总线BUS_bus_05

5.测试

分别启动config-server和两个服务端,然后获取git中的属性信息

SpringCloud-消息总线BUS_bus_06


SpringCloud-消息总线BUS_SpringCloud_07

显示修改git中的属性值

SpringCloud-消息总线BUS_maven_08

现在在客户端获取肯定是没有改变的,这时我们可以通过RestTemplate发送post请求来刷新客户端。比如我们刷新5051的服务

SpringCloud-消息总线BUS_maven_09

public static void main(String[] args) {
RestTemplate template = new RestTemplate();
String url = "http://localhost:9051/bus/refresh";
template.postForLocation(url, null);
System.out.println("刷新客户端...");
}

然后再访问:

SpringCloud-消息总线BUS_SpringCloud_10


SpringCloud-消息总线BUS_spring_11

说明同步成功!同时在RabbitMQ中也可以看到队列信息

SpringCloud-消息总线BUS_maven_12

四、bus-服务端刷新

  接下来看下直接刷新config-server服务来通过bus及时通知client的效果。

1.创建项目

SpringCloud-消息总线BUS_SpringCloud_13

2.pom

<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>1.5.13.RELEASE</version>
</parent>
<groupId>com.bobo</groupId>
<artifactId>config-server-bus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

3.配置文件

spring.application.name=config-server
server.port=9050
#设置服务注册中心地址,指向另一个注册中心
eureka.client.serviceUrl.defaultZone=http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/

#Git 配置
spring.cloud.config.server.git.uri=https://gitee.com/dengpbs/config
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=

management.security.enabled=false

spring.rabbitmq.host=192.168.88.150
spring.rabbitmq.port=5672
spring.rabbitmq.username=dpb
spring.rabbitmq.password=123
spring.rabbitmq.virtualHost=/

4.测试

  分别启动config-server和两个客户端。

SpringCloud-消息总线BUS_bus_14

SpringCloud-消息总线BUS_maven_15

git修改属性

SpringCloud-消息总线BUS_bus_16

通过RestTemplate显示的refresh

public static void main(String[] args) {
RestTemplate template = new RestTemplate();
String url = "http://localhost:9050/bus/refresh";
template.postForLocation(url, null);
System.out.println("刷新客户端...");
}

然后在访问:

SpringCloud-消息总线BUS_bus_17

SpringCloud-消息总线BUS_maven_18

五、局部刷新服务

  参考下图,我们上面介绍的两种bus刷新的案例中,只要refresh了服务,那么订阅的有RabbitMQ服务的所有的服务都会刷新一遍配置信息,那这样就会造成一种情况,比如,我们仅仅修改了影响Server-A 服务的配置信息,但是一刷新,Service-B和Service-C服务都要刷新配置信息,显然这样是不行的。这时我们就可以通过局部刷新来实现刷新部分服务的需求。

SpringCloud-消息总线BUS_maven_02

1.刷新指定服务

  刷新服务的时候我们可以指定刷新的服务信息,具体实现如下:

http://config-server/bus/refresh?destination=需要刷新的服务名称:端口

2.刷新指定集群服务

  如果同一个服务有集群部署的场景,我们希望刷新服务的时候,集群的服务一并都刷新,提交的信息只需要端口通配就行

http://config-server/bus/refresh?destination=需要刷新的服务名称:**