dataMap = NacosDataParserHandler.getInstance().parseNacosData(data, fileExtension);return dataMap == null ? EMPTY_MAP : dataMap;}// ...return EMPTY_MAP;}}public class NacosConfigService implements ConfigService {public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {return getConfigInner(namespace, dataId, group, timeoutMs);}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);// 这里会从缓存文件中获取,如果能获取就不会再从远程加载了// 会从如下缓存目录下加载配置:// System.getProperty("JM.SNAPSHOT.PATH",// System.getProperty("user.home")) + File.separator + "nacos"// + File.separator + "config"String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant);if (content != null) {cr.setContent(content);configFilterChainManager.doFilter(null, cr);content = cr.getContent();return content;}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) {if (NacosException.NO_RIGHT == ioe.getErrCode()) {throw ioe;}}content = LocalConfigInfoProcessor.getSnapshot(agent.getName(), dataId, group, tenant);cr.setContent(content);configFilterChainManager.doFilter(null, cr);content = cr.getContent();return content;}}public class ClientWorker implements Closeable {// 这里就是从远程服务拉取配置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;}HttpRestResult result = null;try {Map params = new HashMap(3);if (StringUtils.isBlank(tenant)) {params.put("dataId", dataId);params.put("group", group);} else {params.put("dataId", dataId);params.put("group", group);params.put("tenant", tenant);}result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout);} catch (Exception ex) {throw new NacosException(NacosException.SERVER_ERROR, ex);}switch (result.getCode()) {case HttpURLConnection.HTTP_OK:// 获取成功后会将数据保存到缓存目录下LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, result.getData());ct[0] = result.getData();if (result.getHeader().getValue(CONFIG_TYPE) != null) {ct[1] = result.getHeader().getValue(CONFIG_TYPE);} else {ct[1] = ConfigType.TEXT.getType();}return ct;case HttpURLConnection.HTTP_NOT_FOUND:LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, null);return ct;case HttpURLConnection.HTTP_CONFLICT: {throw new NacosException(NacosException.CONFLICT,"data being modified, dataId=" + dataId + ",group=" + group + ",tenant=" + tenant);}throw new NacosException(result.getCode(), result.getMessage());}default: {throw new NacosException(result.getCode(),"http error, code=" + result.getCode() + ",dataId=" + dataId + ",group=" + group + ",tenant="+ tenant);}}}}到此就完成了远程配置数据的加载
总结:Nacos Config先从本地缓存中获取数据,如果不能获取才从远程拉取(如果远程服务无法连接,那么会不断重试,前提是本地能够加载到数据,否则服务将无法正常启动) 。
完毕!!!
【Spring Cloud Nacos配置中心实现原理】
推荐阅读
-
-
兵哥说事|向全球二号强国发出邀请,美军不打叙利亚了?亚洲大国成最新目标
-
QQ音乐|QQ音乐正式上线HiRes音质:百万首歌曲镶上小金标
-
揭秘月经不调的元凶 食疗调节月经 让女性吃越来越年轻的食物
-
中国新闻网|男子每晚爬窗守护感染新冠母亲至其逝世 感动20万人
-
“出轨门”7年,宋喆变大叔,宝强开辟新天地,马蓉现状让人意外
-
-
北京学生到外地上大学,到处是外地人,咋找一个北京的女友
-
剑网3祈愿青竹获取攻略|剑网3祈愿青竹怎么获取?剑网3祈愿青竹获取攻略
-
CCTV7国防军事|当特战尖兵遇上世界冠军,谁更强?
-
封面风尚|张馨予欧弟妻子真时尚,引领牛仔衬衫潮流,70到90后都在穿
-
今年高考报名人数达1071万人 各地精心做好防疫准备
-
北京八大胡同都是哪几个胡同,北京八大胡同在什么地方-
-
『梦幻西游』梦幻西游:高灵九眼必出极品?老王连打15枚九眼,老板直接刷超火
-
世界那么大|这6个技巧就够用了,拍摄建筑物要注意什么
-
苏亚雷斯|6-1!苏神首秀踢疯了,105秒助攻+连进2球,巴萨遭打脸
-
『穿衣搭配』40岁的阿姨年龄大,对穿搭有点粗心大意,好在很适合穿针织裙子
-
-
大京网更高精度,Aqara高精度人体传感器来了!还有Aqara TVOC 空气健康伴侣
-