「这是我参与11月更文挑战的第4天,活动详情查看:2021最后一次更文挑战」
软件环境:Nacos版本2.0.3 SpringCloud Alibaba 2.2.3.RELEASE
问题描述:
- 检查了POM文件有引入nacos-config
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
2.在boostrap.yml配置nacos-config配置中心地址
# Tomcat
server:
port: 8888
# Spring
spring:
application:
# 应用名称
name: memberbusiness
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 127.0.0.51:8848
namespace: ${namespace}
config:
namespace: ${namespace}
# 配置中心地址
server-addr: 127.0.0.51:8848
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs[0]:
data-id: ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
group: DEFAULT_GROUP # 默认为DEFAULT_GROUP
refresh: true # 是否动态刷新,默认为false
shared-configs[1]:
data-id: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
group: DEFAULT_GROUP
refresh: true
shared-configs[2]:
data-id: seata-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
group: DEFAULT_GROUP
refresh: true
# shared-configs:
# - ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# - seata-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
devtools:
restart:
enabled: true #设置开启热部署
additional-paths: src/main/java #重启目录
exclude: WEB-INF/**
freemarker:
cache: false #页面不加载缓存,修改即时生效
tinyid:
batch:
size:
max: 100000
bizType: member_id
client:
token: 0f673adf80504e2eaa552f5d791b644c
- 检查启动类启用了SpringCloudApplication
- 测试类代码:
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
@Api(value = "配置测试")
@RefreshScope
@RequestMapping(value = "/config")
public class ConfigController {
@Value("${precharge.appId2:}")
private String appId2;
@GetMapping(value = "/show")
public String showConfig(){
return appId2;
}
}
- nacos 对应namespace和group下的的 memberbusiness-dev.yml配置项
precharge:
appId2: ap22671
- 启动后,浏览器访问/config/show 可以得到
ap22671。但是修改更新后并没有得到新值
找了网上很多博文,都是清一色的介绍加个@Value + @RefreshScope ,enbale=true 。或者介绍说使用@NacosValue(autoRefresh=true),需要使用对象Properties来实现。这些方法都试过了,还是不行。
今晚在验证的时候,突然想到# 共享配置 shared-configs:列表里面,意思是共享配置,那现在将自己${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} 也配置在# 共享配置 shared-configs:列表里面,是否这个的问题。印象中是没有这种配置过。对比了旧的项目配置文件,人家正常的可以获取到自动更新,怀疑是这里出了问题。将配置改成了下面的重新验证发现可以自动刷新更新后的配置了
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
- seata-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
使用shared-configs 自动刷新来替代 nacos 自动刷新 在Baidu上搜索,提到shared-configs[0], shared-configs[1] 这种配置。查看了下源码:
public static class Config {
public Config(String dataId) {
this.dataId = dataId;
}
public Config(String dataId, String group) {
this(dataId);
this.group = group;
}
public Config(String dataId, boolean refresh) {
this(dataId);
this.refresh = refresh;
}
public Config(String dataId, String group, boolean refresh) {
this(dataId, group);
this.refresh = refresh;
}
从构造方法可以看出。默认是不自动刷新的。所以更新,支持自动刷新的正确写法是:
shared-configs[0]:
data-id: ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
group: DEFAULT_GROUP # 默认为DEFAULT_GROUP
refresh: true # 是否动态刷新,默认为false
shared-configs[1]:
data-id: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
group: DEFAULT_GROUP
refresh: true
shared-configs[2]:
data-id: seata-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
group: DEFAULT_GROUP
refresh: true
# shared-configs:
或者改为:
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
- seata-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}