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 ...
随机推荐
- Python中“if __name__=='__main__':”
在Python当中,如果代码写得规范一些,通常会写上一句"if name=='main:"作为程序的入口,但似乎没有这么一句代码,程序也能正常运行.这句代码多余吗?原理又在哪里? ...
- Vue 初学
Vue 的基本代码: 概念简介:Vue.js 是目前最火的一个前端框架,只关注视图层,主要负责MVC中的V这一层 MVC 是后端的分层开发概念: MVVM是前端视图层的概念, ...
- 关于cgroup的几个核心名词及其关系
子系统(subsystem) 所谓子系统可以理解为操作系统里的各种资源(组件),如CPU,内存,磁盘,网卡(带宽) 层级(Hierarchies) 所谓层级就是子系统的集合,又 ...
- 『PyTorch』屌丝的PyTorch玩法
1. prefetch_generator 使用 prefetch_generator库 在后台加载下一batch的数据,原本PyTorch默认的DataLoader会创建一些worker线程来预读取 ...
- 【MySQL】MySQL进阶(外键约束、多表查询、视图、备份与恢复)
约束 外键约束 外键约束概念 让表和表之间产生关系,从而保证数据的准确性! 建表时添加外键约束 为什么要有外键约束 -- 创建db2数据库 CREATE DATABASE db2; -- 使用db2数 ...
- 【C++ Primer Plus】编程练习答案——第2章
1 void ch2_1() { 2 using namespace std; 3 cout << "xxxxxxxx" << endl; 4 } 5 6 ...
- Win32对话框模板创建的窗口上响应键消息,Tab焦点切换消息,加速键消息
今天在学习的时候,发现对话框上不响应键盘消息,查了老半天,终于成功了,现分享出来, 1,首先要在消息循环的时候加如下代码. int WINAPI WinMain(_In_ HINSTANCE hIns ...
- 数字图像处理(一)之灰度转换和卷积python实现
使用Python实现数字图像处理中如下功能: 彩色图像转成灰度图像 实现图像的相关&卷积操作 实现图像的高斯核卷积 使用的库和python版本如下: imageio:2.9.0 用于读取磁盘中 ...
- UOJ 2021 NOI Day2 部分题解
获奖名单 题目传送门 Solution 不难看出,若我们单个 \(x\) 连 \((0,x),(x,0)\),两个连 \((x,y),(y,x)\) ,除去中间过对称轴的一个两个组,就是找很多个欧拉回 ...
- 题解 [NOI2014]购票
题目传送门 题目大意 有一个 \(n\) 个点的树,每个点有三个值 \(p_u,q_u,l_u\) ,现在可以从 \(u\) 走到点 \(v\) 当且仅当 \(v\) 是 \(u\) 的祖先并且 \( ...