当前位置 博文首页 > RtxTitanV的博客:SpringCloud 消息总线Bus Hoxton版本
Spring Cloud Bus简介:Spring Cloud Bus即消息总线通过一个轻量级的消息中间件可以连接分布式系统中的各个节点。可以使用该总线来广播某些状态比如配置信息的改变或其他管理指令。AMQP和Kafka实现包含在其中。或者,在类路径上找到的任何Spring Cloud Stream绑定器都可以作为一个现成的传输工具。
本文主要对Spring Cloud Bus的基本使用进行简单总结,其中SpringBoot使用的2.2.2.RELEASE
版本,SpringCloud使用的Hoxton.SR1
版本。这里将沿用SpringCloud 服务注册与发现Eureka Hoxton版本的eureka-server
作为注册中心,并将SpringCloud 分布式配置中心Config Hoxton版本的config-server
和config-client
的内容复制到新建项目里面,在此基础上添加消息总线实现动态刷新配置。
RabbitMQ的安装这里就不总结了,笔者这里使用的RabbitMQ容器,不清楚的可以先参考Docker常见应用部署中的RabbitMQ部署。
通过Maven新建一个名为spring-cloud-bus-server
的项目,并将config-server
的内容复制进来,在此基本上进行改造。
还需引入以下依赖:
<!-- Spring Cloud Bus Amqp 起步依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- Actuator 起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在application.yml
中进行如下配置:
server:
port: ${PORT:9600}
spring:
application:
name: bus-server
security:
# 配置spring security登录用户名和密码,给Config配置中心添加认证
user:
name: rtxtitanv
password: rtxtitanv
cloud:
config:
server:
git:
# 指定git远程仓库地址
uri: https://github.com/RtxTitanV/springcloud-config-repository.git
# 指定git仓库用户名密码,公开仓库可以不指定用户名密码,私有仓库需要指定
# 指定git仓库用户名
username: rtxtitanv
# 指定git仓库密码
password: .*********.
# 指定是否开启启动时直接从git获取配置,true:开启,false:关闭
clone-on-start: true
# 指定是否强制从远程仓库拉取,true:是,false:否,默认false
force-pull: true
# 指定配置文件被剪出或克隆到本地文件系统的存放目录
basedir: E:\software\DevelopmentTool\IntelliJ IDEA Space\my_project\springcloud-config-repository\localrep
# rabbitmq相关配置
rabbitmq:
# 指定rabbitmq的host
host: 这里可以填自己的rabbitmq服务器ip
# 指定rabbitmq的端口
port: 5672
# 指定rabbitmq的用户名
username: admin
# 指定rabbitmq的密码
password: admin
eureka:
client:
# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册
register-with-eureka: true
# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取
fetch-registry: true
# 配置Eureka注册中心即Eureka服务端的地址,集群地址以,隔开
service-url:
defaultZone: http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
instance:
# 将ip地址注册到Eureka注册中心
prefer-ip-address: true
# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ip
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒
lease-renewal-interval-in-seconds: 20
# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒
lease-expiration-duration-in-seconds: 60
management:
endpoints:
web:
exposure:
# 暴露指定端点,bus-refresh为bus刷新配置端点
include: 'bus-refresh'
通过Maven新建一个名为spring-cloud-bus-client
的项目,并将config-client
的内容复制进来,在此基本上进行改造。
还需引入以下依赖:
<!-- Spring Cloud Bus Amqp 起步依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
在bootstrap.yml
里进行如下配置:
server:
port: ${PORT:9700}
spring:
application:
name: bus-client
cloud:
config:
discovery:
# 指定是否启用服务发现访问配置中心服务端,true:启用,false:不启用
enabled: true
# 指定分布式配置中心服务名称
service-id: bus-server
# 指定连接配置中心服务端的用户名
username: rtxtitanv
# 指定连接配置中心服务端的密码
password: rtxtitanv
# 指定要获取的配置文件的前缀名,对应配置文件中的{application}
name: application
# 指定要获取的配置文件的环境名,对应配置文件中{profile}
profile: dev
# 指定要获取的配置文件的分支名,对应{label},默认为master
label: master
# 指定是否开启快速失败响应,true:开启,false:关闭
fail-fast: true
# config客户端重试配置
retry:
# 指定最大重试次数,默认6
max-attempts: 8
# 指定最大间隔时间,单位ms,默认2000
max-interval: 1200
# 指定间隔乘数,默认1.1
multiplier: 1.2
# 指定初始重试间隔时间,单位ms,默认1000
initial-interval: 800
# rabbitmq相关配置
rabbitmq:
# 指定rabbitmq的host
host: 这里可以填自己的rabbitmq服务器ip
# 指定rabbitmq的端口
port: 5672
# 指定rabbitmq的用户名
username: admin
# 指定rabbitmq的密码
password: admin
eureka:
client:
# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册
register-with-eureka: true
# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取
fetch-registry: true
# 配置Eureka注册中心即Eureka服务端的地址,集群地址以,隔开
service-url:
defaultZone: http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
instance:
# 将ip地址注册到Eureka注册中心
prefer-ip-address: true
# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ip
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒
lease-renewal-interval-in-seconds: 20
# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒
lease-expiration-duration-in-seconds: 60
management:
endpoints:
web:
exposure:
# 暴露指定端点,bus-refresh为bus刷新配置端点
include: 'bus-refresh'
如果启动时出现An unexpected connection driver error occured错误,是由于RabbitMQ用户没有读写等操作的权限,需要按照下列图中操作设置用户权限,Virtual Host最好设置为斜杠:
IDEA启动eureka-server
集群,bus-server
和bus-client
集群,访问注册中心,下图为服务注册信息:
访问RabbitMQ管理页面,,下面动图为访问结果,可以发现Spring Cloud Bus创建了三个连接,一个名为springCloudBus的交换机以及三个以 springCloudBus.anonymous开头的队列:
在Config配置中心服务加入了SpringSecurity安全认证后,POST请求访问/actuator/bus-refresh
端点异常,然后再次访问客户端接口也刷新不了配置,这时需自定义一个继承WebSecurityConfigAdapter的子类:
package com.rtxtitanv.config;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.config.WebSecurityConfig
* @description WebSecurityConfig
* @date 2020/3/13 20:55
*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
// 关闭security的csrf检验
http.csrf().disable();
// 为了可以使用http://${user}:${password}@${host}:${port}/eureka/这种方式登录,所以必须是httpBasic,
// 如果是form方式,不能使用url格式登录
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
分别访问http://localhost:9701/getConfig和http://localhost:9702/getConfig,结果见下面动图:
修改application-dev.yml
配置文件,具体修改见下图:
然后再次访问http://localhost:9701/getConfig和http://localhost:9702/getConfig发现没变化,POST请求访问http://localhost:9701/actuator/bus-refresh后再重试之前的两个链接,根据下面动图的测试过程和结果,说明动态刷新配置成功。
上面这种向具体服务中的某个实例发送请求,再触发对整个服务集群的动态配置更新,虽然能实现功能,但是指定的实例会不同于集群中的其他应用实例,会增加集群内部的复杂度,不利于后期维护。所以给Config服务端也添加了消息总线支持,这样只需将/actuator/bus-refresh
请求直接发到配置中心,这样刷新配置全交给配置中心,普通客户端不需要再触发刷新配置,减少了一些不必要的维护工作。下面再次修改application-dev.yml
配置文件,具体修改见下图:
然后再次进行动态配置刷新测试,这次POST请求访问http://localhost:9600/actuator/bus-refresh,根据下面动图的测试过程和结果,,说明动态刷新配置成功。
如果只需要刷新指定实例的配置按以http://localhost:9600/actuator/bus-refresh/{destination}
格式发送POST请求,下面将application-dev.yml
内容改为master-config-dev-bus-refresh-3,然后发送POST请求http://localhost:9600/actuator/bus-refresh/bus-client:9701只刷新bus-client:9701
实例,根据下面动图的测试过程和结果,说明动态刷新指定实例配置成功。
在仓库中设置WebHooks,在配置发生变更时WebHooks会自动发送POST请求触发动态刷新配置,下面的URL应设置为能被外部访问的配置中心的地址,不能为localhost,有兴趣的可以测试一下。
Kafka的安装这里就不总结了,笔者是在阿里云上安装的,有不清楚的可以参考阿里云部署Kafka。