Spring动态监听Nacos配置中心key值变更的实现方法
作者:qq_34484548
Nacos本身提供支持监听配置变更的操作,但在使用起来,个人感觉不是很友好,无法精确到某个key的变更监听,所以本文小编给大家介绍了Spring动态监听Nacos配置中心key值变更的实现方法,需要的朋友可以参考下
背景:
Nacos本身提供支持监听配置变更的操作,但在使用起来,个人感觉不是很友好,无法精确到某个key的变更监听
实现方法:
1. 自定义一个只能用在方法上的注解(当然你也可以自定义不仅仅用在方法上)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigListener {
/**
* 待监听的key或key前缀
*/
String key();
}2. 添加具体实现
@Slf4j
@Configuration
public class RefreshEnvironmentConfig implements ApplicationListener<EnvironmentChangeEvent>, EnvironmentAware, BeanPostProcessor {
private Environment environment;
private final Map<String, Map<Method, Object>> listeners = new ConcurrentHashMap<>(64);
private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64));
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
Class<?> targetClass = AopUtils.getTargetClass(bean);
Map<Method, ConfigListener> annotatedMethods = MethodIntrospector.selectMethods(targetClass, this::findListenerAnnotations);
if (!annotatedMethods.isEmpty()) {
for (Map.Entry<Method, ConfigListener> entry : annotatedMethods.entrySet()) {
listeners.computeIfAbsent(entry.getValue().key(), k -> new HashMap<>()).put(entry.getKey(), bean);
log.info("Register @ConfigListener methods processed on bean {} listener key {}", beanName, entry.getValue().key());
}
}
this.nonAnnotatedClasses.add(bean.getClass());
}
return bean;
}
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
Set<String> keys = event.getKeys();
keys.forEach(key -> {
String value = environment.getProperty(key);
int index = 0;
do {
index = key.indexOf(".", index + 1);
String subKey = index > 0 ? key.substring(0, index) : key;
Map<Method, Object> methodMap = listeners.get(subKey);
if (methodMap != null) {
methodMap.forEach((method, bean) -> {
try {
method.invoke(bean, key, value);
} catch (IllegalAccessException | InvocationTargetException e) {
log.error("Refresh @ConfigListener {}, error {}", bean, e.getMessage(), e);
}
});
}
} while (index > 0);
});
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
private ConfigListener findListenerAnnotations(Method method) {
return findListenerAnnotationsByAnnotatedElement(method);
}
private ConfigListener findListenerAnnotationsByAnnotatedElement(AnnotatedElement element) {
return AnnotationUtils.findAnnotation(element, ConfigListener.class);
}
}3. demo
@ConfigListener(key = "test.value")
public void listener1(String key, String newValue) {
log.info("listener1 listened {}-->{}", key, newValue);
}
@Async
@ConfigListener(key = "test")
public void listener2(String key, String newValue) {
log.info("listener2 listened {}-->{}", key, newValue);
}已上实现,不仅仅适用于Nacos,理论上也适用于其他配置中心,(个人仅验证过Nacos)
拓展:spring监听nacos配置中心文件变化的两种方式
1.前置条件
1.1依赖
<properties>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
spring-cloud-starter-bootstrap需要引入
1.2配置
1.2.1 nocos配置
我们要监听other.yaml文件的other.name|age属性

1.2.2 bootstrap.yml文件内容
server:
port: 8080
---
spring:
application:
#需要指定
name: gateway
profiles:
active: dev
---
spring:
cloud:
nacos:
discovery:
#需要配置
server-addr: 192.168.56.150:8848
group: DEFAULT_GROUP
config:
#需要配置
server-addr: 192.168.56.150:8848
group: DEFAULT_GROUP
file-extension: yaml
extension-configs:
- data-id: gateway-routes.yaml
group: DEFAULT_GROUP
refresh: true
- data-id: other.yaml
group: DEFAULT_GROUP
refresh: true
1.3属性文件映射
@RefreshScope//Bean 在运行时刷新
@ConfigurationProperties(prefix = "other")//要绑定外部属性的前缀
@Configuration//设置为bean
public class Config {
//映射other.yaml ->oher.name
private String name;
//映射other.yaml ->oher.age
private String age;
//启动后定时打印当前bean中的值,不推荐这种方式,做测试方法
@PostConstruct
public void testValueIsChange() {
new Thread(() -> {
int i = 1;
do {
Config bean = SpringBeanUtil.getBean(Config.class);
System.out.println("--- >" + i + "-->当前bean值: " + "name:" + bean.getName() + "|age:" + bean.getAge());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
i++;
} while (true);
}).start();
}
//....get/set方法
}
2监听
2.1第一种方式nacos的SDK
@Component
public class OtherListen {
@Resource//注入NacosConfigManager
public void nacosListen(NacosConfigManager nacosConfigManager) {
//获取配置中心服务
ConfigService configService = nacosConfigManager.getConfigService();
try {
//对配置中心添加监听(配置文件的dataId,group)
configService.addListener("other.yaml", "DEFAULT_GROUP", new AbstractConfigChangeListener() {
//监听后的处理逻辑
@Override
public void receiveConfigChange(ConfigChangeEvent configChangeEvent) {
for (ConfigChangeItem changeItem : configChangeEvent.getChangeItems()) {
System.out.println("nacos方式监听" + changeItem.getKey() + "-----" + changeItem.getNewValue());
}
}
});
} catch (NacosException e) {
throw new RuntimeException(e);
}
}
//注解监听spring.cloud环境下需要添加额外依赖,可以参考官方文档,这里不做演示
@NacosConfigListener(dataId = "other.yaml")
public void onReceived(String value) {
System.out.println("onReceived : " + value);
}
}
2.2第二种方式监听spring的环境修改
//利用spring事件通知机制
@Component
public class OtherListen implements ApplicationListener<EnvironmentChangeEvent> {
@Resource
private ConfigurableEnvironment environment;
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
for (String key : event.getKeys()) {
System.out.println("spring方式监听: key:" + key + "value:" + environment.getProperty(key));
}
}
}
3.测试
- 启动后,正常读取到nacos other.yaml数据

- 修改nacos other.yaml数据

- 查看监听是否正常
spring监听正常

nacosSDk监听正常

到此这篇关于Spring动态监听Nacos配置中心key值变更的实现方法的文章就介绍到这了,更多相关Spring动态监听Nacos key值变更内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
