From 9086da32327eff0001aa12c0fe7312583e0c5f21 Mon Sep 17 00:00:00 2001 From: ooooo <297872913@qq.com> Date: Sat, 7 Aug 2021 07:07:19 +0800 Subject: [PATCH 1/3] lazy loading need add volatile --- .../main/java/com/alibaba/cloud/nacos/NacosConfigManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java index e27b3e06b5..67eb9a956d 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java @@ -32,7 +32,7 @@ public class NacosConfigManager { private static final Logger log = LoggerFactory.getLogger(NacosConfigManager.class); - private static ConfigService service = null; + private static volatile ConfigService service = null; private NacosConfigProperties nacosConfigProperties; From 28593a2495b71e50e14f888d0153b02d4e241180 Mon Sep 17 00:00:00 2001 From: ooooo <297872913@qq.com> Date: Sat, 7 Aug 2021 08:31:21 +0800 Subject: [PATCH 2/3] NacosEnvironmentPostProcessor load nacos configurations as early as even before Spring loading logging system phase --- .../client/NacosEnvironmentPostProcessor.java | 36 +++++++++++++++++++ .../client/NacosPropertySourceLocator.java | 14 +++++++- .../main/resources/META-INF/spring.factories | 4 ++- .../nacos/NacosConfigurationNewTest.java | 2 +- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java new file mode 100644 index 0000000000..935deafaef --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java @@ -0,0 +1,36 @@ +package com.alibaba.cloud.nacos.client; + +import com.alibaba.cloud.nacos.NacosConfigManager; +import com.alibaba.cloud.nacos.NacosConfigProperties; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertySource; + +import static com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.NACOS_PROPERTY_SOURCE_NAME; + +/** + * @author ooooo + */ +public class NacosEnvironmentPostProcessor implements EnvironmentPostProcessor { + + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + Boolean eagerLoad = environment.getProperty(NacosConfigProperties.PREFIX + ".eagerLoad.enabled", Boolean.class, false); + if (!eagerLoad || environment.getPropertySources().contains(NACOS_PROPERTY_SOURCE_NAME)) { + return; + } + + Binder binder = Binder.get(environment); + NacosConfigProperties nacosConfigProperties = binder.bind(NacosConfigProperties.PREFIX, NacosConfigProperties.class).get(); + nacosConfigProperties.setEnvironment(environment); + nacosConfigProperties.init(); + + NacosConfigManager nacosConfigManager = new NacosConfigManager(nacosConfigProperties); + NacosPropertySourceLocator nacosPropertySourceLocator = new NacosPropertySourceLocator(nacosConfigManager); + PropertySource propertySource = nacosPropertySourceLocator.locate(environment); + environment.getPropertySources().addFirst(propertySource); + } +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java index b243aa9724..db6ef332aa 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java @@ -27,14 +27,19 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import javax.sound.sampled.Port; + /** * @author xiaojing * @author pbting @@ -45,7 +50,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { private static final Logger log = LoggerFactory .getLogger(NacosPropertySourceLocator.class); - private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS"; + protected static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS"; private static final String SEP1 = "-"; @@ -74,6 +79,13 @@ public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager) { @Override public PropertySource locate(Environment env) { + // org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize + // env must be the instance of ConfigurableEnvironment + ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) env; + if (configurableEnvironment.getPropertySources().contains(NACOS_PROPERTY_SOURCE_NAME)) { + return null; + } + nacosConfigProperties.setEnvironment(env); ConfigService configService = nacosConfigManager.getConfigService(); diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories index a7ee06a449..d27604d6aa 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories @@ -9,4 +9,6 @@ org.springframework.boot.env.PropertySourceLoader=\ com.alibaba.cloud.nacos.parser.NacosJsonPropertySourceLoader,\ com.alibaba.cloud.nacos.parser.NacosXmlPropertySourceLoader org.springframework.context.ApplicationListener=\ -com.alibaba.cloud.nacos.logging.NacosLoggingListener \ No newline at end of file +com.alibaba.cloud.nacos.logging.NacosLoggingListener +org.springframework.boot.env.EnvironmentPostProcessor=\ +com.alibaba.cloud.nacos.client.NacosEnvironmentPostProcessor \ No newline at end of file diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java index 7ec80f719b..05465a2395 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java @@ -55,7 +55,7 @@ @PrepareForTest({ NacosConfigService.class }) @SpringBootTest(classes = NacosConfigurationNewTest.TestConfig.class, properties = { "spring.application.name=myTestService1", "spring.profiles.active=dev,test", - "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", "spring.cloud.nacos.config.eagerLoad.enabled=true", "spring.cloud.nacos.config.namespace=test-namespace", "spring.cloud.nacos.config.encode=utf-8", "spring.cloud.nacos.config.timeout=1000", From 304a064e8046d0af4d0d491ae40e0c51e3d8854a Mon Sep 17 00:00:00 2001 From: ooooo <297872913@qq.com> Date: Sat, 7 Aug 2021 09:10:25 +0800 Subject: [PATCH 3/3] NacosEnvironmentPostProcessor load nacos configurations as early as even before Spring loading logging system phase --- .../client/NacosEnvironmentPostProcessor.java | 22 +++++++++++++------ .../client/NacosPropertySourceLocator.java | 7 ++---- .../nacos/NacosConfigurationNewTest.java | 3 ++- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java index 935deafaef..68cddbd837 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosEnvironmentPostProcessor.java @@ -2,6 +2,7 @@ import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.cloud.nacos.NacosConfigProperties; + import org.springframework.boot.SpringApplication; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.env.EnvironmentPostProcessor; @@ -15,22 +16,29 @@ */ public class NacosEnvironmentPostProcessor implements EnvironmentPostProcessor { - @Override - public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { - Boolean eagerLoad = environment.getProperty(NacosConfigProperties.PREFIX + ".eagerLoad.enabled", Boolean.class, false); - if (!eagerLoad || environment.getPropertySources().contains(NACOS_PROPERTY_SOURCE_NAME)) { + public void postProcessEnvironment(ConfigurableEnvironment environment, + SpringApplication application) { + Boolean eagerLoad = environment.getProperty( + NacosConfigProperties.PREFIX + ".eagerLoad.enabled", Boolean.class, + false); + if (!eagerLoad || environment.getPropertySources() + .contains(NACOS_PROPERTY_SOURCE_NAME)) { return; } Binder binder = Binder.get(environment); - NacosConfigProperties nacosConfigProperties = binder.bind(NacosConfigProperties.PREFIX, NacosConfigProperties.class).get(); + NacosConfigProperties nacosConfigProperties = binder + .bind(NacosConfigProperties.PREFIX, NacosConfigProperties.class).get(); nacosConfigProperties.setEnvironment(environment); nacosConfigProperties.init(); - NacosConfigManager nacosConfigManager = new NacosConfigManager(nacosConfigProperties); - NacosPropertySourceLocator nacosPropertySourceLocator = new NacosPropertySourceLocator(nacosConfigManager); + NacosConfigManager nacosConfigManager = new NacosConfigManager( + nacosConfigProperties); + NacosPropertySourceLocator nacosPropertySourceLocator = new NacosPropertySourceLocator( + nacosConfigManager); PropertySource propertySource = nacosPropertySourceLocator.locate(environment); environment.getPropertySources().addFirst(propertySource); } + } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java index db6ef332aa..988d2f87b0 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java @@ -27,8 +27,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; @@ -38,8 +36,6 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import javax.sound.sampled.Port; - /** * @author xiaojing * @author pbting @@ -82,7 +78,8 @@ public PropertySource locate(Environment env) { // org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize // env must be the instance of ConfigurableEnvironment ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) env; - if (configurableEnvironment.getPropertySources().contains(NACOS_PROPERTY_SOURCE_NAME)) { + if (configurableEnvironment.getPropertySources() + .contains(NACOS_PROPERTY_SOURCE_NAME)) { return null; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java index 05465a2395..936cff938a 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNewTest.java @@ -55,7 +55,8 @@ @PrepareForTest({ NacosConfigService.class }) @SpringBootTest(classes = NacosConfigurationNewTest.TestConfig.class, properties = { "spring.application.name=myTestService1", "spring.profiles.active=dev,test", - "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", "spring.cloud.nacos.config.eagerLoad.enabled=true", + "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.config.eagerLoad.enabled=true", "spring.cloud.nacos.config.namespace=test-namespace", "spring.cloud.nacos.config.encode=utf-8", "spring.cloud.nacos.config.timeout=1000",