Spring Cloud 2.2.2 源码之二十八nacos客户端获取配置原理三

Spring Cloud 2.2.2 源码之二十八nacos客户端获取配置原理三

NacosPropertySourceLocator的loadNacosDataIfPresent
NacosPropertySourceLocator的loadNacosPropertySource
NacosPropertySourceBuilder的build
loadNacosData
NacosConfigService的getConfig一
NacosConfigService的getConfig二
ClientWorker的getServerConfig
NacosPropertySourceLocator的loadNacosDataIfPresent

先加载,然后把结果放CompositePropertySource 的最前面,加载不到也没关系。

private void loadNacosDataIfPresent(final CompositePropertySource composite,
final String dataId, final String group, String fileExtension,
boolean isRefreshable) {
...
NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group,
fileExtension, isRefreshable);
this.addFirstPropertySource(composite, propertySource, false);
}
1
2
3
4
5
6
7
8
NacosPropertySourceLocator的loadNacosPropertySource

第一次进来的时候是要去获取的,所以走 nacosPropertySourceBuilder.build。

private NacosPropertySource loadNacosPropertySource(final String dataId,
final String group, String fileExtension, boolean isRefreshable) {
if (NacosContextRefresher.getRefreshCount() != 0) {//刷新过了
if (!isRefreshable) {//不刷新,直接缓存取
return NacosPropertySourceRepository.getNacosPropertySource(dataId,
group);
}
}
return nacosPropertySourceBuilder.build(dataId, group, fileExtension,
isRefreshable);
}
1
2
3
4
5
6
7
8
9
10
11
NacosPropertySourceBuilder的build

先加载数据,然后结果封装成NacosPropertySource,放进缓存。

NacosPropertySource build(String dataId, String group, String fileExtension,
boolean isRefreshable) {
Map<String, Object> p = loadNacosData(dataId, group, fileExtension);
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
p, new Date(), isRefreshable);
NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource);//缓存
return nacosPropertySource;
}
1
2
3
4
5
6
7
8
loadNacosData

用NacosConfigService来加载,加载到了就解析成LinkedHashMap返回,否则就是个空的LinkedHashMap。

private Map<String, Object> loadNacosData(String dataId, String group,
String fileExtension) {
String data = null;
try {
data = configService.getConfig(dataId, group, timeout);
...
Map<String, Object> dataMap = NacosDataParserHandler.getInstance()
.parseNacosData(data, fileExtension);
return dataMap == null ? EMPTY_MAP : dataMap;
}
return EMPTY_MAP;
}
1
2
3
4
5
6
7
8
9
10
11
12
NacosConfigService的getConfig一


@Override
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {
return getConfigInner(namespace, dataId, group, timeoutMs);
}

1
2
3
4
5
6
首先优先从本地获取,其实就是从本地读取,比如windows的话,就是C:\Users\Administrator\nacos\config\fixed-127.0.0.1_8848_nacos\data下的config-data后者config-data-tenant目录中获取相应配置文件。

private String getConfigInner(String tenant, String dataId, String group, long timeoutMs) throws NacosException {
group = null2defaultGroup(group);//默认组
ParamUtils.checkKeyParam(dataId, group);//检查参数
ConfigResponse cr = new ConfigResponse();//创建响应

cr.setDataId(dataId);
cr.setTenant(tenant);
cr.setGroup(group);

// 优先使用本地配置
String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant);
if (content != null) {
LOGGER.warn("[{}] [get-config] get failover ok, dataId={}, group={}, tenant={}, config={}", agent.getName(),
dataId, group, tenant, ContentUtils.truncateContent(content));
cr.setContent(content);
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();
return content;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
具体我就不跟了,就是文件判断在不在,然后读取。

NacosConfigService的getConfig二

如果不本地不存在的话,就从网络读。

try {
String[] ct = worker.getServerConfig(dataId, group, tenant, timeoutMs);
cr.setContent(ct[0]);

configFilterChainManager.doFilter(null, cr);
content = cr.getContent();

return content;
} catch (NacosException ioe) {
...
}
1
2
3
4
5
6
7
8
9
10
11
ClientWorker的getServerConfig

用代理请求/v1/cs/configs,传参数dataId,group,tenant获取配置文件。

public String[] getServerConfig(String dataId, String group, String tenant, long readTimeout)
throws NacosException {
String[] ct = new String[2];//放文件内容和类型
if (StringUtils.isBlank(group)) {
group = Constants.DEFAULT_GROUP;
}

HttpResult result = null;
try {
List<String> params = null;
if (StringUtils.isBlank(tenant)) {
params = new ArrayList<String>(Arrays.asList("dataId", dataId, "group", group));
} else {
params = new ArrayList<String>(Arrays.asList("dataId", dataId, "group", group, "tenant", tenant));
}
result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout);
} catch (IOException e) {
。。。
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
让代理去执行请求下篇说吧。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。
————————————————
原文链接:https://blog.csdn.net/wangwei19871103/article/details/105738895

http://www.61bbw.com/home.php?mod=space&uid=905634
http://www.61bbw.com/home.php?mod=space&uid=905635
http://www.61bbw.com/home.php?mod=space&uid=905636
http://www.61bbw.com/home.php?mod=space&uid=905637
http://www.61bbw.com/home.php?mod=space&uid=905638
http://www.61bbw.com/home.php?mod=space&uid=905639
http://www.61bbw.com/home.php?mod=space&uid=905640

猜你喜欢

转载自www.cnblogs.com/dasdfdfecvcx/p/12770675.html
今日推荐