pom.xml 依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>

application.properties
nacos.config.server-addr=127.0.0.1:8848
启动类:

@SpringBootApplication
@NacosPropertySource(dataId = "springboot2-nacos-config", autoRefreshed = true)
@RestController
public class DemoApplication {
public static void main(String[] args) {
System.out.println("12312");
new ArrayList<>();
SpringApplication.run(DemoApplication.class, args);
}
@NacosValue(value = "${nacos.test.propertie:123}", autoRefreshed = true)
private String testProperties;
@GetMapping("/test")
public String test(){
return testProperties;
}
}
访问 127.0.0.1:8080/test

nacos控制中心

修改下值 再次访问

本文详细介绍了如何在Spring Boot项目中使用Nacos作为配置中心,包括pom.xml依赖配置、application.properties设置、启动类注解使用及动态刷新配置的实现。通过访问127.0.0.1:8080/test接口展示Nacos配置效果。


被折叠的 条评论
为什么被折叠?



