刚开始接触nacos,然后就报了以下错误,网上也找不到任何解决方法:
Failed to instantiate [com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder]:
java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
最后看到一个地方说是Spring的版本和Nacos的版本不一致导致的
来源:https://github.com/alibaba/nacos/issues/529
https://github.com/alibaba/nacos/issues/4554
配置流程如下:
//启动类:
//NacosConfigurationProperties
@NacosPropertySource(dataId = "nacos_test", autoRefreshed = true)
@SpringBootApplication
public class NacoApplication {
public static void main(String[] args) {
SpringApplication.run(NacoApplication.class, args);
}
}
//Controller类
@RestController
public class NacosConfigController {
@NacosValue(value = "${useLocalCache:0}", autoRefreshed = true)
private String useLocalCache1;
@Value("${useLocalCache:1}")
private String useLocalCache2;
@GetMapping("/hello")
public String get() {
return useLocalCache1+"_-_"+useLocalCache2;
}
}
//pom.xml配置类:刚开始选择的Sping2.4.X 就会报错,换成Sping2.3.X的就好了
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
//文件配置类:application.properties
server.port=8090
spring.application.name=tt
nacos.config.server-addr=192.168.1.66:8848
//Nacos配置文件如下:
然后用postman进行测试,发现每次更改nacos_test文件的值,@NacosValue都会实时更新,@Value只会记住初始化启动那次的配置: