Nacos启动配置管理-SpringCloud(1)
前言
时间线——2021-04-19—
Nacos代码实例中的nacos-spring-boot-example/pom.xml中的SpringBoot:2.0.3.RELEASE,其它example中的版本为2.0.4.RELEASE有可能会启动报错,改成2.0.4.RELEASE。
实现
依赖
| 1 | <dependency> | 
组件版本关系
| Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version | 
|---|---|---|---|---|---|
| 2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE | 1.8.0 | 1.4.1 | 4.4.0 | 2.7.8 | 1.3.0 | 
毕业版本依赖关系(推荐使用)
| Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version | 
|---|---|---|
| Spring Cloud 2020.0 | 2021.1 | 2.4.2.RELEASE | 
| Spring Cloud Hoxton.SR8 | 2.2.5.RELEASE | 2.3.2.RELEASE | 
打不开
github可以点这里
配置
| 1 | spring.cloud.nacos.config.server-addr=127.0.0.1:8848 | 
代码
通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新。
| 1 | 
 | 
测试
- 首先通过调用 Nacos Open API 向 - Nacos Server发布配置:- dataId为- example.properties,内容为- useLocalCache=true- 1 - curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=true" 
- 运行 - NacosConfigApplication,调用- curl http://localhost:8080/config/get,返回内容是- true。
- 再次调用 Nacos Open API 向 - Nacos server发布配置:- dataId为- example.properties,内容为- useLocalCache=false- 1 - curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=false" 
- 再次访问 - http://localhost:8080/config/get,此时返回内容为- false,说明程序中的- useLocalCache值已经被动态更新了。
附录
组件版本关系
| Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version | 
|---|---|---|---|---|---|
| 2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE | 1.8.0 | 1.4.1 | 4.4.0 | 2.7.8 | 1.3.0 | 
| 2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE | 1.8.0 | 1.3.3 | 4.4.0 | 2.7.8 | 1.3.0 | 
| 2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE | 1.7.1 | 1.2.1 | 4.4.0 | 2.7.6 | 1.2.0 | 
| 2.2.0.RELEASE | 1.7.1 | 1.1.4 | 4.4.0 | 2.7.4.1 | 1.0.0 | 
| 2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE | 1.7.0 | 1.1.4 | 4.4.0 | 2.7.3 | 0.9.0 | 
| 2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE | 1.6.3 | 1.1.1 | 4.4.0 | 2.7.3 | 0.7.1 | 
毕业版本依赖关系(推荐使用)
| Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version | 
|---|---|---|
| Spring Cloud 2020.0 | 2021.1 | 2.4.2.RELEASE | 
| Spring Cloud Hoxton.SR8 | 2.2.5.RELEASE | 2.3.2.RELEASE | 
| Spring Cloud Greenwich.SR6 | 2.1.4.RELEASE | 2.1.13.RELEASE | 
| Spring Cloud Hoxton.SR3 | 2.2.1.RELEASE | 2.2.5.RELEASE | 
| Spring Cloud Hoxton.RELEASE | 2.2.0.RELEASE | 2.2.X.RELEASE | 
| Spring Cloud Greenwich | 2.1.2.RELEASE | 2.1.X.RELEASE | 
| Spring Cloud Finchley | 2.0.4.RELEASE(停止维护,建议升级) | 2.0.X.RELEASE | 
| Spring Cloud Edgware | 1.5.1.RELEASE(停止维护,建议升级) | 1.5.X.RELEASE |