Nacos Client Config相关源码解析
整体概览
最近在筹备对接公司内自研的ACM服务,但由于ACM短期内尚未计划提供完备的JAVA SDK,所以需要自行研究学习一下Nacos是如何实现的,来开发配套的SDK。由于公司内提供的也是长轮询机制的HTTP接口,所以本文的分析是基于Nacos 1.4.2版本的。后续也会继续对nacos-spring、nacos-spring-boot、nacos-spring-cloud进行研究。
nacos-client依赖了nacos-api与nacos-common两个模块,nacos-api模块主要用于定义nacos对外提供的功能接口以及扩展接口,nacos-common模块主要是用于定义nacos项目中通用的工具类,而nacos-client模块则负责配置管理与服务注册的具体实现。
在nacos项目中,ConfigService定义了配置管理的增删改查以及配置监听的接口,NacosConfigService负责具体功能的实现。在NacosConfigService中,主要依赖于两个类来完成配置管理相关的实现:
-
ServerHttpAgent:负责与Nacos Server进行远程通信,利用HTTP请求查询配置、发布配置
-
ClientWorker:负责管理应用中注册的配置项以及配置项的数据更新,每个配置项对应一个CacheData,CacheData负责配置项的数据存储以及事件通知
NacosConfigService
NacosConfigService作为ConfigService的具体实现类,主要依赖与ClientWorker与ServerHttpAgent来为应用提供配置的查询、发布、删除与监听。大部分的接口都是直接调用这两个类的方法来实现的,在后面的小节我们逐个分析。这里主要关注一下NacosConfigService在获取配置时的特殊逻辑。由于配置存储在远端server上,为了避免server故障或网络故障,nacos client在获取配置时提供了failover与snapshot两种备用机制。
首先如果应用所处的环境中在特定目录下(默认是{user.home}/nacos/config/serverName_nacos/data/config-data)存在配置文件,那么将会直接加载这个配置文件并返回。其次,如果没有failover的本地配置,那么就会server利用http请求查询远程配置。最后如果在限定时间内未能获取到远程配置,则会查询当前服务是否有snapshot配置,snapshot配置与failover配置类似存放在特定的目录下。
除此以外我们可以注意到在获取配置时ConfigFilterChainManager还会负责对当前配置进行一次过滤,在ConfigFilterChainManager中维护了一个通过ServiceLoader的自动加载的IConfigFilter的List列表,用户可以自实现IConfigFilter来完成配置内容的过滤,例如对配置进行解密(从getConfigInner函数中每个步骤都会加载EncryptedDataKey,但实际上目前在nacos-client中并没有解密相关的IConfigFilter)
private String getConfigInner(String tenant, String dataId, String group, long timeoutMs) throws NacosException {
group = blank2defaultGroup(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);
String encryptedDataKey = LocalEncryptedDataKeyProcessor
.getEncryptDataKeyFailover(agent.getName(), dataId, group, tenant);
cr.setEncryptedDataKey(encryptedDataKey);
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();
return content;
}
try {
ConfigResponse response = worker.getServerConfig(dataId, group, tenant, timeoutMs);
cr.setContent(response.getContent());
cr.setEncryptedDataKey(response.getEncryptedDataKey());
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();
return content;
} catch (NacosException ioe) {
if (NacosException.NO_RIGHT == ioe.getErrCode()) {
throw ioe;
}
LOGGER.warn("[{}] [get-config] get from server error, dataId={}, group={}, tenant={}, msg={}",
agent.getName(), dataId, group, tenant, ioe.toString());
}
LOGGER.warn("[{}] [get-config] get snapshot ok, dataId={}, group={}, tenant={}, config={}", agent.getName(),
dataId, group, tenant, ContentUtils.truncateContent(content));
content = LocalConfigInfoProcessor.getSnapshot(agent.getName(), dataId, group, tenant);
cr.setContent(content);
String encryptedDataKey = LocalEncryptedDataKeyProcessor
.getEncryptDataKeyFailover(agent.getName(), dataId, group, tenant);
cr.setEncryptedDataKey(encryptedDataKey);
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();
return content;
}
Failover与Snapshot相关的功能都是由LocalConfigInfoProcessor负责提供的,Failover的配置文件是由开发者负责生成与保存的,LocalConfigInfoProcessor只负责读取,而Snapshot是由LocalConfigInfoProcessor负责保存、读取与清理的。Snapshot代表着当前远程服务的配置当前的快照,保存的时间点是在每次获取远程配置的时候,即调用ClientWorker的getServerConfig函数时。当远程请求成功获取到配置或者是当远程请求返回配置不存在时,都会通过LocalConfigInfoProcessor刷新本地配置快照文件。
public ConfigResponse getServerConfig(String dataId, String group, String tenant, long readTimeout)
throws NacosException {
HttpRestResult<String> result = null;
try {
.....
result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout);
} catch (Exception ex) {
....</