apollo在Spring boot加载过程解析

本文介绍了ApolloApplicationContextInitializer类在Spring应用中的作用,包括初始化环境、处理系统属性和在Spring配置启动阶段的加载顺序。文章还提及了一个面向Java开发者的资源包,包含了学习资料和项目实战,旨在帮助开发者提升技能。
摘要由CSDN通过智能技术生成

加载过程解析

======

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);

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

链图片转存中…(img-N6NThvYz-1710780244595)]
[外链图片转存中…(img-hiJrapsW-1710780244595)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-VdQTZIG6-1710780244596)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值