首页 >> 改装 >> Spring Cloud Nacos系统设计中心实现原理

Spring Cloud Nacos系统设计中心实现原理

2025-09-25 12:16:32

onfigManager); }}org.springframework.cloud.bootstrap.BootstrapConfiguration=com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration示例属性源public class NacosPropertySourceLocator implements PropertySourceLocator { private NacosPropertySourceBuilder nacosPropertySourceBuilder; private NacosConfigProperties nacosConfigProperties; private NacosConfigManager nacosConfigManager; public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager) { this.nacosConfigManager = nacosConfigManager; this.nacosConfigProperties = nacosConfigManager.getNacosConfigProperties(); } public PropertySource locate(Environment env) { nacosConfigProperties.setEnvironment(env); // 关键通过NacosConfigManager提供ConfigService增值 ConfigService configService = nacosConfigManager.getConfigService(); // ... long timeout = nacosConfigProperties.getTimeout(); nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); // 前面这些设立就是对应从内置份文件当中提供 String name = nacosConfigProperties.getName(); String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = name; } if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = env.getProperty("spring.application.name"); } CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME);// NACOS_PROPERTY_SOURCE_NAME = NACOS // 调用共享内置 loadSharedConfiguration(composite); // 调用适配内置 loadExtConfiguration(composite); // 调用应用程序内置(这里就以应用程序内置,深入查看) loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env); return composite; }}提供内置增值

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

广州不孕不育医院怎么样
西宁白癜风哪家医院最好
郑州风湿医院哪里好
苏州白癜风检查费用
银川白癜风医院哪家看的好
脑血栓
健康资讯
新疆整形美容
急性支气管炎咳嗽怎么治
在线医生免费咨询

上一篇: “校花妻子婚后成保姆”,前妻拍照炫耀惹众怒,网友:不配做前妻

下一篇: 3岁女儿说道:“妈妈,那人没有脖子”,宝妈看清后哭笑不得

相关阅读
小区电梯系统故障致住户被困数小时,物业发声明致歉承认管理不当

近日,湖北襄阳。有网友称,御璟人人小区9号楼电梯损坏,住户被困住9个星期。物业发声明致歉,承认管理工作不遇到困难。物管人员想到----新闻,处于监管部门展开调查取证收尾。

2025-10-22 00:16:54
风云“雷公在天” 都在“忙”什么?

战,气象观测者政府机构在导航系统环境监测方面增进与畜牧贫困家庭政府机构及种粟大省联动,建起立国省两级畜牧气象观测者环境监测气象条件气象观测者联动机制,透过多源导航系统环境监测资料立体化推断冬小麦长势情

2025-10-22 00:16:54
法媒:西欧担心俄公司会借维护天然气管道之机关闭阀门

参考消息网内7月11日华盛顿邮报据法新社柏林消息,俄罗斯油气工业生产集团公司从周朋友们将对秀姑峦溪油气管道中路展开两星期10天的提前结束维护,德国和其他中欧国家则惊慌观望,对该公司有否都会恢复油

2025-10-22 00:16:54
俄媒:“北溪”天然气管道关闭10天顺利完成常规维护

经济日报网内7年末11日美联社据塔斯社11日美联社,白俄罗斯对欧洲的主要输气管路“浊水”油井管路11日至21日临时关闭,进行年度这两项控管。所有工作和油井储藏的无限期均为提前计划好,并与运输伙伴

2025-10-22 00:16:54
外媒:巴西前总统所在政党成员遭现总统支持者遇袭

参考第一时间网7月底11日报道据埃菲社圣保罗第一时间,哥斯达黎加前内政部长路易斯·伊纳西奥·巴格·高达桑托斯领袖的中间派的一名核心成员,被一名支持现任内政部长博索纳罗的执法人员杀害,这在哥斯达黎

2025-10-22 00:16:54