您现在的位置:首页 >> 选购知识

Spring Cloud Nacos内置中心实现原理

发布时间:2025/09/26 12:16    来源:涡阳家居装修网

(nacosConfigManager); }}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

郑州男科医院
南京看白癜风到哪家医院
甘肃男科医院哪家看的好
潮州白癜风正规的医院
上海看白癜风去哪里比较好
耳鼻喉科
6月底至7月初是新冠流行高峰期?感染后抗病毒治疗很重要!
信息频道
流鼻血怎么办
减肥

上一篇: 吴金贵:我们努力渴望青春的感觉 但风暴还谈不上

下一篇: 美国猴痘传染病增加 专家警告勿大意

友情链接