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配置中心实现原理】
推荐阅读
-
明星婚姻|妻子倾力将他捧红,成名却遭抛弃,如今前妻再嫁富商他仍孑然一身
-
音乐盛典女歌手红毯生图:鞠婧祎再现“半永久妆发”,?Ella脸微肿
-
-
上海海关对两家企业立案调查,退运15吨涉案“洋垃圾”
-
基金|百亿基金规模大缩水,部分基民或踏空!震荡行情中如何找准买卖时机?
-
新华网@中国宝鸡市中心医院成立专家组指导中国第35批援苏丹医疗队开展新冠肺炎防控工作
-
-
中国好声音|历届中国好声音冠军,各有各的特点,我最看好的唯有她!
-
"白坭镇快手达人孵化成长行动"正式启动,快手将打造网红镇新典范
-
@即将上线的三款皮肤,2020年最丑和最漂亮的都来了,网友纷纷好评
-
唐嫣|身体保湿乳哪个牌子好 十大身体乳性价比高的推荐
-
周三:下周三起,8号线北段、9号线、亦庄线、房山线继续提升运力
-
健康长寿|男性想健康长寿?这3个习惯可得坚持好,长寿才会“走近”
-
东风本田执行副总经理郑纯楷:建设第三工厂突破产能瓶颈
-
炎枫解说|英雄联盟里卡萨丁与吸血鬼谁的后期能力更强?
-
武侯区疾控中心|面包新语一加盟店蛋糕检出沙门氏菌,全国门店整改审查
-
育儿知识盒子|你属于哪一级?,“中国家庭收入等级”出炉
-
「刘亦菲」蓝盈莹首谈花木兰试镜!败给刘亦菲不可惜,盛赞对方名副其实
-
-
大国风暴|莫迪惊呼:他们是龙的传人,停火协议无效!印度地方武装揭竿而起