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配置中心实现原理】
推荐阅读
-
收藏大师|这4位明星竟真的是警察,张家辉上榜,最后一位还是“警花”
-
小哈带你看世界|为了还清我国援助资金,竟然以这样的方式,委内瑞拉出奇招
-
-
腾讯极光盒子300元就能改造顶级智能电视,保5年流畅,体验腾讯极光盒子2s
-
和女朋友谈了两年多了,她父母非叫她回老家,很坚决,该咋办
-
支付宝|防止小学生游戏充值?支付宝公开未成年人防沉迷专利
-
生生不息|嘴里容易咳出痰,这是鼻涕吗?不妨了解下!,人在咳嗽时
-
|邱淑贞女儿沈月颜值太高了!肤白貌美赢过偶像剧女主,随便拍都是大片
-
-
从心理学角度来看,现在被家长“惯坏”的零零后有多少会成为社会的隐患
-
不甘心的萌可爱|就一定要改正?做个“孤独患者”不好吗?,孩子内向
-
-
-
社交场合需要注意的7个礼仪 公共社交礼仪常识有什么
-
猪肉|中国队穿防护服赴日参赛,日网友评论亮了!回应来了……
-
梦见自己的鞋找不到穿别人的鞋是什么意思 梦见自己鞋找不到穿别人的鞋走在混浊的水里
-
儿童饮食|晒晒孩子开学一周的早餐,不浪费,搭配均衡,网友:照着做准没错
-
为什么想应聘文员,面试官问你为什么不做销售想做文员-
-
新民晚报|志愿者专属福利——“上海公益保”11月1日起实施了!
-
第一次使用保温杯该如何清洗 第一次使用保温杯怎么清洗