nacos-spring-boot-project
nacos-spring-boot-project copied to clipboard
在Spring Boot项目中,当application.properties文件跟nacos上面有相同的配置时,通过@NacosValue获取不到Nacos上面的配置内容
在Spring Boot项目中,当application.properties文件跟nacos上面有相同的配置时,通过@NacosValue获取不到Nacos上面的配置内容
预想的是在application.properties上配置一些默认配置,在Nacos上面配置一些特殊情况下需要修改的配置。
配置:nacos.config.remote-first=true
@chuntaojun 配置nacos.config.remote-first=true也没有用
application.properties配置为: nacos.config.server-addr=192.168.99.214:8848 nacos.config.remote-first=true name=zhangsan
Nacos上的hello.properties配置为: name=lisi
测试代码为: @SpringBootApplication @RestController @NacosPropertySource(dataId = "hello.properties") public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@NacosValue("${name}")
private String name;
@GetMapping("hello")
public String hello() {
System.out.println(name);
return "hello";
}
}
@chuntaojun 配置nacos.config.remote-first=true也没有用
application.properties配置为: nacos.config.server-addr=192.168.99.214:8848 nacos.config.remote-first=true name=zhangsan
Nacos上的hello.properties配置为: name=lisi
测试代码为: @SpringBootApplication @RestController @NacosPropertySource(dataId = "hello.properties") public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @NacosValue("${name}") private String name; @GetMapping("hello") public String hello() { System.out.println(name); return "hello"; }
}
springboot的方式,推荐配置文件配置好,而不是采用注解,采用注解的话,wiki里面的介绍的特性会无法使用
nacos.config.server-addr=192.168.99.214:8848 nacos.config.data-id=hello nacos.config.type=properties nacos.config.bootstrap.enable=true nacos.config.remote-first=true
name=zhangsan
改成这样可以了,这种方式比使用注解多配置了很多东西吗?没有默认配置 像nacos.config.type,一开始没配置启动直接报错了
nacos.config.server-addr=192.168.99.214:8848 nacos.config.data-id=hello nacos.config.type=properties nacos.config.bootstrap.enable=true nacos.config.remote-first=true
name=zhangsan
改成这样可以了,这种方式比使用注解多配置了很多东西吗?没有默认配置 像nacos.config.type,一开始没配置启动直接报错了
我得空的话估计会对这个整体优化过一下
0.2.3版本没有nacos.config.remote-first=true这个配置?
0.2.3版本没有nacos.config.remote-first=true这个配置?
0.2.7新加的
那是要更新到0.2.7版本吗,单独更新nacos-config-spring-boot-starter可以不
可以
当在application.properties中配置
nacos.config.data-ids=discover.bootstrap.properties nacos.config.type=properties nacos.config.bootstrap.enable=true nacos.config.group=discover nacos.config.remote-first=true
然后又在代码中配置:
@NacosPropertySource(dataId = "discover.bootstrap.properties", groupId = "discover", autoRefreshed = true)
nacos-config-spring-boot-starter 0.2.7 测试,在nacos上手动更新某个值,@NacosValue无法更新。
主要是MutablePropertySources有两个相同配置的NacosPropertySource。NacosPropertySourcePostProcessor#addListenerIfAutoRefreshed添加的匿名监听Listener每次更新的时候只替换最后一个NacosPropertySource,但是NacosValueAnnotationBeanPostProcessor#onApplicationEvent取的又是前面一个NacosPropertySource中的值,所以@NacosValue无法更新。
通过设置first = true解决了
@NacosPropertySource(dataId = "discover.bootstrap.properties", groupId = "discover", autoRefreshed = true, first = true)