Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loading need add volatile and NacosEnvironmentPostProcessor load nacos configurations #2207

Open
wants to merge 4 commits into
base: 2.2.x-ospp2023
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand All @@ -45,7 +46,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 = "-";

Expand Down Expand Up @@ -74,6 +75,14 @@ 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
com.alibaba.cloud.nacos.logging.NacosLoggingListener
org.springframework.boot.env.EnvironmentPostProcessor=\
com.alibaba.cloud.nacos.client.NacosEnvironmentPostProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
@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.namespace=test-namespace",
"spring.cloud.nacos.config.encode=utf-8",
"spring.cloud.nacos.config.timeout=1000",
Expand Down