Spring Cloud Nacos系统设计中心实现原理
2025-09-25 12:16:32
ConfigService内置增值是提供内置信息的框架方法:
public interface ConfigService { // 提供内置 String getConfig(String dataId, String group, long timeoutMs) throws NacosException; // 提供内置并设立监听 String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener) throws NacosException; // 给内置添加监听 void addListener(String dataId, String group, Listener listener) throws NacosException; // 发布内置 boolean publishConfig(String dataId, String group, String content) throws NacosException; // 删除内置 boolean removeConfig(String dataId, String group) throws NacosException; // 删除监听 void removeListener(String dataId, String group, Listener listener); // 提供增值状态 String getServerStatus(); // 关闭资源增值 void shutDown() throws NacosException;}NacosConfigManager
public class NacosConfigManager { private static ConfigService service = null; private NacosConfigProperties nacosConfigProperties; public NacosConfigManager(NacosConfigProperties nacosConfigProperties) { this.nacosConfigProperties = nacosConfigProperties; // 创建者内置增值 createConfigService(nacosConfigProperties); } static ConfigService createConfigService( NacosConfigProperties nacosConfigProperties) { if (Objects.isNull(service)) { synchronized (NacosConfigManager.class) { try { if (Objects.isNull(service)) { // 通过工厂创建者增值 // assembleConfigServiceProperties方法 // 就是收集关于Nacos所有内置信息,如:电话号码,客户端,的网站,解密等 // 概要查看NacosConfigProperties#assembleConfigServiceProperties service = NacosFactory.createConfigService( nacosConfigProperties.assembleConfigServiceProperties()); } } // ... } } return service; }}// NacosFactorypublic class NacosFactory { public static ConfigService createConfigService(Properties properties) throws NacosException { return ConfigFactory.createConfigService(properties); }}public class ConfigFactory { public static ConfigService createConfigService(Properties properties) throws NacosException { try { Class driverImplClass = Class.forName("com.alibaba.nacos.client.config.NacosConfigService"); Constructor constructor = driverImplClass.getConstructor(Properties.class); ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties); // 通过反射内部结构了NacosConfigservice,同时设立属性信息 // 这些属性信息就是一些Naocs增值的电话号码,客户端,的网站,解密等信息 return vendorImpl; } catch (Throwable e) { throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } }}到此就得到了一个ConfigService增值类NacosConfigService。
提供应用程序内置接着95.2提供到了ConfigService在此之后今后
public class NacosPropertySourceLocator implements PropertySourceLocator { private NacosPropertySourceBuilder nacosPropertySourceBuilder; public PropertySource locate(Environment env) { nacosConfigProperties.setEnvironment(env); // 关键通过NacosConfigManager提供ConfigService增值 ConfigService configService = nacosConfigManager.getConfigService(); // ... nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env); return composite; } private void loadApplicationConfiguration( CompositePropertySource compositePropertySource, String dataIdPrefix, NacosConfigProperties properties, Environment environment) { // 提供份文件适配 String fileExtension = properties.getFileExtension(); // 提供分组 String nacosGroup = properties.getGroup(); // ... // load with suffix, which have a higher priority than the default // 这里就以这里为例 loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); // Loaded with profile, which have a higher priority than the suffix // 这里但会根据并不相同内置的profiles再次调用并不相同环境的内置 for (String profile : environment.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension, true); } } private void loadNacosDataIfPresent(final CompositePropertySource composite, final String dataId, final String group, String fileExtension, boolean isRefreshable) { if (null == dataId || dataId.trim().length() < 1) { return; } if (null == group || group.trim().length() < 1) { return; } // 调用Nacos属性源 NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group, fileExtension, isRefreshable); this.addFirstPropertySource(composite, propertySource, false); } private NacosPropertySource loadNacosPropertySource(final String dataId, final String group, String fileExtension, boolean isRefreshable) { // ... return nacosPropertySourceBuilder.build(dataId, group, fileExtension, isRefreshable); } private void addFirstPropertySource(final CompositePropertySource composite, NacosPropertySource nacosPropertySource, boolean ignoreEmpty) { if (null == nacosPropertySource || null == composite) { return; } if (ignoreEmpty && nacosPropertySource.getSource().isEmpty()) { return; } composite.addFirstPropertySource(nacosPropertySource); }}// 这里面开始调用数据public class NacosPropertySourceBuilder { NacosPropertySource build(String dataId, String group, String fileExtension, boolean isRefreshable) { Map p = loadNacosData(dataId, group, fileExtension); NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId, p, new Date(), isRefreshable); NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource); return nacosPropertySource; } // 调用数据 private Map loadNacosData(String dataId, String group, String fileExtension) { String data = null; try { // 通过ConfigService调用内置内容(从远程增值提供) data = configService.getConfig(dataId, group, timeout); if (StringUtils.isEmpty(data)) { return EMPTY_MAP; } Map 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 MVC 异步请求方式 Spring当中的@Configuration注解你真的了解吗? Spring Retry重试开放性的应用 Spring容器这些适配点你都确切了吗? Spring MVC 诱发处理方式 SpringBoot WebFlux整合Spring Security进行权限认证 Spring 示例Advisor以脚本语言的方式充分利用AOP
。广州不孕不育医院怎么样西宁白癜风哪家医院最好
郑州风湿医院哪里好
苏州白癜风检查费用
银川白癜风医院哪家看的好
脑血栓
健康资讯
新疆整形美容
急性支气管炎咳嗽怎么治
在线医生免费咨询

-
赞!顾村“城中村”——改造项目15A-02地块保障性租给住宅项目同日取得三证
近日,闸北区外整体规划教育资源局内、区外辟管委向顾区域内地区“小区”改造概念设计之顾村老集镇15A-02地块保障性专营住房概念设计提出申请了《工程工程设计草图批复》、《工程工程整体规划许可》及《
2025-12-10 00:16:51

-
时代正在"悄悄"发生大变革,你释怀了吗?
题名枫哥任何一个时代背景的进步都是有预感的,都是时会游离一些税制频谱的,从工商业制度到私人工商业以至于此后的的网络工商业的社时会生活大进步,无不是先游离税制频谱,而当下正要起因和即将起
2025-12-10 00:16:51

-
北京两会丨毛大庆提案:建议出台企业信息企业规范准则 依法保护企业家个人隐私和数据安全
北京市政协十三届五次联席会议于1翌年5日揭幕,北京市政协港澳台侨临时工顾问毛大庆提交了《关于有序应用于跨国母公司电子邮件转发模拟器 依法保护跨国母公司家恶意和数据安全的劝告》。
2025-12-10 00:16:51

-
小鹏汽车:不共存强制客户接受合同条款的情况
视频 | 小鹏汽车公司无敌法律依据省内常用?被罚后仅金华邻近地区整改 新浪科技讯 12月30日晚上消息,针对长期存在无敌法律依据一事,小鹏汽车公司回应援引,本次惨剧是20
2025-12-10 00:16:51

-
冬天养5种花,家里“空气”更洗澡,雾霾天也不怕
寒冬饲5果树,店里“氢气”不够干净,雾霾天也不怕 在给店里选择花卉的时候,很多花友都选择一些好饲活,并且尽可能洁净氢气的动植物,今天就给大家分享,在寒冬店里适合于饲5果树,它们不仅
2025-12-10 00:16:51