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 10 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 @@ -90,7 +91,8 @@ public class ApolloApplicationContextInitializer implements
ApolloClientSystemConsts.APOLLO_META,
ApolloClientSystemConsts.APOLLO_CONFIG_SERVICE,
ApolloClientSystemConsts.APOLLO_PROPERTY_ORDER_ENABLE,
ApolloClientSystemConsts.APOLLO_PROPERTY_NAMES_CACHE_ENABLE};
ApolloClientSystemConsts.APOLLO_PROPERTY_NAMES_CACHE_ENABLE,
ApolloClientSystemConsts.APOLLO_OVERRIDE_SYSTEM_PROPERTIES};

private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector
.getInstance(ConfigPropertySourceFactory.class);
Expand Down Expand Up @@ -140,7 +142,12 @@ protected void initialize(ConfigurableEnvironment environment) {

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

if (!configUtil.isOverrideSystemProperties()) {
if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
return;
}
}
environment.getPropertySources().addFirst(composite);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ApolloClientSystemConsts;
import com.ctrip.framework.apollo.spring.events.ApolloConfigChangeEvent;
import com.ctrip.framework.apollo.spring.util.SpringInjector;
import com.ctrip.framework.apollo.util.ConfigUtil;
Expand All @@ -40,11 +41,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 +107,20 @@ private void initializePropertySources() {
if (environment.getPropertySources()
.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {

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

environment.getPropertySources()
.addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite);
} else {
if (!configUtil.isOverrideSystemProperties()) {
if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
return;
}
}
environment.getPropertySources().addFirst(composite);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ConfigUtil {
private boolean propertiesOrdered = false;
private boolean propertyNamesCacheEnabled = false;
private boolean propertyFileCacheEnabled = true;
private boolean overrideSystemProperties = true;

public ConfigUtil() {
warnLogRateLimiter = RateLimiter.create(0.017); // 1 warning log output per minute
Expand All @@ -72,6 +73,7 @@ public ConfigUtil() {
initPropertiesOrdered();
initPropertyNamesCacheEnabled();
initPropertyFileCacheEnabled();
initOverrideSystemProperties();
}

/**
Expand Down Expand Up @@ -416,6 +418,10 @@ public boolean isPropertyFileCacheEnabled() {
return propertyFileCacheEnabled;
}

public boolean isOverrideSystemProperties() {
return overrideSystemProperties;
}

private void initPropertyNamesCacheEnabled() {
propertyNamesCacheEnabled = getPropertyBoolean(ApolloClientSystemConsts.APOLLO_PROPERTY_NAMES_CACHE_ENABLE,
ApolloClientSystemConsts.APOLLO_PROPERTY_NAMES_CACHE_ENABLE_ENVIRONMENT_VARIABLES,
Expand All @@ -428,6 +434,12 @@ private void initPropertyFileCacheEnabled() {
propertyFileCacheEnabled);
}

private void initOverrideSystemProperties() {
overrideSystemProperties = getPropertyBoolean(ApolloClientSystemConsts.APOLLO_OVERRIDE_SYSTEM_PROPERTIES,
ApolloClientSystemConsts.APOLLO_OVERRIDE_SYSTEM_PROPERTIES,
overrideSystemProperties);
}

private boolean getPropertyBoolean(String propertyName, String envName, boolean defaultVal) {
String enablePropertyNamesCache = System.getProperty(propertyName);
if (Strings.isNullOrEmpty(enablePropertyNamesCache)) {
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.overrideSystemProperties",
"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,27 @@ 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_BOOTSTRAP_NAMESPACES,
ConfigConsts.NAMESPACE_APPLICATION)).thenReturn("");
ConfigUtil configUtil = new ConfigUtil();
configUtil = spy(configUtil);
when(configUtil.isOverrideSystemProperties()).thenReturn(false);
MockInjector.setInstance(ConfigUtil.class, configUtil);

apolloApplicationContextInitializer.initialize(environment);

assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME));
assertEquals(propertySources.iterator().next().getName(), StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@
*/
package com.ctrip.framework.apollo.spring.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.ApolloClientSystemConsts;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.AbstractSpringIntegrationTest;
import com.ctrip.framework.apollo.spring.events.ApolloConfigChangeEvent;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.collect.Lists;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.*;

import java.util.Properties;

public class PropertySourcesProcessorTest extends AbstractSpringIntegrationTest {

Expand Down Expand Up @@ -122,4 +124,31 @@ public void testApplicationEvent() {
ApolloConfigChangeEvent event = eventCaptor.getValue();
assertSame(someConfigChangeEvent, event.getConfigChangeEvent());
}


@Test
public void testOverrideSystemProperties() {
Properties properties = new Properties();
properties.setProperty("server.port", "8080");
properties.setProperty("apollo.overrideSystemProperties", "false");
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_BOOTSTRAP_NAMESPACES,
ConfigConsts.NAMESPACE_APPLICATION)).thenReturn("");

ConfigUtil configUtil = new ConfigUtil();
configUtil = spy(configUtil);
when(configUtil.isOverrideSystemProperties()).thenReturn(false);
MockInjector.setInstance(ConfigUtil.class, configUtil);

processor.setEnvironment(environment);
processor.postProcessBeanFactory(beanFactory);

assertTrue(propertySources.contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME));
assertEquals(propertySources.iterator().next().getName(), StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,9 @@ public class ApolloClientSystemConsts {
* enable property names cache environment variables
*/
public static final String APOLLO_CACHE_FILE_ENABLE_ENVIRONMENT_VARIABLES = "APOLLO_CACHE_FILE_ENABLE";

/**
* enable apollo overrideSystemProperties
*/
public static final String APOLLO_OVERRIDE_SYSTEM_PROPERTIES = "apollo.overrideSystemProperties";
}