Nacos启动配置管理-SpringCloud(1)

前言

Nacos官方文档#启动配置管理

时间线——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
2
3
4
5
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${latest.version}</version>
</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

版本说明 Wiki

打不开 github 可以点这里

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# Nacos 控制台添加配置:
# 在 Nacos Spring Cloud 中,dataId 的完整格式如下:${prefix}-${spring.profiles.active}.${file-extension}
# spring.profiles.active 即为当前环境对应的 profile,当 spring.profiles.active 为空时,对应的连接符 - 也将不存在
# prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
# Data ID:example.properties
# Group:DEFAULT_GROUP
# 配置内容:useLocalCache=true
spring.application.name=example
spring.profiles.active=
# dataId 的 prefix
#spring.cloud.nacos.config.prefix:
# 指定配置的后缀,支持 properties、yaml、yml,默认为 properties
spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.file-extension=yaml

代码

通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新。

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

@Value("${useLocalCache:false}")
private boolean useLocalCache;

@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}

测试

  1. 首先通过调用 Nacos Open APINacos Server 发布配置:dataIdexample.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"
  2. 运行 NacosConfigApplication,调用 curl http://localhost:8080/config/get,返回内容是 true

  3. 再次调用 Nacos Open APINacos server 发布配置:dataIdexample.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"
  4. 再次访问 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

本文地址: https://github.com/maxzhao-it/blog/post/15456/