SpringCloud-动态配置变化监控-获取变化(支持Config、Nacos)
2019-10-17 18:31
1301 查看
SpringCloud-动态配置变化监控-获取变化(支持Config、Nacos)
qq交流群:
812321371
微信交流群:MercyYao
简介
配置中心有原生的
SpringCloud Config、国内开源的 阿里
Nacos、携程
Apollo等。
都是配置热加载的基础上增加了其他的功能。
配置中心具有配置热加载,修改配置 -> 推送到程序 -> 执行配置更新。
而获取配置更新的具体内容,这些组件都没有做到(
SpringCloud Config、
Nacos,
Apollo待验证)。如:获取变化的配置
test更新前为
123,更新后为
111。
SpringCloud Config、
Nacos、
Apollo待验证配置更新都是将整个配置文件推送给服务进行配置对比结果进行更新。这个阶段用户无法从程序中获取更新的内容。
为了实现这个扩展功能点,更新结果可以让程序感知。
在这个阶段用户通过code可以实现配置监听, 监听到某个配置变化做其他操作等。
示例: 获取到推送配置变化, 感知到
test发生变化, 做更新同步数据操作等等。
感知结果集如:
{ `被更新的配置key`:{ before: `原来的值`, after: `更新后的值` }, `被更新的配置key`:{ before: `原来的值`, after: `更新后的值` } }
功能使用
添加依赖
<dependency> <groupId>com.purgeteam</groupId> <artifactId>dynamic-config-spring-boot-starter</artifactId> <version>0.1.0.RELEASE</version> </dependency>
当然实际情况要结合添加动态配置依赖包
SpringCloud Config、
Nacos、
Apollo。
dynamic-config-spring-boot-starter模块不包含以上依赖, 需要自行添加。
@EnableDynamicConfigEvent
简介: 开启这个特性注解,具备配置推送更新监听能力。
启动类添加
@EnableDynamicConfigEvent注解开启配置变化监听功能。
@EnableDynamicConfigEvent @SpringBootApplication public class DynamicConfigSpringBootApplication { public static void main(String[] args) { SpringApplication.run(DynamicConfigSpringBootApplication.class, args); } }
编写事件接收器
创建
NacosListener(名称随意) 实现
ApplicationListener<ActionConfigEvent>#onApplicationEvent方法
@Slf4j @Component public class NacosListener implements ApplicationListener<ActionConfigEvent> { @Override public void onApplicationEvent(ActionConfigEvent event) { log.info("接收事件"); log.info(event.getPropertyMap().toString()); } }
在
NacosListener#onApplicationEvent方法里获取目标值, 作相应的逻辑处理。
ActionConfigEvent event:
public class ActionConfigEvent extends ApplicationEvent { // 事件说明 private String eventDesc; // 更新变化结果集 private Map<String, HashMap> propertyMap; ... }
ActionConfigEvent主要包含
Map<String, HashMap> propertyMap;, 从这里可以获取更新变化结果, propertyMap结构如下:
{ `被更新的配置key`:{ before: `原来的值`, after: `更新后的值` }, `被更新的配置key`:{ before: `原来的值`, after: `更新后的值` } }
更新配置演示
ps: 示例为
Nacos其它配置中心无差别。
原始配置:
test.age=18 user.name=purgeyao
修改配置:
# test.age 将18更新为19 test.age=19 user.name=purgeyao
在
NacosListener#onApplicationEvent方法加入端点调试观察
ActionConfigEvent对象参数。
更新的
test.age已经被记录在了
ActionConfigEvent.propertyMap里了, 从 18 更新为 19。
控制台打印:
2019-10-17 10:44:09.221 INFO 54054 --- [-10.1.1.97_8848] c.p.dynamic.config.demo.NacosListener : 接收事件 2019-10-17 10:45:19.752 INFO 54054 --- [-10.1.1.97_8848] c.p.dynamic.config.demo.NacosListener : {test.age={before=18, after=19}}
总结
上述功能基于
Nacos、
SpringCloud Config或基于
SpringCloud Config之上构建的配置中心都可以使用, 其他配置中心待验证。
qq交流群:
812321371
微信交流群:MercyYao
示例代码地址:dynamic-config-spring-boot
相关文章推荐
- Spring Cloud Config服务化后获取配置失败
- spring cloud config 之svn仓库配置与动态刷新(服务端)
- springcloud实战之14动态刷新分布式配置中心属性配置(config)
- springcloud config 服务获取git配置文件总是报404,Cannot clone or checkout repository,完美帮你解决
- spring cloud config 统一配置中心 读取Git/SVN/本地文件配置及动态刷新
- spring cloud bus与spring cloud config整合实现应用配置动态刷新
- SpringCloud Config Server使用SVN Hooks实现动态刷新配置
- Spring Cloud Alibaba实现服务配置管理和动态服务发现(使用Nacos)
- Spring Cloud Config的配置中心获取不到最新配置信息的问题
- Spring Cloud Config 之svn配置仓库与动态刷新(客户端手动手动刷新)
- Spring Cloud Config 之svn配置仓库与动态刷新(客户端)
- spring-cloud-config + spring-cloud-bus实现动态刷新集群配置
- SpringCloud Config-----4、动态刷新配置
- Spring Cloud Config + Spring Cloud Bus 实现配置文件动态刷新(版本 Finchley.RC1)
- springcloud-config简单搭建及从git获取配置文件问题
- springcloud_config_server远端配置获取
- 完美解决Cannot clone or checkout repository报错,SpringCloud Config配置远程git仓库获取配置
- Spring Cloud Config 之svn配置仓库与动态刷新(客户端自动刷新)
- springboot 2.0 spring boot admin 配置1,登录配置,连接eureka,监控spring cloud client
- (Spring Cloud微服务实战-书中之坑)spring cloud config优先读取本地配置,其次读取git仓库