-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Support apollo config injection in Spring Boot bootstrap phase #937
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这句日志少了一个大括号{}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
。。。这个日志不需要传
PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED
这个参数。。。可以提个PR改一下。。。或者晚点我改一下也行