<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
SpringBoot啟動時,會執行這個方法:SpringApplication#run,這個方法中會調prepareContext來準備上下文,這個方法中呼叫了applyInitializers方法來執行實現了ApplicationContextInitializer介面的類的initialize方法。其中包括PropertySourceBootstrapConfiguration#initialize 來載入外部的設定。
@Autowired(required = false) private List<PropertySourceLocator> propertySourceLocators = new ArrayList<>(); public void initialize(ConfigurableApplicationContext applicationContext) { ... for (PropertySourceLocator locator : this.propertySourceLocators) { //載入PropertySource Collection<PropertySource<?>> source = locator.locateCollection(environment); ... } ... } //org.springframework.cloud.bootstrap.config.PropertySourceLocator#locateCollection default Collection<PropertySource<?>> locateCollection(Environment environment) { return locateCollection(this, environment); } //org.springframework.cloud.bootstrap.config.PropertySourceLocator#locateCollection static Collection<PropertySource<?>> locateCollection(PropertySourceLocator locator, Environment environment) { //執行locate方法,將PropertySource載入進來 PropertySource<?> propertySource = locator.locate(environment); ... }
這個類中會注入實現了PropertySourceLocator介面的類,在nacos中是NacosPropertySourceLocator。
在initialize方法中會執行NacosPropertySourceLocator的locate方法,將NacosPropertySource載入進來。
NacosPropertySourceLocator在設定類NacosConfigBootstrapConfiguration中註冊。
@Configuration(proxyBeanMethods = false) @ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true) public class NacosConfigBootstrapConfiguration { @Bean @ConditionalOnMissingBean public NacosConfigProperties nacosConfigProperties() { return new NacosConfigProperties(); } @Bean @ConditionalOnMissingBean public NacosConfigManager nacosConfigManager( NacosConfigProperties nacosConfigProperties) { return new NacosConfigManager(nacosConfigProperties); } @Bean public NacosPropertySourceLocator nacosPropertySourceLocator( NacosConfigManager nacosConfigManager) { return new NacosPropertySourceLocator(nacosConfigManager); } }
在這裡會依次註冊NacosConfigProperties,NacosConfigManager,NacosPropertySourceLocator。
//com.alibaba.cloud.nacos.client.NacosPropertySourceLocator#locate public PropertySource<?> locate(Environment env) { nacosConfigProperties.setEnvironment(env); //獲取ConfigService ConfigService configService = nacosConfigManager.getConfigService(); if (null == configService) { log.warn("no instance of config service found, can't load config from nacos"); return null; } long timeout = nacosConfigProperties.getTimeout(); //構建nacosPropertySourceBuilder nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); String name = nacosConfigProperties.getName(); //獲取dataIdPrefix,一次從prefix,name,spring.application.name中獲取 String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = name; } if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = env.getProperty("spring.application.name"); } //構建CompositePropertySource:NACOS CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); //載入share 設定 loadSharedConfiguration(composite); //載入extention 設定 loadExtConfiguration(composite); //載入application設定 loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env); return composite; }
private void loadSharedConfiguration( CompositePropertySource compositePropertySource) { //獲取share節點的設定資訊 List<NacosConfigProperties.Config> sharedConfigs = nacosConfigProperties .getSharedConfigs(); //如果不為空,載入 if (!CollectionUtils.isEmpty(sharedConfigs)) { checkConfiguration(sharedConfigs, "shared-configs"); loadNacosConfiguration(compositePropertySource, sharedConfigs); } }
載入設定,公用方法
//com.alibaba.cloud.nacos.client.NacosPropertySourceLocator#loadNacosConfiguration private void loadNacosConfiguration(final CompositePropertySource composite, List<NacosConfigProperties.Config> configs) { for (NacosConfigProperties.Config config : configs) { loadNacosDataIfPresent(composite, config.getDataId(), config.getGroup(), NacosDataParserHandler.getInstance() .getFileExtension(config.getDataId()), config.isRefresh()); } }
//com.alibaba.cloud.nacos.client.NacosPropertySourceLocator#loadNacosDataIfPresent 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; } //載入NacosPropertySource,後面也會用這個方法 NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group, fileExtension, isRefreshable); //將NacosPropertySource放入第一個 this.addFirstPropertySource(composite, propertySource, false); }
載入NacosPropertySource
//com.alibaba.cloud.nacos.client.NacosPropertySourceLocator#loadNacosPropertySource private NacosPropertySource loadNacosPropertySource(final String dataId, final String group, String fileExtension, boolean isRefreshable) { //標註@RefreshScope的類的數量不為0,且不允許自動重新整理,從快取載入nacos的設定 if (NacosContextRefresher.getRefreshCount() != 0) { if (!isRefreshable) { return NacosPropertySourceRepository.getNacosPropertySource(dataId, group); } } return nacosPropertySourceBuilder.build(dataId, group, fileExtension, isRefreshable); }
從快取中載入
//NacosPropertySourceRepository private final static ConcurrentHashMap<String, NacosPropertySource> NACOS_PROPERTY_SOURCE_REPOSITORY = new ConcurrentHashMap<>(); public static NacosPropertySource getNacosPropertySource(String dataId, String group) { return NACOS_PROPERTY_SOURCE_REPOSITORY.get(getMapKey(dataId, group)); } public static String getMapKey(String dataId, String group) { return String.join(NacosConfigProperties.COMMAS, String.valueOf(dataId), String.valueOf(group)); }
NacosPropertySourceRepository中快取了NacosPropertySource
獲取設定並加入快取
//com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder#build NacosPropertySource build(String dataId, String group, String fileExtension, boolean isRefreshable) { //獲取設定 List<PropertySource<?>> propertySources = loadNacosData(dataId, group, fileExtension); //包裝成NacosPropertySource NacosPropertySource nacosPropertySource = new NacosPropertySource(propertySources, group, dataId, new Date(), isRefreshable); //加入快取 NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource); return nacosPropertySource; }
//com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder#loadNacosData private List<PropertySource<?>> loadNacosData(String dataId, String group, String fileExtension) { String data = null; try { //獲取設定 data = configService.getConfig(dataId, group, timeout); if (StringUtils.isEmpty(data)) { log.warn( "Ignore the empty nacos configuration and get it based on dataId[{}] & group[{}]", dataId, group); return Collections.emptyList(); } if (log.isDebugEnabled()) { log.debug(String.format( "Loading nacos data, dataId: '%s', group: '%s', data: %s", dataId, group, data)); } return NacosDataParserHandler.getInstance().parseNacosData(dataId, data, fileExtension); } catch (NacosException e) { log.error("get data from Nacos error,dataId:{} ", dataId, e); } catch (Exception e) { log.error("parse data from Nacos error,dataId:{},data:{}", dataId, data, e); } return Collections.emptyList(); }
//com.alibaba.nacos.client.config.NacosConfigService#getConfig public String getConfig(String dataId, String group, long timeoutMs) throws NacosException { return getConfigInner(namespace, dataId, group, timeoutMs); }
//com.alibaba.nacos.client.config.NacosConfigService#getConfigInner private String getConfigInner(String tenant, String dataId, String group, long timeoutMs) throws NacosException { group = blank2defaultGroup(group); ParamUtils.checkKeyParam(dataId, group); //宣告響應 ConfigResponse cr = new ConfigResponse(); //設定dataId,tenant,group cr.setDataId(dataId); cr.setTenant(tenant); cr.setGroup(group); // use local config first //先從本地快取中載入 String content = LocalConfigInfoProcessor.getFailover(worker.getAgentName(), dataId, group, tenant); if (content != null) { LOGGER.warn("[{}] [get-config] get failover ok, dataId={}, group={}, tenant={}, config={}", worker.getAgentName(), dataId, group, tenant, ContentUtils.truncateContent(content)); cr.setContent(content); String encryptedDataKey = LocalEncryptedDataKeyProcessor .getEncryptDataKeyFailover(agent.getName(), dataId, group, tenant); cr.setEncryptedDataKey(encryptedDataKey); configFilterChainManager.doFilter(null, cr); content = cr.getContent(); return content; } try { //從伺服器獲取設定 ConfigResponse response = worker.getServerConfig(dataId, group, tenant, timeoutMs, false); cr.setContent(response.getContent()); cr.setEncryptedDataKey(response.getEncryptedDataKey()); configFilterChainManager.doFilter(null, cr); content = cr.getContent(); return content; } catch (NacosException ioe) { if (NacosException.NO_RIGHT == ioe.getErrCode()) { throw ioe; } LOGGER.warn("[{}] [get-config] get from server error, dataId={}, group={}, tenant={}, msg={}", worker.getAgentName(), dataId, group, tenant, ioe.toString()); } LOGGER.warn("[{}] [get-config] get snapshot ok, dataId={}, group={}, tenant={}, config={}", worker.getAgentName(), dataId, group, tenant, ContentUtils.truncateContent(content)); content = LocalConfigInfoProcessor.getSnapshot(worker.getAgentName(), dataId, group, tenant); cr.setContent(content); String encryptedDataKey = LocalEncryptedDataKeyProcessor .getEncryptDataKeyFailover(agent.getName(), dataId, group, tenant); cr.setEncryptedDataKey(encryptedDataKey); configFilterChainManager.doFilter(null, cr); content = cr.getContent(); return content; }
將NacosPropertySource加入composite的第一個
//com.alibaba.cloud.nacos.client.NacosPropertySourceLocator#addFirstPropertySource 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); }
private void loadExtConfiguration(CompositePropertySource compositePropertySource) { //獲取extention節點的設定資訊 List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties .getExtensionConfigs(); //如果不為空,載入 if (!CollectionUtils.isEmpty(extConfigs)) { checkConfiguration(extConfigs, "extension-configs"); loadNacosConfiguration(compositePropertySource, extConfigs); } }
private void loadApplicationConfiguration( CompositePropertySource compositePropertySource, String dataIdPrefix, NacosConfigProperties properties, Environment environment) { //獲取副檔名 String fileExtension = properties.getFileExtension(); //獲取group String nacosGroup = properties.getGroup(); // load directly once by default //載入nacos的設定 loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup, fileExtension, true); // 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 for (String profile : environment.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; //載入帶profile,檔案格式字尾的設定,優先順序高於上一個 loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension, true); } }
這裡會載入至少三個nacos上面的組態檔,按優先順序依次為application,application.yaml,application-dev.yaml。
到此這篇關於Spring Cloud Alibaba Nacos Config載入設定詳解流程的文章就介紹到這了,更多相關Spring Cloud Alibaba Nacos Config 內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45