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配置中心实现原理】
推荐阅读
-
『石器时代』地球上有生物进入“石器时代”?人类文明的竞争对手出现?
-
新氧美容APP@网红没文化有多可怕,为卖货吹嘘产品获得“诺贝尔化妆学奖”?
-
awesome科技@X2为OPPO技术实力证明,多年耕耘终获果实,满身黑科技的Find
-
美丽秀|学习日本女孩“水果穿搭”,你就是元气女孩,甜美女孩是穿出来的
-
特斯拉|特斯拉2021年全年交付量达93.6万辆 四季度销量史上最佳
-
-
文章保姆揭露:马伊琍离婚不止因出轨,真相比出轨还令人难以忍受!
-
饭后汽车闲谈|车主小哥还有辆X6,提车有龙虾午餐,130万买宝马840i
-
经济观察网|国际油价失守40美元/桶关口 成品油调价大概率再触“地板价”
-
-
普拉达女王|简约优雅且高级,完完全全的日系风,中国女性的日系穿搭
-
学会这样做咖喱牛肉烩饭,营养美味又能解馋,挑食的孩子都喜欢吃
-
-
-
全程都在搔首弄姿,简直称得上是显眼包,也导致自己的路人缘消耗殆尽
-
路行千里车担忧|价格更是“感人”,董明珠又成功了!30亿造车:车标比奥迪多一个圈
-
风高科技|唯独缺了现总统,特朗普他骂过我,三位前总统现身民权领袖葬礼
-
-
-
pentalaser 奔腾激光20KW激光切割机典型使用案例