这里写自定义目录标题
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:8848group: DEFAULT_GROUP
config:#需要配置server-addr: 192.168.56.150:8848group: 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//设置为beanpublicclassConfig{//映射other.yaml ->oher.nameprivateString name;//映射other.yaml ->oher.ageprivateString age;//启动后定时打印当前bean中的值,不推荐这种方式,做测试方法@PostConstructpublicvoidtestValueIsChange(){newThread(()->{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){thrownewRuntimeException(e);}
i++;}while(true);}).start();}//....get/set方法}
2监听
2.1第一种方式nacos的SDK
@ComponentpublicclassOtherListen{@Resource//注入NacosConfigManagerpublicvoidnacosListen(NacosConfigManager nacosConfigManager){//获取配置中心服务ConfigService configService = nacosConfigManager.getConfigService();try{//对配置中心添加监听(配置文件的dataId,group)
configService.addListener("other.yaml","DEFAULT_GROUP",newAbstractConfigChangeListener(){//监听后的处理逻辑@OverridepublicvoidreceiveConfigChange(ConfigChangeEvent configChangeEvent){for(ConfigChangeItem changeItem : configChangeEvent.getChangeItems()){System.out.println("nacos方式监听"+ changeItem.getKey()+"-----"+ changeItem.getNewValue());}}});}catch(NacosException e){thrownewRuntimeException(e);}}//注解监听spring.cloud环境下需要添加额外依赖,可以参考官方文档,这里不做演示@NacosConfigListener(dataId ="other.yaml")publicvoidonReceived(String value){System.out.println("onReceived : "+ value);}}
2.2第二种方式监听spring的环境修改
//利用spring事件通知机制
@ComponentpublicclassOtherListenimplementsApplicationListener<EnvironmentChangeEvent>{@ResourceprivateConfigurableEnvironment environment;@OverridepublicvoidonApplicationEvent(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监听正常
本文转载自: https://blog.csdn.net/qq_35187384/article/details/140344680
版权归原作者 喝一口七喜 所有, 如有侵权,请联系我们删除。
版权归原作者 喝一口七喜 所有, 如有侵权,请联系我们删除。