In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the loading process of apollo in spring-boot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Integrated use 1, add gradle dependency implementation "com.ctrip.framework.apollo:apollo-client:1.6.0" 2, configure application.properties
Apollo's own configuration contains a total of 9 items, only 3 necessary configurations, and the rest are optional configurations. The configuration naming of apollo in spring-boot environment and the naming of System parameters are maintained all the time, and eventually the configuration of spring will be injected into System. The specific logic is analyzed below.
You must configure # applied IDapp.id = java-project# apollo config-service service discovery address apollo.meta = http://apollo.meta# enable apolloapollo.bootstrap.enabled = true optional configuration # load the namespace loaded by the apollo configuration apollo.bootstrap.eagerLoad.enabled=true# before the log system initialization, and load application by default Multiple secure pull secret configurations separated by commas apollo.bootstrap.namespaces = application# apollo apollo.accesskey.secret = xx# cluster configuration apollo.cluster = hk# cache path apollo.cacheDir = / opt# is consistent with the configuration order of the apollo configuration page apollo.property.order.enable = true loading process parses public class ApolloApplicationContextInitializer implements ApplicationContextInitializer, EnvironmentPostProcessor, Ordered {public static final int DEFAULT_ORDER = 0 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", "apollo.accesskey.secret", ConfigConsts.APOLLO_META_KEY, PropertiesFactory.APOLLO_PROPERTY_ORDER_ENABLE}; private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector.getInstance (ConfigPropertySourceFactory.class); private int order = DEFAULT_ORDER @ Override public void initialize (ConfigurableApplicationContext context) {ConfigurableEnvironment environment = context.getEnvironment (); if (! environment.getProperty (PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class, false)) {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); initialize (environment) } / * Initialize Apollo Configurations Just after environment is ready. * * @ param environment * / protected void initialize (ConfigurableEnvironment environment) {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 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 (configPropertySourceFactory.getConfigPropertySource (namespace, config));} 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);} / * In order to load Apollo configurations as early as even before Spring loading logging system phase, * this EnvironmentPostProcessor can be called Just After ConfigFileApplicationListener has succeeded. * * The processing sequence would be like this: * Load Bootstrap properties and application properties-> load Apollo configuration properties-> Initialize Logging systems * * @ param configurableEnvironment * @ param springApplication * / @ Override public void postProcessEnvironment (ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {/ / should always initialize system properties like app.id in the first place initializeSystemProperty (configurableEnvironment); Boolean eagerLoadEnabled = configurableEnvironment.getProperty (PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, Boolean.class, false) / / EnvironmentPostProcessor should not be triggered if you don't want Apollo Loading before Logging System Initialization if (! eagerLoadEnabled) {return;} Boolean bootstrapEnabled = configurableEnvironment.getProperty (PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class, false); if (bootstrapEnabled) {initialize (configurableEnvironment);}} / * * @ since 1.3.0 * / @ Override public int getOrder () {return order } / * * @ since 1.3.0 * / public void setOrder (int order) {this.order = order;}}
The loading logic of apollo in spring-boot is all in the above code. The key of the code is to implement the interface of two spring lifecycles.
ApplicationContextInitializer
Initialize ConfigurableApplicationContext's callback interface before being refreshed by ConfigurableApplicationContext.refresh ().
EnvironmentPostProcessor
It is earlier than the loading time of ApplicationContextInitializer, when the log system of spring-boot has not been initialized.
Logical parsing of postProcessEnvironment method
1. Initialize the configuration of System, copy the configuration (environment variables, System parameters, application.properties) in the context of spring to the System configuration, and skip if the configuration with the same name already exists in System, ensuring the highest priority of the System parameter set by-D. But it also brings an implicit problem. By default, the configuration design of apollo supports taking values from environment variables, and it also follows the uppercase specification of environment variables, which sets the "." of the System parameter. Replace it with "_" splicing, and then capitalize it. For example, apollo.meta corresponds to the APOLLO_META of the environment variable. However, in the spring-boot environment, because the configuration system of spring also loads the configuration of environment variables by default, eventually configuring apollo.meta in the environment variables will also take effect. Even higher than the properly configured APOLLO_META environment variable value.
2. Judge whether to initialize apollo at this stage according to the configuration of apollo.bootstrap.eagerLoad.enabled and apollo.bootstrap.enabled. When postProcessEnvironment () is executed, the log system is not initialized. Loading apollo at this stage can solve the problem of hosting the log configuration into apollo. The problem is that if there is a problem with apollo loading at this stage, the loading log of apollo can not be seen because the log system is not initialized, so it is not convenient to locate the loading problem of apollo. Therefore, bloggers suggest that if there is a scenario with managed log configuration, you can not enable the configuration of apollo.bootstrap.eagerLoad.enabled and enable it after apollo integration is completed.
Logical parsing of initialize method
1. According to the configuration of apollo.bootstrap.enabled, determine whether to initialize apollo at this stage. If the PropertySources of apollo is already included in the context of spring, which means that apollo has been initialized, return is dropped directly.
2. According to the configuration of apollo.bootstrap.namespaces, the default configuration is not "application". The configuration of the corresponding namespace is obtained in turn, and the configuration is added to the configuration context of spring by using the setting method of addFirst () with the highest priority attribute source. This explains why the configuration of apollo has the highest priority, which is higher than that of direct configuration in application.properties. This priority problem often causes an own problem. During the local development and debugging phase, the configuration will be debugged directly in application.properties, and then no change will take effect, because there is a configuration with the same name in apollo, which directly overwrites the local configuration at startup. Bloggers have made this mistake several times.
This is the end of the content of "what is the loading process of apollo in spring-boot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.