SpringCloud Bus 动态刷新全局广播和定点通知
全局广播
前提: 先具备良好的 RabbitMQ 环境
1. 演示广播效果,增加复杂度,再以3355为模板再制做一个3366模块

<!--pom.xml-->
<?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>cloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>cloud-config-client-3366</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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.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>
#bootstrap.yml
server:
port: 3366 spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取 http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址 #服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
//主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class,args);
}
}
//controller
package com.atguigu.springcloud.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${server.port}")
private String serverPort;
@Value(("${config.info}"))
private String configInfo; @GetMapping("/configInfo")
public String getConfigInfo(){
return "serverPort: "+serverPort+"\t\n\n configInfo: "+configInfo;
}
}
2. 讲一下设计思想
1)利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置
2)利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置

图二的架构显然更加合适,图一不合适的原因如下:
打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新的职责
破坏了微服务各节点的对等性
有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就好增加更多的修改
3. 给cloud-config-center-3344配置中心服务端端架消息总线支持
<!--添加消息总线RbbitMQ支持-->
<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>
#rabbit相关配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest #rabbitmq相关配置,暴露bus刷新配置的端点
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh' #凡是暴露监控、刷新的都要有actuator依赖,bus-refresh就是actuator的刷新操作
4. 给cloud-config-client-3355客户端添加消息总线支持
<!--添加消息总线rabbitMQ支持-->
<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>
#rabbit相关配置 15672是web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest #这是客户端,不需要刷新
5. 给cloud-config-client-3366客户端添加消息总线支持
<!--添加消息总线rabbitMQ支持-->
<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>
#rabbit相关配置 15672是web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest #这是客户端,不需要刷新
6. 启动7001、3344、3355、3366,测试
先看一下eureka注册中心,3344,3355,3366都已经注册进来了

github上现在version 是3,访问3344,看拿到的是多少

是3,正常,访问3355

正常,访问3366

现在,在github将version改为4,如果不使用Bus消息总线通知,3344可以拿到新数据:

3355/3366要想拿到新数据需要每个都发送一个Post请求激活,但现在使用了Bus总线,我们只需给3344服务端发送一次Post请求,3355/3366就能自动的跟着更新。测试一下,给3344发送Post:

然后测试3355、3366,这时候已经可以拿到新数据了,但我们并没有重启3355/3366,或给每一个发送post请求


这样就达到了一次修改,广播通知,处处生效。
定点通知
如果现在不想全部通知,指向定点通知该如何呢?
解决:
指定具体某一个实例生效而不是全部
公式:http://localhost:配置中心端口号/actuator/bus-refresh/{destination}
/bus/refresh请求不再发送到具体的服务实例上,而是发给 config server并通过destination参数类指定需要更新配置的服务或实例
案例: 这里我们以刷新运行在3355端口上的config-client为例,只通知3355,不通知3366
现在再来修改github配置文件的version,改为5

现在3344是可以直接拿到新数据,3355/3366不能,我们来发一条这样的Post请求
curl -X -POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
config-client就是3355的微服务名,3355即端口号

现在来刷新3355、3366,只有3355拿到了新数据,3366没被通知,没拿到新数据


通知总结

