项目初始配置:
- maven依赖如下:
<!--配置与发现依赖--> <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>
- 类声明
- 两种监听配置的方式:手动注册、使用注解
场景复现
- 修改文件内容:
2.如下结果所示:
只有手动添加的listener能获取到变更内容。注解的形式不生效
排查思路:
@NacosConfigListener既是注解,则应有相应的注解处理器。收拾全局后,没有找到,chatgpt询问后得知其对应的处理器类为:com.alibaba.nacos.spring.context.annotation.config.NacosConfigListenerMethodProcessor(在nacos-spring-context.jar中)。因此只要保证处理器类被正确注册使用即可
解决:
step1.添加依赖
<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-spring-context</artifactId> <version>1.1.1</version> </dependency>
step2.确保处理器类被正确加载注入spring容器
package cn.yangfeiself.serverorder; import com.alibaba.nacos.api.annotation.NacosProperties; import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //启用nacosConfig注解,该注解最终会扫描加载NacosConfigListenerMethodProcessor类 @EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "${spring.cloud.nacos.config.serverAddr}")) public class ServerOrderApplication { public static void main(String[] args) { SpringApplication.run(ServerOrderApplication.class, args); } }
结果:
标签:NacosConfigListener,spring,nacos,alibaba,注解,com,监听,cloud From: https://www.cnblogs.com/swz1104919/p/17562659.html