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);
}
先介紹下這個,其他的後面說。
好了,今天就到這裏了,希望對學習理解有幫助,大神看見勿噴,僅爲自己的學習理解,能力有限,請多包涵。