-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #937 from nobodyiam/apollo.bootstrap
Support apollo config injection in Spring Boot bootstrap phase
- Loading branch information
Showing
12 changed files
with
468 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.ctrip.framework.apollo.spring.boot; | ||
|
||
import com.ctrip.framework.apollo.spring.config.ConfigPropertySourcesProcessor; | ||
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; | ||
import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ConditionalOnProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED) | ||
@ConditionalOnMissingBean(PropertySourcesProcessor.class) | ||
public class ApolloAutoConfiguration { | ||
|
||
@Bean | ||
public ConfigPropertySourcesProcessor configPropertySourcesProcessor() { | ||
return new ConfigPropertySourcesProcessor(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
.../main/java/com/ctrip/framework/apollo/spring/boot/ApolloSpringApplicationRunListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.ctrip.framework.apollo.spring.boot; | ||
|
||
import com.ctrip.framework.apollo.Config; | ||
import com.ctrip.framework.apollo.ConfigService; | ||
import com.ctrip.framework.apollo.core.ConfigConsts; | ||
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; | ||
import com.ctrip.framework.apollo.spring.config.ConfigPropertySource; | ||
import com.google.common.base.Splitter; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.SpringApplicationRunListener; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.PriorityOrdered; | ||
import org.springframework.core.env.CompositePropertySource; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
/** | ||
* Inject the Apollo config in Spring Boot bootstrap phase | ||
* | ||
* <p>Configuration example:</p> | ||
* <pre class="code"> | ||
* # will inject 'application' namespace in bootstrap phase | ||
* apollo.bootstrap.enabled = true | ||
* </pre> | ||
* | ||
* or | ||
* | ||
* <pre class="code"> | ||
* apollo.bootstrap.enabled = true | ||
* # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase | ||
* apollo.bootstrap.namespaces = application,FX.apollo | ||
* </pre> | ||
*/ | ||
public class ApolloSpringApplicationRunListener implements SpringApplicationRunListener, | ||
PriorityOrdered { | ||
private static final Logger logger = LoggerFactory.getLogger(ApolloSpringApplicationRunListener.class); | ||
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults(); | ||
|
||
public ApolloSpringApplicationRunListener(SpringApplication application, String[] args) { | ||
//ignore | ||
} | ||
|
||
public void starting() { | ||
} | ||
|
||
public void started() { | ||
} | ||
|
||
public void environmentPrepared(ConfigurableEnvironment environment) { | ||
} | ||
|
||
public void contextPrepared(ConfigurableApplicationContext context) { | ||
ConfigurableEnvironment environment = context.getEnvironment(); | ||
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); | ||
return; | ||
} | ||
logger.debug("Apollo bootstrap config is enabled for context {}", context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED); | ||
|
||
if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { | ||
//already initialized | ||
return; | ||
} | ||
|
||
String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION); | ||
logger.debug("Apollo bootstrap namespaces: {}", namespaces); | ||
List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces); | ||
|
||
CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); | ||
for (String namespace : namespaceList) { | ||
Config config = ConfigService.getConfig(namespace); | ||
|
||
composite.addPropertySource(new ConfigPropertySource(namespace, config)); | ||
} | ||
|
||
environment.getPropertySources().addFirst(composite); | ||
} | ||
|
||
public void contextLoaded(ConfigurableApplicationContext context) { | ||
} | ||
|
||
public void finished(ConfigurableApplicationContext configurableApplicationContext, | ||
Throwable throwable) { | ||
} | ||
|
||
public int getOrder() { | ||
return Ordered.LOWEST_PRECEDENCE; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ient/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.ctrip.framework.apollo.spring.config; | ||
|
||
public interface PropertySourcesConstants { | ||
String APOLLO_PROPERTY_SOURCE_NAME = "ApolloPropertySources"; | ||
String APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME = "ApolloBootstrapPropertySources"; | ||
String APOLLO_BOOTSTRAP_ENABLED = "apollo.bootstrap.enabled"; | ||
String APOLLO_BOOTSTRAP_NAMESPACES = "apollo.bootstrap.namespaces"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration | ||
org.springframework.boot.SpringApplicationRunListener=\ | ||
com.ctrip.framework.apollo.spring.boot.ApolloSpringApplicationRunListener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.