SpringCloud 2020.0.4 系列之 Bus
1. 概述
老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了。
言归正传,之前我们聊了 SpringCloud 的 分布式配置中心 Config,文章里我们聊了config配置的动态刷新,但这个动态刷新,一次只能刷新一个 Config Client 节点,如果服务节点少还好,如果业务多了,有成百上千个服务节点,再一台一台刷新,就不合适了。
因此,今天我们来聊聊 SpringCloud的消息总线组件 Bus,Bus 组件可以借助MQ中间件,向所有的 Config Client 发送刷新广播,只需要调用一个接口,就可以刷新整个集群的Config 配置。
我们使用 RabbitMQ 作为中间件,关于 RabbitMQ 集群的搭建,可参见我的另一篇文章《RabbitMQ 3.9.7 镜像模式集群的搭建》(https://www.cnblogs.com/w84422/p/15356202.html)。
闲话不多说,直接上代码。
2. Git 准备
1)创建一个 repository,用于放置配置文件,例如:my-config-repo
2)创建 my-bus-client 文件夹
3)在 my-bus-client 文件夹下,新建 my-bus-client-dev.yml 配置文件,内容如下:
info:
profile: dev name: zhuifengren
desc: hello dev
3. Config Server 引入 Bus 组件
3.1 主要依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-bootstrap</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3.2 application.yml 配置
server:
port: 42000 spring:
application:
name: my-bus-server
rabbitmq:
addresses: 192.168.1.22:5672,192.168.1.12:5672,192.168.1.11:5672 # rabbitmq 集群的地址
username: guest
password: guest
virtual-host: /
connection-timeout: 16000
cloud:
config:
server:
git:
uri: https://github.com/w84422/my-config-repo.git # git地址
force-pull: true # 强制拉取资源文件
default-label: main # 默认拉取的分支
search-paths: '{application}' # 将配置文件放到接入方服务名称对应的文件夹下,这种写法,只在config组件设置生效 management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always eureka:
client:
service-url:
defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/ # Eureka Server的地址
3.3 启动类增加注解
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class MyBusServerApplication { public static void main(String[] args) {
SpringApplication.run(MyBusServerApplication.class, args);
}
}
3.4 启动 Config Server
启动 Config Server
4. Config Client 引入 Bus 组件
4.1 主要依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- rabbitmq -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
4.2 bootstrap.yml 配置
server:
port: 43000 spring:
application:
name: my-bus-client
rabbitmq:
addresses: 192.168.1.22:5672,192.168.1.12:5672,192.168.1.11:5672
username: guest
password: guest
virtual-host: /
connection-timeout: 16000
cloud:
config:
profile: dev # 拉取的配置文件
label: main # 拉取的分支
name: my-bus-client # 指定远程获取配置文件的 application,默认会取 spring.application.name
discovery:
enabled: true
service-id: my-bus-server # config服务的服务名称 myDesc: ${desc} eureka:
client:
service-url:
defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/ # Eureka Server的地址 management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
4.3 启动类增加注解
@SpringBootApplication
@EnableDiscoveryClient
public class MyBusClientApplication { public static void main(String[] args) {
SpringApplication.run(MyBusClientApplication.class, args);
}
}
4.4 增加用于实验的 Controller 类
@RefreshScope
@RestController
public class MyBusClientRefreshController { @Value("${info.profile}")
private String profile;
@Value("${name}")
private String name;
@Value("${myDesc}")
private String desc; @RequestMapping("/refresh-info")
public String getInfo() { String info = "profile:" + profile + "<br>";
info += "name:" + name + "<br>";
info += "desc:" + desc + "<br>"; return info;
}
}
4.5 启动 Config Client 服务,并进行验证
4.5.1 启动 Config Client 服务
启动两个或多个 Config Client 服务
4.5.2 访问 Controller 接口,获取配置信息
GET http://localhost:43000/refresh-info

4.5.3 修改 Git 上的配置
将 name 改为 zhangsan
4.5.4 执行bus配置刷新接口
在 Config Server 或 任意 Config Client 节点,执行 bus 配置刷新接口
POST http://localhost:42000/actuator/busrefresh
4.5.5 在所有 Config Client 节点调用 Controller 接口,重新获取信息
GET http://localhost:43000/refresh-info

5. 综述
今天聊了一下 SpringCloud 的 Bus 组件,希望可以对大家的工作有所帮助。
欢迎帮忙点赞、评论、转发、加关注 :)
关注追风人聊Java,每天更新Java干货。
6. 个人公众号
追风人聊Java,欢迎大家关注

