Skip to content

Commit

Permalink
support setting apollo.meta, app.id, apollo.cacheDir and apollo.clust…
Browse files Browse the repository at this point in the history
…er from spring boot configuration files, e.g. application.properties, bootstrap.properties, etc
  • Loading branch information
nobodyiam committed Aug 6, 2018
1 parent 3ee3748 commit 5258ba1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
import com.ctrip.framework.apollo.spring.util.SpringInjector;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -16,17 +17,22 @@
import org.springframework.core.env.ConfigurableEnvironment;

/**
* Inject the Apollo config in Spring Boot bootstrap phase
* Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
*
* <p>Configuration example:</p>
* <pre class="code">
* # will inject 'application' namespace in bootstrap phase
* # set app.id
* app.id = 100004458
* # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
* apollo.bootstrap.enabled = true
* </pre>
*
* or
*
* <pre class="code">
* # set app.id
* app.id = 100004458
* # enable apollo bootstrap config
* apollo.bootstrap.enabled = true
* # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
* apollo.bootstrap.namespaces = application,FX.apollo
Expand All @@ -36,13 +42,18 @@ public class ApolloApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final Logger logger = LoggerFactory.getLogger(ApolloApplicationContextInitializer.class);
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults();
private static final String[] APOLLO_SYSTEM_PROPERTIES = {"app.id", ConfigConsts.APOLLO_CLUSTER_KEY,
"apollo.cacheDir", ConfigConsts.APOLLO_META_KEY};

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

@Override
public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment();

initializeSystemProperty(environment);

String enabled = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, "false");
if (!Boolean.valueOf(enabled)) {
logger.debug("Apollo bootstrap config is not enabled for context {}, see property: ${{}}", context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
Expand All @@ -68,4 +79,27 @@ public void initialize(ConfigurableApplicationContext context) {

environment.getPropertySources().addFirst(composite);
}

/**
* To fill system properties from environment config
*/
void initializeSystemProperty(ConfigurableEnvironment environment) {
for (String propertyName : APOLLO_SYSTEM_PROPERTIES) {
fillSystemPropertyFromEnvironment(environment, propertyName);
}
}

private void fillSystemPropertyFromEnvironment(ConfigurableEnvironment environment, String propertyName) {
if (System.getProperty(propertyName) != null) {
return;
}

String propertyValue = environment.getProperty(propertyName);

if (Strings.isNullOrEmpty(propertyValue)) {
return;
}

System.setProperty(propertyName, propertyValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.ctrip.framework.apollo.spring.boot;

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

import com.ctrip.framework.apollo.core.ConfigConsts;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.env.ConfigurableEnvironment;

public class ApolloApplicationContextInitializerTest {

private ApolloApplicationContextInitializer apolloApplicationContextInitializer;

@Before
public void setUp() throws Exception {
apolloApplicationContextInitializer = new ApolloApplicationContextInitializer();
}

@After
public void tearDown() throws Exception {
System.clearProperty("app.id");
System.clearProperty(ConfigConsts.APOLLO_CLUSTER_KEY);
System.clearProperty("apollo.cacheDir");
System.clearProperty(ConfigConsts.APOLLO_META_KEY);
}

@Test
public void testFillFromEnvironment() throws Exception {
String someAppId = "someAppId";
String someCluster = "someCluster";
String someCacheDir = "someCacheDir";
String someApolloMeta = "someApolloMeta";

ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);

when(environment.getProperty("app.id")).thenReturn(someAppId);
when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(someCluster);
when(environment.getProperty("apollo.cacheDir")).thenReturn(someCacheDir);
when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(someApolloMeta);

apolloApplicationContextInitializer.initializeSystemProperty(environment);

assertEquals(someAppId, System.getProperty("app.id"));
assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY));
assertEquals(someCacheDir, System.getProperty("apollo.cacheDir"));
assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY));
}

@Test
public void testFillFromEnvironmentWithSystemPropertyAlreadyFilled() throws Exception {
String someAppId = "someAppId";
String someCluster = "someCluster";
String someCacheDir = "someCacheDir";
String someApolloMeta = "someApolloMeta";

System.setProperty("app.id", someAppId);
System.setProperty(ConfigConsts.APOLLO_CLUSTER_KEY, someCluster);
System.setProperty("apollo.cacheDir", someCacheDir);
System.setProperty(ConfigConsts.APOLLO_META_KEY, someApolloMeta);

String anotherAppId = "anotherAppId";
String anotherCluster = "anotherCluster";
String anotherCacheDir = "anotherCacheDir";
String anotherApolloMeta = "anotherApolloMeta";

ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);

when(environment.getProperty("app.id")).thenReturn(anotherAppId);
when(environment.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)).thenReturn(anotherCluster);
when(environment.getProperty("apollo.cacheDir")).thenReturn(anotherCacheDir);
when(environment.getProperty(ConfigConsts.APOLLO_META_KEY)).thenReturn(anotherApolloMeta);

apolloApplicationContextInitializer.initializeSystemProperty(environment);

assertEquals(someAppId, System.getProperty("app.id"));
assertEquals(someCluster, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY));
assertEquals(someCacheDir, System.getProperty("apollo.cacheDir"));
assertEquals(someApolloMeta, System.getProperty(ConfigConsts.APOLLO_META_KEY));
}

@Test
public void testFillFromEnvironmentWithNoPropertyFromEnvironment() throws Exception {
ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);

apolloApplicationContextInitializer.initializeSystemProperty(environment);

assertNull(System.getProperty("app.id"));
assertNull(System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY));
assertNull(System.getProperty("apollo.cacheDir"));
assertNull(System.getProperty(ConfigConsts.APOLLO_META_KEY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public void initialize() {
in = DefaultApplicationProvider.class.getResourceAsStream(APP_PROPERTIES_CLASSPATH);
}

if (in == null) {
logger.warn("{} not found from classpath!", APP_PROPERTIES_CLASSPATH);
}
initialize(in);
} catch (Throwable ex) {
logger.error("Initialize DefaultApplicationProvider failed.", ex);
Expand Down

0 comments on commit 5258ba1

Please sign in to comment.