diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java index 4b5e98f9..704c8a26 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java @@ -16,7 +16,12 @@ */ package com.alibaba.cloud.nacos; +import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.exception.NacosException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -25,7 +30,8 @@ import org.springframework.context.ApplicationContextAware; * @author liaochuntao */ public class NacosConfigManager implements ApplicationContextAware { - + private static final Logger log = LoggerFactory + .getLogger(NacosConfigManager.class); private ConfigService configService; public ConfigService getConfigService() { @@ -37,6 +43,11 @@ public class NacosConfigManager implements ApplicationContextAware { throws BeansException { NacosConfigProperties properties = applicationContext .getBean(NacosConfigProperties.class); - configService = properties.configServiceInstance(); + try { + configService = NacosFactory.createConfigService(properties.getConfigServiceProperties()); + properties.initConfigService(configService); + } catch (NacosException e) { + log.error("create config service error!properties={},e=,", properties, e); + } } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java index d03c399f..dc108633 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java @@ -403,13 +403,19 @@ public class NacosConfigProperties { + refreshableDataids + '\'' + ", extConfig=" + extConfig + '}'; } + /** + * @see NacosConfigManager#getConfigService() + */ @Deprecated public ConfigService configServiceInstance() { + return configService; + } - if (null != configService) { - return configService; - } + public void initConfigService(ConfigService configService){ + this.configService = configService; + } + public Properties getConfigServiceProperties(){ Properties properties = new Properties(); properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, "")); properties.put(ENCODE, Objects.toString(this.encode, "")); @@ -424,7 +430,6 @@ public class NacosConfigProperties { properties.put(CONFIG_RETRY_TIME, Objects.toString(this.configRetryTime, "")); properties.put(ENABLE_REMOTE_SYNC_CONFIG, Objects.toString(this.enableRemoteSyncConfig, "")); - String endpoint = Objects.toString(this.endpoint, ""); if (endpoint.contains(":")) { int index = endpoint.indexOf(":"); @@ -434,14 +439,7 @@ public class NacosConfigProperties { else { properties.put(ENDPOINT, endpoint); } - - try { - configService = NacosFactory.createConfigService(properties); - return configService; - } - catch (Exception e) { - log.error("create config service error!properties={},e=,", this, e); - return null; - } + return properties; } + } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java index 10a48cd8..35494afc 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java @@ -156,8 +156,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { String fileExtension = properties.getFileExtension(); String nacosGroup = properties.getGroup(); + // load directly once by default + 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; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, @@ -168,22 +173,35 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { private void loadNacosDataIfPresent(final CompositePropertySource composite, final String dataId, final String group, String fileExtension, boolean isRefreshable) { - if (NacosContextRefresher.getRefreshCount() != 0) { - NacosPropertySource ps; - if (!isRefreshable) { - ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); - } - else { - ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); - } + NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group, + fileExtension, isRefreshable); + this.addFirstPropertySource(composite, propertySource, false); + } - composite.addFirstPropertySource(ps); + private NacosPropertySource loadNacosPropertySource(final String dataId, + final String group, String fileExtension, boolean isRefreshable) { + if (NacosContextRefresher.getRefreshCount() != 0) { + if (!isRefreshable) { + return NacosPropertySourceRepository.getNacosPropertySource(dataId); + } } - else { - NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, - fileExtension, isRefreshable); - composite.addFirstPropertySource(ps); + return nacosPropertySourceBuilder.build(dataId, group, fileExtension, + isRefreshable); + } + + /** + * Add the nacos configuration to the first place and maybe ignore the empty + * configuration + */ + 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 static void checkDataIdFileExtension(String[] dataIdArray) { diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java index e3168c91..cc41bd36 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java @@ -24,8 +24,6 @@ import java.util.Properties; */ public class NacosDataParserHandler { - private static final NacosDataParserHandler HANDLER = new NacosDataParserHandler(); - private AbstractNacosDataParser parser; private NacosDataParserHandler() { @@ -68,7 +66,10 @@ public class NacosDataParserHandler { } public static NacosDataParserHandler getInstance() { - return HANDLER; + return ParserHandler.HANDLER; } + private static class ParserHandler { + private static final NacosDataParserHandler HANDLER = new NacosDataParserHandler(); + } }