这里写自定义目录标题
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监听正常