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 163c34171..d04bb0cdf 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 @@ -42,11 +42,6 @@ public class NacosConfigAutoConfiguration { return nacosConfigProperties; } - @Bean - public NacosPropertySourceRepository nacosPropertySourceRepository() { - return new NacosPropertySourceRepository(); - } - @Bean public NacosRefreshProperties nacosRefreshProperties() { return new NacosRefreshProperties(); 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 1be9157f4..1c770f64d 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 @@ -28,9 +28,8 @@ import org.springframework.context.annotation.Configuration; public class NacosConfigBootstrapConfiguration { @Bean - public NacosPropertySourceLocator nacosPropertySourceLocator( - NacosConfigProperties nacosConfigProperties) { - return new NacosPropertySourceLocator(nacosConfigProperties); + public NacosPropertySourceLocator nacosPropertySourceLocator() { + return new NacosPropertySourceLocator(); } @Bean @@ -38,4 +37,10 @@ public class NacosConfigBootstrapConfiguration { public NacosConfigProperties nacosConfigProperties() { 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/NacosPropertySourceRepository.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceRepository.java index 47075ca12..ad8fc9639 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 @@ -16,54 +16,30 @@ package org.springframework.cloud.alibaba.nacos; +import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource; + import java.util.ArrayList; import java.util.List; - -import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.CompositePropertySource; -import org.springframework.core.env.PropertySource; +import java.util.concurrent.ConcurrentHashMap; /** * @author xiaojing */ -public class NacosPropertySourceRepository implements ApplicationContextAware { - - private ApplicationContext applicationContext; +public class NacosPropertySourceRepository { - @Override - public void setApplicationContext(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } + private ConcurrentHashMap nacosPropertySourceRepository = new ConcurrentHashMap<>(); /** * @return all nacos properties from application context */ public List getAll() { List result = new ArrayList<>(); - ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext; - for (PropertySource p : ctx.getEnvironment().getPropertySources()) { - if (p instanceof NacosPropertySource) { - result.add((NacosPropertySource) p); - } - else if (p instanceof CompositePropertySource) { - collectNacosPropertySources((CompositePropertySource) p, result); - } - } + result.addAll(nacosPropertySourceRepository.values()); return result; } - private void collectNacosPropertySources(CompositePropertySource composite, - List result) { - for (PropertySource p : composite.getPropertySources()) { - if (p instanceof NacosPropertySource) { - result.add((NacosPropertySource) p); - } - else if (p instanceof CompositePropertySource) { - collectNacosPropertySources((CompositePropertySource) p, result); - } - } + public void collectNacosPropertySources(NacosPropertySource nacosPropertySource) { + nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(), + nacosPropertySource); } } 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 aed331805..58122006a 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 @@ -16,17 +16,17 @@ package org.springframework.cloud.alibaba.nacos.client; -import java.io.StringReader; -import java.util.*; - +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.factory.config.YamlPropertiesFactoryBean; +import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository; import org.springframework.core.io.ByteArrayResource; import org.springframework.util.StringUtils; -import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.exception.NacosException; +import java.io.StringReader; +import java.util.*; /** * @author xiaojing @@ -35,6 +35,9 @@ import com.alibaba.nacos.api.exception.NacosException; public class NacosPropertySourceBuilder { private static final Logger LOGGER = LoggerFactory .getLogger(NacosPropertySourceBuilder.class); + private static final Properties EMPTY_PROPERTIES = new Properties(); + + private NacosPropertySourceRepository nacosPropertySourceRepository; private ConfigService configService; private long timeout; @@ -67,10 +70,12 @@ public class NacosPropertySourceBuilder { boolean isRefreshable) { Properties p = loadNacosData(dataId, group, fileExtension); if (p == null) { - return null; + p = EMPTY_PROPERTIES; } - return new NacosPropertySource(group, dataId, propertiesToMap(p), new Date(), - isRefreshable); + NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId, + propertiesToMap(p), new Date(), isRefreshable); + nacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource); + return nacosPropertySource; } private Properties loadNacosData(String dataId, String group, String fileExtension) { @@ -122,4 +127,13 @@ 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 68b1981dd..99e010ea8 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 @@ -16,12 +16,12 @@ package org.springframework.cloud.alibaba.nacos.client; -import java.util.Arrays; -import java.util.List; - +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.cloud.alibaba.nacos.NacosConfigProperties; +import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; @@ -29,7 +29,8 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.util.StringUtils; -import com.alibaba.nacos.api.config.ConfigService; +import java.util.Arrays; +import java.util.List; /** * @author xiaojing @@ -47,10 +48,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { private static final List SUPPORT_FILE_EXTENSION = Arrays.asList("properties", "yaml", "yml"); + @Autowired private NacosConfigProperties nacosConfigProperties; - public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) { - this.nacosConfigProperties = nacosConfigProperties; + @Autowired + private NacosPropertySourceRepository nacosPropertySourceRepository; + + public NacosPropertySourceLocator() { } private NacosPropertySourceBuilder nacosPropertySourceBuilder; @@ -68,7 +72,8 @@ 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(); 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 4574ede25..117c70bb0 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 @@ -16,15 +16,9 @@ package org.springframework.cloud.alibaba.nacos.refresh; -import java.io.UnsupportedEncodingException; -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executor; -import java.util.concurrent.atomic.AtomicBoolean; - +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.listener.Listener; +import com.alibaba.nacos.api.exception.NacosException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationReadyEvent; @@ -36,9 +30,14 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.util.StringUtils; -import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.config.listener.Listener; -import com.alibaba.nacos.api.exception.NacosException; +import java.io.UnsupportedEncodingException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; /** * On application start up, NacosContextRefresher add nacos listeners to all application @@ -81,9 +80,8 @@ public class NacosContextRefresher @Override public void onApplicationEvent(ApplicationReadyEvent event) { // many Spring context - if (this.ready.get()) { + if (this.ready.compareAndSet(true, false)) { this.registerNacosListenersForApplications(); - this.ready.compareAndSet(true, false); } }