参看:https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
1、添加依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本
2、在 application.properties
中配置 Nacos server 的地址:
nacos.config.server-addr=127.0.0.1:8848
3、使用 @NacosPropertySource
加载 dataId
为 example
的配置源,并开启自动更新
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
4、通过 Nacos 的 @NacosValue
注解设置属性值
@Controller
@RequestMapping("mytestconfig")
public class ConfigController {
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public boolean get() {
return useLocalCache;
}
}
5、启动 NacosConfigApplication
,调用 curl http://localhost:8080/mytestconfig/get
,返回内容是 false
。
6、通过调用 Nacos Open API 向 Nacos server 发布配置:dataId 为example
,内容为useLocalCache=true
方式一:使用postman发送post请求
方式二:进入nacos页面编辑Data ID 为 example的配置,并发布。
7、再次访问 http://localhost:8080/mytestconfig/get
,此时返回内容为true
,说明程序中的useLocalCache
值已经被动态更新了。
总结:
* 依赖项:nacos-config-spring-boot-starter
* Nacos服务器地址配置在application.properties
* @NacosPropertySource注解指定配置源
* @NacosValue注解指定配置值