Skip to content

Commit eea2008

Browse files
authored
Merge pull request apolloconfig#1336 from nobodyiam/1.x
support setting apollo system properties via application.properties
2 parents 3ee3748 + 5258ba1 commit eea2008

File tree

3 files changed

+131
-5
lines changed

3 files changed

+131
-5
lines changed

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
88
import com.ctrip.framework.apollo.spring.util.SpringInjector;
99
import com.google.common.base.Splitter;
10+
import com.google.common.base.Strings;
1011
import java.util.List;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
@@ -16,17 +17,22 @@
1617
import org.springframework.core.env.ConfigurableEnvironment;
1718

1819
/**
19-
* Inject the Apollo config in Spring Boot bootstrap phase
20+
* Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
2021
*
2122
* <p>Configuration example:</p>
2223
* <pre class="code">
23-
* # will inject 'application' namespace in bootstrap phase
24+
* # set app.id
25+
* app.id = 100004458
26+
* # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
2427
* apollo.bootstrap.enabled = true
2528
* </pre>
2629
*
2730
* or
2831
*
2932
* <pre class="code">
33+
* # set app.id
34+
* app.id = 100004458
35+
* # enable apollo bootstrap config
3036
* apollo.bootstrap.enabled = true
3137
* # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
3238
* apollo.bootstrap.namespaces = application,FX.apollo
@@ -36,13 +42,18 @@ public class ApolloApplicationContextInitializer implements
3642
ApplicationContextInitializer<ConfigurableApplicationContext> {
3743
private static final Logger logger = LoggerFactory.getLogger(ApolloApplicationContextInitializer.class);
3844
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults();
45+
private static final String[] APOLLO_SYSTEM_PROPERTIES = {"app.id", ConfigConsts.APOLLO_CLUSTER_KEY,
46+
"apollo.cacheDir", ConfigConsts.APOLLO_META_KEY};
3947

4048
private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector
4149
.getInstance(ConfigPropertySourceFactory.class);
4250

4351
@Override
4452
public void initialize(ConfigurableApplicationContext context) {
4553
ConfigurableEnvironment environment = context.getEnvironment();
54+
55+
initializeSystemProperty(environment);
56+
4657
String enabled = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, "false");
4758
if (!Boolean.valueOf(enabled)) {
4859
logger.debug("Apollo bootstrap config is not enabled for context {}, see property: ${{}}", context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
@@ -68,4 +79,27 @@ public void initialize(ConfigurableApplicationContext context) {
6879

6980
environment.getPropertySources().addFirst(composite);
7081
}
82+
83+
/**
84+
* To fill system properties from environment config
85+
*/
86+
void initializeSystemProperty(ConfigurableEnvironment environment) {
87+
for (String propertyName : APOLLO_SYSTEM_PROPERTIES) {
88+
fillSystemPropertyFromEnvironment(environment, propertyName);
89+
}
90+
}
91+
92+
private void fillSystemPropertyFromEnvironment(ConfigurableEnvironment environment, String propertyName) {
93+
if (System.getProperty(propertyName) != null) {
94+
return;
95+
}
96+
97+
String propertyValue = environment.getProperty(propertyName);
98+
99+
if (Strings.isNullOrEmpty(propertyValue)) {
100+
return;
101+
}
102+
103+
System.setProperty(propertyName, propertyValue);
104+
}
71105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.ctrip.framework.apollo.spring.boot;
2+
3+
import static org.junit.Assert.*;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.when;
6+
7+
import com.ctrip.framework.apollo.core.ConfigConsts;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.springframework.core.env.ConfigurableEnvironment;
12+
13+
public class ApolloApplicationContextInitializerTest {
14+
15+
private ApolloApplicationContextInitializer apolloApplicationContextInitializer;
16+
17+
@Before
18+
public void setUp() throws Exception {
19+
apolloApplicationContextInitializer = new ApolloApplicationContextInitializer();
20+
}
21+
22+
@After
23+
public void tearDown() throws Exception {
24+
System.clearProperty("app.id");
25+
System.clearProperty(ConfigConsts.APOLLO_CLUSTER_KEY);
26+
System.clearProperty("apollo.cacheDir");
27+
System.clearProperty(ConfigConsts.APOLLO_META_KEY);
28+
}
29+
30+
@Test
31+
public void testFillFromEnvironment() throws Exception {
32+
String someAppId = "someAppId";
33+
String someCluster = "someCluster";
34+
String someCacheDir = "someCacheDir";
35+
String someApolloMeta = "someApolloMeta";
36+
37+
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
38+
39+
when(environment.getProperty("app.id")).thenReturn(someAppId);
40+
when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(someCluster);
41+
when(environment.getProperty("apollo.cacheDir")).thenReturn(someCacheDir);
42+
when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(someApolloMeta);
43+
44+
apolloApplicationContextInitializer.initializeSystemProperty(environment);
45+
46+
assertEquals(someAppId, System.getProperty("app.id"));
47+
assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY));
48+
assertEquals(someCacheDir, System.getProperty("apollo.cacheDir"));
49+
assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY));
50+
}
51+
52+
@Test
53+
public void testFillFromEnvironmentWithSystemPropertyAlreadyFilled() throws Exception {
54+
String someAppId = "someAppId";
55+
String someCluster = "someCluster";
56+
String someCacheDir = "someCacheDir";
57+
String someApolloMeta = "someApolloMeta";
58+
59+
System.setProperty("app.id", someAppId);
60+
System.setProperty(ConfigConsts.APOLLO_CLUSTER_KEY, someCluster);
61+
System.setProperty("apollo.cacheDir", someCacheDir);
62+
System.setProperty(ConfigConsts.APOLLO_META_KEY, someApolloMeta);
63+
64+
String anotherAppId = "anotherAppId";
65+
String anotherCluster = "anotherCluster";
66+
String anotherCacheDir = "anotherCacheDir";
67+
String anotherApolloMeta = "anotherApolloMeta";
68+
69+
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
70+
71+
when(environment.getProperty("app.id")).thenReturn(anotherAppId);
72+
when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(anotherCluster);
73+
when(environment.getProperty("apollo.cacheDir")).thenReturn(anotherCacheDir);
74+
when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(anotherApolloMeta);
75+
76+
apolloApplicationContextInitializer.initializeSystemProperty(environment);
77+
78+
assertEquals(someAppId, System.getProperty("app.id"));
79+
assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY));
80+
assertEquals(someCacheDir, System.getProperty("apollo.cacheDir"));
81+
assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY));
82+
}
83+
84+
@Test
85+
public void testFillFromEnvironmentWithNoPropertyFromEnvironment() throws Exception {
86+
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
87+
88+
apolloApplicationContextInitializer.initializeSystemProperty(environment);
89+
90+
assertNull(System.getProperty("app.id"));
91+
assertNull(System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY));
92+
assertNull(System.getProperty("apollo.cacheDir"));
93+
assertNull(System.getProperty(ConfigConsts.APOLLO_META_KEY));
94+
}
95+
}

apollo-core/src/main/java/com/ctrip/framework/foundation/internals/provider/DefaultApplicationProvider.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ public void initialize() {
2828
in = DefaultApplicationProvider.class.getResourceAsStream(APP_PROPERTIES_CLASSPATH);
2929
}
3030

31-
if (in == null) {
32-
logger.warn("{} not found from classpath!", APP_PROPERTIES_CLASSPATH);
33-
}
3431
initialize(in);
3532
} catch (Throwable ex) {
3633
logger.error("Initialize DefaultApplicationProvider failed.", ex);

0 commit comments

Comments
 (0)