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

add a config adjust the property source overriden behavior #4377

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e23f628
feat: add a config adjust the property source overriden behavior
shenhuaxin May 23, 2022
b6dafd2
feat: add a config adjust the property source overriden behavior
shenhuaxin May 23, 2022
6d4b5d8
feat: add a config adjust the property source overriden behavior
shenhuaxin May 24, 2022
4d1d27d
feat: add a config adjust the property source overriden behavior
shenhuaxin May 24, 2022
6e85efa
feat: add a config adjust the property source overriden behavior
shenhuaxin May 24, 2022
b235a98
feat: add a config adjust the property source overriden behavior for …
shenhuaxin May 25, 2022
27be3f9
fix: change property name
shenhuaxin May 28, 2022
6bcb67d
refactor: remove isOverrideSystemProperties()
shenhuaxin May 28, 2022
0975296
refactor: remove isOverrideSystemProperties()
shenhuaxin May 28, 2022
8a4930f
test: remove isOverrideSystemProperties()
shenhuaxin May 28, 2022
5db5b4d
Update apollo-client/src/main/resources/META-INF/additional-spring-co…
shenhuaxin May 29, 2022
8b3f899
Merge branch 'master' into override-system-properties
shenhuaxin May 29, 2022
cada7d1
fix: rename override system properties, original name is not valid
shenhuaxin May 31, 2022
60c15ce
docs: add apollo.override-system-properties docs
shenhuaxin May 31, 2022
92fc457
fix the delete appnamespace failed issue
nobodyiam May 29, 2022
6360408
Bump gson from 2.8.0 to 2.8.9
dependabot[bot] May 28, 2022
f5d1092
Bump okhttp3 from 3.11.0 to 4.9.3
shoothzj Jun 2, 2022
56be420
Update apollo-open-api-platform.md
lepdou Jun 4, 2022
e357378
Update apollo-open-api-platform.md
lepdou Jun 4, 2022
5d1d261
bump version to 2.1.0-SNAPSHOT (#4403)
nobodyiam Jun 9, 2022
cfa9333
add a config adjust the property source overriden behavior
shenhuaxin Jun 10, 2022
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 @@ -39,6 +39,7 @@
import org.springframework.core.Ordered;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;

/**
* Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
Expand Down Expand Up @@ -140,7 +141,12 @@ protected void initialize(ConfigurableEnvironment environment) {

composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
}

if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
if (!isOverrideSystemProperties(environment)) {
shenhuaxin marked this conversation as resolved.
Show resolved Hide resolved
environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
return;
}
}
environment.getPropertySources().addFirst(composite);
}

Expand Down Expand Up @@ -215,4 +221,9 @@ public int getOrder() {
public void setOrder(int order) {
this.order = order;
}


private boolean isOverrideSystemProperties(ConfigurableEnvironment environment) {
return environment.getProperty(PropertySourcesConstants.APOLLO_OVERRIDE_SYSTEM_PROPERTIES, Boolean.class, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public interface PropertySourcesConstants {
String APOLLO_BOOTSTRAP_ENABLED = "apollo.bootstrap.enabled";
String APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED = "apollo.bootstrap.eagerLoad.enabled";
String APOLLO_BOOTSTRAP_NAMESPACES = "apollo.bootstrap.namespaces";
String APOLLO_OVERRIDE_SYSTEM_PROPERTIES = "apollo.bootstrap.overrideSystemProperties";
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.*;

/**
* Apollo Property Sources processor for Spring Annotation Based Application. <br /> <br />
Expand Down Expand Up @@ -110,12 +106,20 @@ private void initializePropertySources() {
if (environment.getPropertySources()
.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {

// ensure ApolloBootstrapPropertySources is still the first
ensureBootstrapPropertyPrecedence(environment);
if (isOverrideSystemProperties(environment)) {
// ensure ApolloBootstrapPropertySources is still the first
ensureBootstrapPropertyPrecedence(environment);
}

environment.getPropertySources()
.addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite);
} else {
if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
if (!isOverrideSystemProperties(environment)) {
environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
return;
}
}
environment.getPropertySources().addFirst(composite);
}
}
Expand Down Expand Up @@ -149,6 +153,10 @@ private void initializeAutoUpdatePropertiesFeature(ConfigurableListableBeanFacto
}
}

private Boolean isOverrideSystemProperties(ConfigurableEnvironment environment) {
return environment.getProperty(PropertySourcesConstants.APOLLO_OVERRIDE_SYSTEM_PROPERTIES, Boolean.class, true);
}

@Override
public void setEnvironment(Environment environment) {
//it is safe enough to cast as all known environment is derived from ConfigurableEnvironment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
"description": "enable apollo initialization before logging system initialization.",
"defaultValue": false
},
{
"name": "apollo.bootstrap.overrideSystemProperties",
shenhuaxin marked this conversation as resolved.
Show resolved Hide resolved
"type": "java.lang.Boolean",
"sourceType": "com.ctrip.framework.apollo.spring.config.PropertySourcesConstants",
shenhuaxin marked this conversation as resolved.
Show resolved Hide resolved
"description": "enable apollo server config override system properties.",
"defaultValue": true
},
{
"name": "apollo.cache-dir",
"type": "java.lang.String",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
import org.junit.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.util.Properties;

public class ApolloApplicationContextInitializerTest {

Expand Down Expand Up @@ -143,4 +147,24 @@ public void testPropertyNamesCacheEnabled() {
assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
assertTrue(propertySources.iterator().next() instanceof CachedCompositePropertySource);
}

@Test
public void testOverrideSystemProperties() {
Properties properties = new Properties();
properties.setProperty("server.port", "8080");
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);

MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(new PropertiesPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, properties));

when(environment.getPropertySources()).thenReturn(propertySources);
when(environment.getProperty(PropertySourcesConstants.APOLLO_OVERRIDE_SYSTEM_PROPERTIES, Boolean.class, true)).thenReturn(false);
when(environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES,
ConfigConsts.NAMESPACE_APPLICATION)).thenReturn("");

apolloApplicationContextInitializer.initialize(environment);

assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
assertEquals(propertySources.iterator().next().getName(), StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
}
}