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的更多相关文章

  1. SpringCloud 2020.0.4 系列之 Feign

    1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解. 言归正传,之前我们聊了 SpringCloud 的服务治理组件 Eureka,今天我 ...

  2. SpringCloud 2020.0.4 系列之 Stream 消息广播 与 消息分组 的实现

    1. 概述 老话说的好:事情太多,做不过来,就先把事情记在本子上,然后理清思路.排好优先级,一件一件的去完成. 言归正传,今天我们来聊一下 SpringCloud 的 Stream 组件,Spring ...

  3. SpringCloud 2020.0.4 系列之 Stream 延迟消息 的实现

    1. 概述 老话说的好:对待工作要有责任心,不仅要完成自己的部分,还要定期了解整体的进展. 言归正传,我们在开发产品时,常常会遇到一段时间后检查状态的场景,例如:用户下单场景,如果订单生成30分钟后, ...

  4. SpringCloud 2020.0.4 系列之 Stream 消息出错重试 与 死信队列 的实现

    1. 概述 老话说的好:出错不怕,怕的是出了错,却不去改正.如果屡次出错,无法改对,就先记下了,然后找援军解决. 言归正传,今天来聊一下 Stream 组件的 出错重试 和 死信队列. RabbitM ...

  5. SpringCloud 2020.0.4 系列之Eureka

    1. 概述 老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气.抱怨或生气. 言归正传,微服务是当今非常流行的一种架构方式,其中 SpringCloud 是我们常用的一种微服务框架. ...

  6. SpringCloud 2020.0.4 系列之服务降级

    1. 概述 老话说的好:做人要正直,做事要正派,胸怀坦荡.光明磊落,才会赢得他人的信赖与尊敬. 言归正传,之前聊了服务间通信的组件 Feign,今天我们来聊聊服务降级. 服务降级简单的理解就是给一个备 ...

  7. SpringCloud 2020.0.4 系列之 Gateway入门

    1. 概述 老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心. 言归正传,今天我们来聊聊 SpringCloud 的网关组件 Gateway,之前我们去访问 SpringCloud 不同服务的接 ...

  8. SpringCloud 2020.0.4 系列之 JWT用户鉴权

    1. 概述 老话说的好:善待他人就是善待自己,虽然可能有所付出,但也能得到应有的收获. 言归正传,之前我们聊了 Gateway 组件,今天来聊一下如何使用 JWT 技术给用户授权,以及如果在 Gate ...

  9. SpringCloud 2020.0.4 系列之服务降级的其他用法与熔断

    1. 概述 老话说的好:控制好自己的情绪,才能控制好自己的人生.冲动是魔鬼,冷静才最重要. 言归正传,之前聊了在 Feign 调用时,如何给整个 Feign接口类 增加降级策略. 今天我们来聊一下 H ...

随机推荐

  1. ecshop 加入购物车和直接购买同时存在的方法

    一.首先将直接购买的链接设置为 <a href="javascript:bool =1;addToCart({$goods.goods_id})"> bool值为1,g ...

  2. Jmeter导出测试报告

    测试数据概述 jemter导出数据 另存为导出csv文件 命令行导出 测试报告的作用: 反馈结果 复现问题,所以需要写明测试场景.数据

  3. nginx 利用return实现301跳转

    第一种: server { location / { rewrite ^/(.*)$ http://www.baidu.com/$1 permanent; } } 第二种: server { loca ...

  4. 分析 ajax 请求并抓取 “今日头条的街拍图”

    今日头条抓取页面: 分析街拍页面的 ajax 请求: 通过在 XHR 中查看内容,获取 url 链接,params 参数信息,将两者进行拼接后取得完整 url 地址.data 中的 article_u ...

  5. Python3入门系列之-----字典

    字典 字典是一种可变容器模型,且存放任何类型对像(如:字符串,数字,或者列表甚至字典),每个字典有键名(key)和键值(value)且用冒号 :  隔开, 多个字典用逗号(,)隔开整个字典包括在花括号 ...

  6. JS中call,apply,bind的区别

    1.关于this对象的指向,请看如下代码 var name = 'jack'; var age = 18; var obj = { name:'mary', objAge:this.age, myFu ...

  7. WPF进阶技巧和实战08-依赖属性与绑定02

    将元素绑定在一起 数据绑定最简单的形式是:源对象是WPF元素而且源属性是依赖项属性.依赖项属性内置了更改通知支持,当源对象中改变依赖项属性时,会立即更新目标对象的绑定属性. 元素绑定到元素也是经常使用 ...

  8. C++面向行输入:get()与getline()

    面向行的输入:get()与getline() 引入: char a = 's';//这样的语句合法 char b = "s";//不合法 /* "S"不是字符常 ...

  9. Python爬取 | 唯美女生图片

    这里只是代码展示,且复制后不能直接运行,需要配置一些设置才行,具体请查看下方链接介绍: Python爬取 | 唯美女生图片 from selenium import webdriver from fa ...

  10. 洛谷2093 JZPFAR + KD-Tree学习笔记 (KD-Tree)

    KD-Tree这玩意还真的是有趣啊.... (基本完全不理解) 只能谈一点自己的对KD-Tree的了解了. 首先这个玩意就是个暴力... 他的结构有点类似二叉搜索树 每一层都是以一个维度作为划分标准. ...