1.概述
衔接上一讲解的SpringCloud Config 的加深和扩充分布式自动刷新配置功能Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新是什么Bus支持两种消息代理:RabbitMQ和Kafka能干嘛为何被称为总线
2.RabbitMQ环境配置
下面是win10,上面安装rabbitMq的步骤 直接下一步即可,安装在javaTools文件夹下面安装Erlang,下载地址:http://erlang.org/download/otp_win64_21.3.exe 安装RabbitMQ,下载地址https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe先安装erlang,直接安装下一步即可安装rabbitMq,直接下一步即可安装完成,开始界面有很多东西
安装可视化插件进入sbin下面cmd
rabbitmq-plugins enable rabbitmq_management
访问测试,输入账号密码并登录: guest guesthttp://localhost:15672/3.SpringCloud Bus动态刷新全局广播
必须先具备良好的RabbitMQ环境先演示广播效果,增加复杂度,再以3355为模板再制作一个3377新建cloud-config-client-3377
<?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>springcloud2020</artifactId>
<groupId>com.chl.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3377</artifactId>
<dependencies>
<!--config 客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--mq依赖用于动态刷新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.chl.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
ymlserver:
port: 3377
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
主启动@SpringBootApplication
public class ConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3377.class,args);
}
}
业务类@RestController
@RefreshScope //用于刷新分布式的配置
//可能需要服务中心先请求一次,而且还要运维人员发起一个请求去刷新相应的客户端
//例如运维人员改了,还要刷新客户端3355 curl -X POST "http://localhost:3355/actuator/refresh
public class ConfigClientController {
//获取统一配置的数据信息
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
设计思想
架构1:利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置架构2:利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置(更加推荐)图二的架构显然更加合适,图一不适合的原因如下
- 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新职责
- 破坏了微服务各节点的对等性
- 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改
给配置中心添加总线支持
给cloud-config-center-3344配置中心服务端添加消息总线支持pom<!--mq依赖用于动态刷新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml 添加rabbitMq的配置server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config: #分支/config-环境名.yml
server: #http://config-3344.com:3344/master/config-dev.yml
git: #githud仓库的位置 用码云不行,识别不了,只能githud
uri: https://github.com/badbad001/springcloud-config.git
search-paths:
- springcloud-config #仓库名称
label: master #仓库的分支
rabbitmq: #rabbit mq的 配置
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#暴露监控端口,用于刷新分布式的配置
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
给客户端添加总线支持
给cloud-config-center-3355客户端添加消息总线支持pom<!--mq依赖用于动态刷新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml 添加rabbitMq的配置#这个文件名 bootrap.yml 系统级别的 优先于 application.yml(用户界别)
server:
port: 3355
spring:
application:
name: config-client
cloud:
config: #组装成就是 http://localhost:3344/master/config-dev.yml 配置的是从哪里获取配置信息
uri: http://localhost:3344
label: master
name: config
profile: dev
rabbitmq: #rabbit mq的 配置
host: localhost
port: 5672 #本地端口这里有问题, 访问是这个http://localhost:15672,但是有端口冲突,所以链接额不用15672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
#暴露监控端口,用于刷新分布式的配置
management:
endpoints:
web:
exposure:
include: "*"
给cloud-config-center-3377客户端添加消息总线支持pom
<!--mq依赖用于动态刷新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml 添加rabbitMq的配置server:
port: 3377
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
测试
修改Github上配置文件增加版本号运维工程师 发送Post请求curl -X POST "http://localhost:3344/actuator/bus-refresh"
一次发送,处处生效配置中心客户端
获取配置信息,发现都已经刷新了 一次修改,广播通知,处处生效
4.SpringCloud Bus动态刷新定点通知
不想全部通知,只想定点通知- 只通知3355
- 不通知3377
案例说明我们这里以刷新运行在3355端口上的config-client为例
- 只通知3355
- 不通知3366
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
总结