SpringCloud 2020.0.4 系列之 Bus的更多相关文章
- SpringCloud 2020.0.4 系列之 Feign
1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解. 言归正传,之前我们聊了 SpringCloud 的服务治理组件 Eureka,今天我 ...
- SpringCloud 2020.0.4 系列之 Stream 消息广播 与 消息分组 的实现
1. 概述 老话说的好:事情太多,做不过来,就先把事情记在本子上,然后理清思路.排好优先级,一件一件的去完成. 言归正传,今天我们来聊一下 SpringCloud 的 Stream 组件,Spring ...
- SpringCloud 2020.0.4 系列之 Stream 延迟消息 的实现
1. 概述 老话说的好:对待工作要有责任心,不仅要完成自己的部分,还要定期了解整体的进展. 言归正传,我们在开发产品时,常常会遇到一段时间后检查状态的场景,例如:用户下单场景,如果订单生成30分钟后, ...
- SpringCloud 2020.0.4 系列之 Stream 消息出错重试 与 死信队列 的实现
1. 概述 老话说的好:出错不怕,怕的是出了错,却不去改正.如果屡次出错,无法改对,就先记下了,然后找援军解决. 言归正传,今天来聊一下 Stream 组件的 出错重试 和 死信队列. RabbitM ...
- SpringCloud 2020.0.4 系列之Eureka
1. 概述 老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气.抱怨或生气. 言归正传,微服务是当今非常流行的一种架构方式,其中 SpringCloud 是我们常用的一种微服务框架. ...
- SpringCloud 2020.0.4 系列之服务降级
1. 概述 老话说的好:做人要正直,做事要正派,胸怀坦荡.光明磊落,才会赢得他人的信赖与尊敬. 言归正传,之前聊了服务间通信的组件 Feign,今天我们来聊聊服务降级. 服务降级简单的理解就是给一个备 ...
- SpringCloud 2020.0.4 系列之 Gateway入门
1. 概述 老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心. 言归正传,今天我们来聊聊 SpringCloud 的网关组件 Gateway,之前我们去访问 SpringCloud 不同服务的接 ...
- SpringCloud 2020.0.4 系列之 JWT用户鉴权
1. 概述 老话说的好:善待他人就是善待自己,虽然可能有所付出,但也能得到应有的收获. 言归正传,之前我们聊了 Gateway 组件,今天来聊一下如何使用 JWT 技术给用户授权,以及如果在 Gate ...
- SpringCloud 2020.0.4 系列之服务降级的其他用法与熔断
1. 概述 老话说的好:控制好自己的情绪,才能控制好自己的人生.冲动是魔鬼,冷静才最重要. 言归正传,之前聊了在 Feign 调用时,如何给整个 Feign接口类 增加降级策略. 今天我们来聊一下 H ...
随机推荐
- Linux系列(34) - yum源文件(1)
yum源文件各参数含义 在[/etc/yum.repos.d/]目录中,默认有4个yum源文件,其中[CentOS-Linux-BaseOS.repo]是基本yum源文件,如果我们能上网,那它是默认生 ...
- httprunner版本没有更新问题
使用命令行创建虚拟环境,创建脚手架目录后,使用pycharm打开所创建的脚手架目录. 执行:hrun demo_testcase_request.yml 提示: E:\hrun_ven\test_hr ...
- springBoot 基础入门
来处:是spring项目中的一个子项目 优点 (被称为搭建项目的脚手架) 减少一切xml配置,做到开箱即用,快速上手,专注于业务而非配置 从创建项目上: -- 快速创建独立运 ...
- 51nod1821-最优集合【贪心】
正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1821 题目大意 \(n\)个可重集合,\(T\)次询问,询问将集合\(S ...
- 项目配置shiro原缓存注解失效
项目用springboot + shiro + ehcache @cacheable 注解不起作用原因 Shiro框架初始化比Spring框架的某些部件早,导致使用@Autowire注入Shiro框架 ...
- Liunx下Mysql,MongoDB性能优化的配置
场景 这几天在赶十一上线的项目,但是突然发现接口性能不好,高并发支持不住.又不想改代码,就在数据库层面进行优化. Mysql 分区:项目中有对40万条的数据进行时间查询的要求,就算对DateTime建 ...
- java 请求第三方接口 GET\POST 实现方法
(1)GET方法 /** * 根据高德地图api获取位置信息 * @return * */ public static String getMapAddInfo(String httpurl) { H ...
- 查询windows日志
系统日志可以用来查看系统的一些信息,比如警告.错误.验证.开关机等. 打开系统日志 按下快捷键win+R,输入eventvwr.exe,并点击确定 查询开关机记录 点击左侧Windows日志-> ...
- SphereEx 公司成立,推动 Apache ShardingSphere 社区加速发展
近日,SphereEx 商业公司在中国红杉种子基金及初心资本助力下,已完成公司及团队组建.各大媒体平台及公众号已相继报道,并抢占新闻头条.作为以 Apache ShardingSphere 核心团队组 ...
- Geocoding Tools(地理编码工具)
地理编码工具 # Process: 创建地址定位器 arcpy.CreateAddressLocator_geocoding("", "", "&qu ...