diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java index d04bb0cd..3833293e 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java @@ -56,9 +56,8 @@ public class NacosConfigAutoConfiguration { public NacosContextRefresher nacosContextRefresher( NacosConfigProperties nacosConfigProperties, NacosRefreshProperties nacosRefreshProperties, - NacosRefreshHistory refreshHistory, - NacosPropertySourceRepository propertySourceRepository) { + NacosRefreshHistory refreshHistory) { return new NacosContextRefresher(nacosRefreshProperties, refreshHistory, - propertySourceRepository, nacosConfigProperties.configServiceInstance()); + nacosConfigProperties.configServiceInstance()); } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java index 1c770f64..076d884f 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java @@ -38,9 +38,4 @@ public class NacosConfigBootstrapConfiguration { return new NacosConfigProperties(); } - @Bean - public NacosPropertySourceRepository nacosPropertySourceRepository() { - return new NacosPropertySourceRepository(); - } - } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java index 19d5721f..7483a7fc 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java @@ -16,24 +16,21 @@ package org.springframework.cloud.alibaba.nacos; -import static com.alibaba.nacos.api.PropertyKeyConst.*; +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.config.ConfigService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.core.env.Environment; +import javax.annotation.PostConstruct; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Properties; -import javax.annotation.PostConstruct; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.core.env.Environment; - -import com.alibaba.nacos.api.NacosFactory; -import com.alibaba.nacos.api.config.ConfigService; +import static com.alibaba.nacos.api.PropertyKeyConst.*; /** * nacos properties @@ -110,7 +107,6 @@ public class NacosConfigProperties { */ private String clusterName; - @Value("${spring.application.name}") private String name; private String[] activeProfiles; @@ -271,6 +267,10 @@ public class NacosConfigProperties { this.extConfig = extConfig; } + public void setActiveProfiles(String[] activeProfiles) { + this.activeProfiles = activeProfiles; + } + public static class Config { /** * the data id of extended configuration @@ -348,4 +348,5 @@ public class NacosConfigProperties { return null; } } + } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceRepository.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceRepository.java index ad8fc963..f29ab80a 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceRepository.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceRepository.java @@ -27,19 +27,24 @@ import java.util.concurrent.ConcurrentHashMap; */ public class NacosPropertySourceRepository { - private ConcurrentHashMap nacosPropertySourceRepository = new ConcurrentHashMap<>(); + private final static ConcurrentHashMap nacosPropertySourceRepository = new ConcurrentHashMap<>(); /** * @return all nacos properties from application context */ - public List getAll() { + public static List getAll() { List result = new ArrayList<>(); result.addAll(nacosPropertySourceRepository.values()); return result; } - public void collectNacosPropertySources(NacosPropertySource nacosPropertySource) { + public static void collectNacosPropertySources( + NacosPropertySource nacosPropertySource) { nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(), nacosPropertySource); } + + public static NacosPropertySource getNacosPropertySource(String dataId) { + return nacosPropertySourceRepository.get(dataId); + } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java index 58122006..b900a674 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java @@ -37,7 +37,6 @@ public class NacosPropertySourceBuilder { .getLogger(NacosPropertySourceBuilder.class); private static final Properties EMPTY_PROPERTIES = new Properties(); - private NacosPropertySourceRepository nacosPropertySourceRepository; private ConfigService configService; private long timeout; @@ -74,7 +73,7 @@ public class NacosPropertySourceBuilder { } NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId, propertiesToMap(p), new Date(), isRefreshable); - nacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource); + NacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource); return nacosPropertySource; } @@ -128,12 +127,4 @@ public class NacosPropertySourceBuilder { return result; } - public NacosPropertySourceRepository getNacosPropertySourceRepository() { - return nacosPropertySourceRepository; - } - - public void setNacosPropertySourceRepository( - NacosPropertySourceRepository nacosPropertySourceRepository) { - this.nacosPropertySourceRepository = nacosPropertySourceRepository; - } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index 99e010ea..aa9450cc 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.alibaba.nacos.NacosConfigProperties; import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository; +import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; @@ -51,9 +52,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { @Autowired private NacosConfigProperties nacosConfigProperties; - @Autowired - private NacosPropertySourceRepository nacosPropertySourceRepository; - public NacosPropertySourceLocator() { } @@ -72,8 +70,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { long timeout = nacosConfigProperties.getTimeout(); nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); - nacosPropertySourceBuilder - .setNacosPropertySourceRepository(nacosPropertySourceRepository); String name = nacosConfigProperties.getName(); String nacosGroup = nacosConfigProperties.getGroup(); @@ -82,6 +78,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { dataIdPrefix = name; } + if (StringUtils.isEmpty(dataIdPrefix)) { + dataIdPrefix = env.getProperty("spring.application.name"); + } + + List profiles = Arrays.asList(env.getActiveProfiles()); + nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0])); + String fileExtension = nacosConfigProperties.getFileExtension(); CompositePropertySource composite = new CompositePropertySource( @@ -164,9 +167,20 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { private void loadNacosDataIfPresent(final CompositePropertySource composite, final String dataId, final String group, String fileExtension, boolean isRefreshable) { - NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, - fileExtension, isRefreshable); - if (ps != null) { + if (NacosContextRefresher.loadCount.get() != 0) { + NacosPropertySource ps; + if (!isRefreshable) { + ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); + } + else { + ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); + } + + composite.addFirstPropertySource(ps); + } + else { + NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, + fileExtension, isRefreshable); composite.addFirstPropertySource(ps); } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpoint.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpoint.java index 02db85af..1160db1b 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpoint.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpoint.java @@ -16,13 +16,6 @@ package org.springframework.cloud.alibaba.nacos.endpoint; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.cloud.alibaba.nacos.NacosConfigProperties; @@ -30,6 +23,13 @@ import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository; import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource; import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * Endpoint for Nacos, contains config data and refresh history * @author xiaojing @@ -41,15 +41,12 @@ public class NacosConfigEndpoint { private final NacosRefreshHistory refreshHistory; - private final NacosPropertySourceRepository propertySourceRepository; - private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - public NacosConfigEndpoint(NacosConfigProperties properties, NacosRefreshHistory refreshHistory, - NacosPropertySourceRepository propertySourceRepository) { + public NacosConfigEndpoint(NacosConfigProperties properties, + NacosRefreshHistory refreshHistory) { this.properties = properties; this.refreshHistory = refreshHistory; - this.propertySourceRepository = propertySourceRepository; } @ReadOperation @@ -57,7 +54,7 @@ public class NacosConfigEndpoint { Map result = new HashMap<>(16); result.put("NacosConfigProperties", properties); - List all = propertySourceRepository.getAll(); + List all = NacosPropertySourceRepository.getAll(); List> sources = new ArrayList<>(); for (NacosPropertySource ps : all) { diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java index c0d92632..e853546d 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java @@ -19,12 +19,10 @@ package org.springframework.cloud.alibaba.nacos.endpoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.cloud.alibaba.nacos.NacosConfigProperties; -import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository; import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory; import org.springframework.context.annotation.Bean; @@ -41,22 +39,16 @@ public class NacosConfigEndpointAutoConfiguration { @Autowired private NacosRefreshHistory nacosRefreshHistory; - @Autowired - private NacosPropertySourceRepository nacosPropertySourceRepository; - @ConditionalOnMissingBean @ConditionalOnEnabledEndpoint @Bean public NacosConfigEndpoint nacosConfigEndpoint() { - return new NacosConfigEndpoint(nacosConfigProperties, nacosRefreshHistory, - nacosPropertySourceRepository); + return new NacosConfigEndpoint(nacosConfigProperties, nacosRefreshHistory); } @Bean - public NacosConfigHealthIndicator nacosConfigHealthIndicator( - NacosPropertySourceRepository nacosPropertySourceRepository) { + public NacosConfigHealthIndicator nacosConfigHealthIndicator() { return new NacosConfigHealthIndicator(nacosConfigProperties, - nacosPropertySourceRepository, nacosConfigProperties.configServiceInstance()); } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigHealthIndicator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigHealthIndicator.java index 41796157..491b07bf 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigHealthIndicator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigHealthIndicator.java @@ -16,11 +16,7 @@ package org.springframework.cloud.alibaba.nacos.endpoint; -import java.util.ArrayList; -import java.util.List; - import com.alibaba.nacos.api.config.ConfigService; - import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; import org.springframework.cloud.alibaba.nacos.NacosConfigProperties; @@ -28,6 +24,9 @@ import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository; import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource; import org.springframework.util.StringUtils; +import java.util.ArrayList; +import java.util.List; + /** * @author xiaojing */ @@ -35,21 +34,17 @@ public class NacosConfigHealthIndicator extends AbstractHealthIndicator { private final NacosConfigProperties nacosConfigProperties; - private final NacosPropertySourceRepository nacosPropertySourceRepository; - private final List dataIds; private final ConfigService configService; public NacosConfigHealthIndicator(NacosConfigProperties nacosConfigProperties, - NacosPropertySourceRepository nacosPropertySourceRepository, ConfigService configService) { this.nacosConfigProperties = nacosConfigProperties; - this.nacosPropertySourceRepository = nacosPropertySourceRepository; this.configService = configService; this.dataIds = new ArrayList<>(); - for (NacosPropertySource nacosPropertySource : this.nacosPropertySourceRepository + for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository .getAll()) { this.dataIds.add(nacosPropertySource.getDataId()); } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java index 117c70bb..c726f948 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java @@ -38,6 +38,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; /** * On application start up, NacosContextRefresher add nacos listeners to all application @@ -53,34 +54,31 @@ public class NacosContextRefresher private final static Logger LOGGER = LoggerFactory .getLogger(NacosContextRefresher.class); + public static final AtomicLong loadCount = new AtomicLong(0); + private final NacosRefreshProperties refreshProperties; private final NacosRefreshHistory refreshHistory; - private final NacosPropertySourceRepository nacosPropertySourceRepository; - private final ConfigService configService; private ApplicationContext applicationContext; - private AtomicBoolean ready = new AtomicBoolean(true); + private AtomicBoolean ready = new AtomicBoolean(false); private Map listenerMap = new ConcurrentHashMap<>(16); public NacosContextRefresher(NacosRefreshProperties refreshProperties, - NacosRefreshHistory refreshHistory, - NacosPropertySourceRepository nacosPropertySourceRepository, - ConfigService configService) { + NacosRefreshHistory refreshHistory, ConfigService configService) { this.refreshProperties = refreshProperties; this.refreshHistory = refreshHistory; - this.nacosPropertySourceRepository = nacosPropertySourceRepository; this.configService = configService; } @Override public void onApplicationEvent(ApplicationReadyEvent event) { // many Spring context - if (this.ready.compareAndSet(true, false)) { + if (this.ready.compareAndSet(false, true)) { this.registerNacosListenersForApplications(); } } @@ -92,7 +90,7 @@ public class NacosContextRefresher private void registerNacosListenersForApplications() { if (refreshProperties.isEnabled()) { - for (NacosPropertySource nacosPropertySource : nacosPropertySourceRepository + for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository .getAll()) { if (!nacosPropertySource.isRefreshable()) { @@ -110,6 +108,7 @@ public class NacosContextRefresher Listener listener = listenerMap.computeIfAbsent(dataId, i -> new Listener() { @Override public void receiveConfigInfo(String configInfo) { + loadCount.incrementAndGet(); String md5 = ""; if (!StringUtils.isEmpty(configInfo)) { try { diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/nacos/NacosParameterInitListener.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/nacos/NacosParameterInitListener.java index 60945754..73dcc75d 100644 --- a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/nacos/NacosParameterInitListener.java +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/nacos/NacosParameterInitListener.java @@ -3,27 +3,33 @@ package org.springframework.cloud.alicloud.context.nacos; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; -import org.springframework.context.ApplicationListener; +import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener; import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration; import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory; public class NacosParameterInitListener - implements ApplicationListener { + extends AbstractOnceApplicationListener { private static final Logger log = LoggerFactory .getLogger(NacosParameterInitListener.class); @Override - public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + protected String conditionalOnClass() { + return "org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration"; + } + @Override + protected void handleEvent(ApplicationEnvironmentPreparedEvent event) { preparedNacosConfiguration(); } private void preparedNacosConfiguration() { EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory .getEdasChangeOrderConfiguration(); + log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.", edasChangeOrderConfiguration.isEdasManaged()); + if (!edasChangeOrderConfiguration.isEdasManaged()) { return; }