前言
本小结将学习一下Apollo Client如何监听配置中心配置信息修改事件的。
在实际的业务场景应用中Apollo Client会需要时刻监听配置中心中的配置状态,比如配置信息的新增/修改/删除等操作时,需要作出相应的动作。
案例
使用 SpringCloud微服务 Apollo分布式配置中心客户端获取配置(三) 中的案例,启动配置中心。
Apollo Client端实现
项目结构
Core Code
ApolloConfigChangeListener1.java
/** * * @author Dustyone * @DateTime 2019年3月22日 下午5:19:42 * */ @Component public class ApolloConfigChangeListener1 { private static final Logger logger = LoggerFactory.getLogger(ApolloConfigChangeListener1.class); //使用Apollo Client API获取配置信息 Config config = ConfigService.getAppConfig(); // config instance is singleton for each namespace and is never null @ApolloConfigChangeListener(value = {ConfigConsts.NAMESPACE_APPLICATION,"dustyone.yml","customized.properties"}, interestedKeyPrefixes= ("crm.*")) public void onChange(ConfigChangeEvent changeEvent) { logger.info("Changes for namespace " + changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); logger.info(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType())); } } }
application.properties
#与Apollo配置中心的APPID一致 app.id=Dustyone163 #设置Client的meta即配置中心地址 apollo.meta=http://127.0.0.1:8080 #开启namespace注入功能,默认注入的是application.properties/application.yml apollo.bootstrap.enabled = true #申明注入的namespace apollo.bootstrap.namespaces = application,dustyone.yml,customized.properties
修改customized.properties中的配置信息:
ASIA-11
客户端后台日志
2019-03-22 17:23:15.332 INFO 1804 --- [Apollo-Config-1] c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: ASIA-1, key: customized.broker, beanName: crmRoleController, field: com.example.deal.controller.CrmRoleController.broker
说明更改时间已被监听到了。
ApolloConfigChangeListener 源码解析一下下
package com.ctrip.framework.apollo.spring.annotation; import com.ctrip.framework.apollo.core.ConfigConsts; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented public @interface ApolloConfigChangeListener { /** * 被监听的namespace,默认为ConfigConsts.NAMESPACE_APPLICATION 即 application */ String[] value() default {ConfigConsts.NAMESPACE_APPLICATION}; /** * The keys interested by the listener, will only be notified if any of the interested keys is changed. * 指定被监听的配置属性Key值,若不指定则默认所有的配置属性Value值被修改都将被监控 * 如果interestedKeys 和 interestedKeyPrefixes都未指定则所有的配置属性Value值被修改都将被监控 */ String[] interestedKeys() default {}; /** * 指定被监听的配置属性Key值(采用模糊匹配模式),即假设指定interestedKeyPrefixes= ("crm.*"),那么crm下所有的聚合属性一旦被修改都将被监听、 * 如果interestedKeys 和 interestedKeyPrefixes都未指定则所有的配置属性Value值被修改都将被监控 */ String[] interestedKeyPrefixes() default {}; }
小结
- 在配置文件中可以通过apollo.bootstrap.namespaces来申明需要注入的配置信息
- 使用@ApolloConfigChangeListener注解时可以指定被监听的namespace,比如
@ApolloConfigChangeListener(value = {ConfigConsts.NAMESPACE_APPLICATION,“dustyone.yml”,“customized.properties”})则表示application.*/dustyone.yml/customized.properties三个namespace中的配置信息都将被监听。 - ApolloConfigChangeListener中指定监听的namespace注入优先级高于配置文件中使用apollo.bootstrap.namespaces的声明。比如在配置文件中声明即便未对apollo.bootstrap.namespaces来声明需要注入的namespace,但在ApolloConfigChangeListener中声明了application./dustyone.yml/customized.properties三个,那么application./dustyone.yml/customized.properties这三个namespace都将被注入且被监听。
- interestedKeys和interestedKeyPrefixes的使用个人建议应场景而使用,若无特殊需求请保持无任何被监听的配置属性Key。即不做interestedKeys和interestedKeyPrefixes的声明。
版权声明:本文为u012437781原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。