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配置中心实现原理】
推荐阅读
-
-
云南将建新高铁,全长107公里,共设5个站点,经过你的家乡吗
-
好事儿娇娘|为了延续儿子的性命,在佛前叩首三千回,民间故事:初为人母
-
Techweb■2015 年至 2019 年全球人工智能投资报告:美国公司吸走了 56%
-
肌酐升高时,人体会出现哪些症状?肌酐386中医治疗降为正常值
-
[Andy无泪]商家不给你拿柜台上的,而是去仓库拿货?,为什么去实体店买电脑
-
猫眼娱乐V|呼吸困难送医院,当妈心切称想生4个?曝郭富城旧爱冻卵后太操劳
-
-
血管 让血管一通到底的食物是它们,记得每天吃1种,清除血管垃圾毒素
-
临安检察:商版《庆渝年》之抢“传国玉玺”有没有用?
-
公路施工标准化测量钢筋直螺纹套筒连接工艺及质量检查
-
-
-
-
-
-
游戏提莫酱|IVL秋季赛:Weibo迎战MRC,梦之女巫或成为胜负手
-
-
『石军帅说娱乐』才艺表演唱了歌,打赏礼物的却寥寥无几,胡海泉直播试水不太成功
-
摄影师|奇闻:新郎拍婚纱照只拍到两只脚,新娘不满意,看成片服了