手动监听nacos配置变更
添加基于nacos配置中心的maven依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.7.RELEASE</version> </dependency>
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Slf4j @Component public class EvnChangeListener implements ApplicationListener<EnvironmentChangeEvent> { @Resource private ConfigurableEnvironment environment; @Override public void onApplicationEvent(EnvironmentChangeEvent event) { for (String key : event.getKeys()) { log.info("EvnChangeListener key:{} value:{}", key, environment.getProperty(key)); if (StringUtils.equals("xxxx", key)) { // TODO } } } }
spring-cloud-starter-alibaba-nacos-config
是SpringCloud Alibaba中一个组件,用于在 SpringCloud 应用中使用 Nacos 作为配置中心。基于SpringCloud的配置中心可以使用EnvironmentChangeEvent事件监听配置变更。