SpringCloud Bus 动态刷新全局广播和定点通知的更多相关文章
- SpringCloud Bus消息总线简介
简介: SpringCloud Bus配合SpringCloud Config使用可以实现配置的动态刷新 SpringCloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了 ...
- SpringCloud Bus消息总线
在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线. SpringCloud中也有对应的解决方案 ...
- 带你入门SpringCloud 之 通过SpringCloud Bus 自动更新配置
前言 在<带你入门SpringCloud统一配置 | SpringCloud Config>中通过 SpringCloud Config 完成了统一配置基础环境搭建,但是并没有实现配置修改 ...
- SpringCloud学习笔记(八、SpringCloud Bus)
目录: 什么是bus消息总栈 如何使用bus消息总栈 什么是bus消息总栈 SpringCloud Bus使用轻量的消息代理连接分布式系统的各个节点,可以用于系统状态变更时的广播(如配置变更)或其它管 ...
- SpringCloud全家桶学习之消息总线---SpringCloud Bus
一.概述 ConfigClient(微服务)从ConfigServer端获取自己对应的配置文件,但是目前的问题是:当远程git仓库配置文件发生改变时,每次都是需要重启ConfigCient(微服务), ...
- 使用SetLocaleInfo设置时间后必须调用广播WM_SETTINGCHANGE,通知其他程序格式已经更改
uses messages; Procedure SetDateFormat; //设置系统日期格式var buf:pchar; i:integer; p:DWORD;begin getmem(buf ...
- workerman Channel组件全局广播
<?phpuse Workerman\Worker; require_once '../../web/Workerman/Autoloader.php';require_once '../../ ...
- iview Message(全局提示)与Notice(通知提醒)全局配置方法
在使用iview 的Message与Notice组件时,可以对提示框的显示位置与显示时长进行配置. iview提供了两个配置属性.分别是: top 提示组件距离顶端的距离,单位像素. duration ...
- springcloud Eureka设置服务下线上线事件通知
最近由于微服务会莫名其妙挂掉,导致一些服务宕机: 固所以寻找解决办法,莫名宕机暂时还未查明原因,先人肉解决办法 Eureka的server端会发出5个事件通知,分别是: EurekaInstanceC ...
随机推荐
- C# 特性篇 Attributes
特性[Required] (必修的) /// <summary> /// 操作人EmpID /// </summary> [Required] public string Op ...
- Redis系列(八):数据结构List双向链表中阻塞版本之BLPOP、BRPOP和LINDEX、LINSERT、LRANGE命令详解
1.BRPOP.BLPOP BLPOP: BLPOP 是阻塞式列表的弹出原语. 它是命令 LPOP 的阻塞版本,这是因为当给定列表内没有任何元素可供弹出的时候, 连接将被 BLPOP 命令阻塞. 当给 ...
- kubernetes-pod驱逐机制
1.驱逐策略 kubelet持续监控主机的资源使用情况,并尽量防止计算资源被耗尽.一旦出现资源紧缺的迹象,kubelet就会主动终止部分pod的运行,以回收资源. 2.驱逐信号 以下是一些kubele ...
- css3动画讲解,关于css3的@keyframes和animation
通过css3我们可以创建动画,它能取代gif图片.Flash.Js动画等,css3的animation动画是应用在html的DOM元素上的,通过样式来实现的. @keyframes 规则 @Keyfr ...
- HDU 4352 XHXJ's LIS HDU(数位DP)
HDU 4352 XHXJ's LIS HDU 题目大意 给你L到R区间,和一个数字K,然后让你求L到R区间之内满足最长上升子序列长度为K的数字有多少个 solution 简洁明了的题意总是让人无从下 ...
- JVM 学习笔记(一)
一:jvm架构图解 我们经常关注的jdk和jre如图所示: jre包含在jdk中,这里说一下jdk和jre的作用 JRE是Java Runtime Environment的缩写,是Java程序的运行环 ...
- python面试题四:Python web框架
1 django.flask.tornado框架的比较? 2 什么是wsgi? WSGI的全称是Web Server Gateway Interface,翻译过来就是Web服务器网关接口.具体的来说, ...
- 史上最强vue总结~万字长文---面试开发全靠它了
vue框架篇 vue的优点 轻量级框架:只关注视图层,是一个构建数据的视图集合,大小只有几十kb: 简单易学:国人开发,中文文档,不存在语言障碍 ,易于理解和学习: 双向数据绑定:保留了angular ...
- css导航菜单二级显示的问题
m项目中出现了二级菜单的标签是在导航的里面,用css ul>li:hover ul>li>ul>li 这样子实现不了鼠标经过时导航里二级菜单的显示,这里个人感觉是冲突了.最后通 ...
- web自动化测试实战之批量执行测试用例
01实战之分离测试固件 在UI 自动化测试中,不管编写哪个模块的测试用例,都需要首先在测试类中编写测试固件初始化WebDriver类及打开浏览器,执行登录,才能进行下一步业务逻辑的操作,测试用例执行完 ...
