Spring Cloud 2.2.2 源码之二十六nacos客户端获取配置原理一
NacosConfigService大致的结构

NacosConfigManager
上篇说了,BootstrapApplicationListener监听方法里会进行NacosConfigBootstrapConfiguration的注册,然后里面有个比较重要的注入对象NacosConfigManager。我们来看看他的构造方法做了什么。

createConfigService创建配置服务
因为是第一次,所以会创建,这里你会发现他给NacosConfigManager类对象上锁了,防止多线程多次创建NacosConfigManager而重复创建配置服务,不多啰嗦继续。

NacosConfigProperties的assembleConfigServiceProperties一些初始化配置

NacosFactory的createConfigService
其实就是反射出NacosConfigService,然后获取有参构造方法,反射创建实例。
public static ConfigService createConfigService(Properties properties) throws NacosException {
return ConfigFactory.createConfigService(properties);
}
public static ConfigService createConfigService(Properties properties) throws NacosException {
try {
Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.config.NacosConfigService");
Constructor constructor = driverImplClass.getConstructor(Properties.class);
ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties);
return vendorImpl;
} catch (Throwable e) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
}
}
NacosConfigService构造方法
里面有几个组件,ServerHttpAgent做http请求的代理,MetricsHttpAgent包装了ServerHttpAgent,加了计时的功能,ClientWorker做配置文件检查。
public NacosConfigService(Properties properties) throws NacosException {
String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
if (StringUtils.isBlank(encodeTmp)) {
encode = Constants.ENCODE;//默认设置utf-8
} else {
encode = encodeTmp.trim();
}
initNamespace(properties);
agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
agent.start();
worker = new ClientWorker(agent, configFilterChainManager, properties);
}
ServerHttpAgent
ServerListManager用来管理注册中心集群列表,SecurityProxy用来安全验证的,默认没有用户名就没启用,登录验证直接返回true。另外这里会启一个调度任务,每5秒执行登录验证,但是内部还有个令牌时间判断的,超时了才会去验证。
public ServerHttpAgent(Properties properties) throws NacosException {
serverListMgr = new ServerListManager(properties);
securityProxy = new SecurityProxy(properties);
namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE);
init(properties);//设置编码,密码,最大重试次数
securityProxy.login(serverListMgr.getServerUrls());//代理登录
//单线程调度执行器
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.config.security.updater");
t.setDaemon(true);
return t;
}
});
//无延迟开始调度,每5秒一次
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
securityProxy.login(serverListMgr.getServerUrls());
}
}, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS);
}
包装的MetricsHttpAgent:

ClientWorker
http代理就是MetricsHttpAgent对象,有个过滤器管理器ConfigFilterChainManager,默认里面没有过滤器,可以addFilter自己加。这里也开启了一个单线程的执行器,执行checkConfigInfo检查配置任务,每10毫秒一次,去检查当前的配置数量,如果超过一个轮询任务的限制数量,默认3000个,就开启一个新的任务去做。
public ClientWorker(final HttpAgent agent, final ConfigFilterChainManager configFilterChainManager, final Properties properties) {
this.agent = agent;
this.configFilterChainManager = configFilterChainManager;
init(properties);
executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.Worker." + agent.getName());
t.setDaemon(true);
return t;
}
});
//有cpu核数的线程,用来做长轮询的,每次检查配置,如果LongPollingRunnable任务的配置缓存超过一定数量,默认3000个,就要去开启一个新任务去检查配置
executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.Worker.longPolling." + agent.getName());
t.setDaemon(true);
return t;
}
});
//配置检查
executor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
checkConfigInfo();
} catch (Throwable e) {
LOGGER.error("[" + agent.getName() + "] [sub-check] rotate check error", e);
}
}
}, 1L, 10L, TimeUnit.MILLISECONDS);
}
先介绍下这个,其他的后面说。
好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。
本文深入剖析SpringCloud中Nacos配置中心的工作原理,包括NacosConfigService的结构、配置服务的创建过程、初始化配置、反射实例化及核心组件如ServerHttpAgent、ClientWorker的职责,适合对Nacos配置管理感兴趣的开发者。
3372

被折叠的 条评论
为什么被折叠?



