博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apollo源码阅读笔记(一)
阅读量:6928 次
发布时间:2019-06-27

本文共 3552 字,大约阅读时间需要 11 分钟。

Apollo源码阅读笔记(一)

先来一张官方客户端设计图,方便我们了解客户端的整体思路。

client-architecture.png

我们在使用Apollo的时候,需要标记@EnableApolloConfig来告诉程序开启apollo配置,所以这里就以EnableApolloConfig为入口,来看下apollo客户端的实现逻辑。关于apollo的使用方法详见

1. 入口 @EnableApolloConfig 注解

@EnableApolloConfig(value={"application","test-yejg"})

默认的namespace是application;

通过@EnableApolloConfig注解,引入了ApolloConfigRegistrar

public class ApolloConfigRegistrar implements ImportBeanDefinitionRegistrar {  @Override  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {    AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableApolloConfig.class.getName()));    String[] namespaces = attributes.getStringArray("value");    int order = attributes.getNumber("order");    // 暂存需要关注的namespaces,后面在PropertySourcesProcessor中会把配置属性加载env中    PropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaces), order);    Map
propertySourcesPlaceholderPropertyValues = new HashMap<>(); // to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer propertySourcesPlaceholderPropertyValues.put("order", 0); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesProcessor.class.getName(), PropertySourcesProcessor.class); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(), ApolloAnnotationProcessor.class); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueProcessor.class.getName(), SpringValueProcessor.class); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueDefinitionProcessor.class.getName(), SpringValueDefinitionProcessor.class); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloJsonValueProcessor.class.getName(), ApolloJsonValueProcessor.class); }}

注意上面代码中,通过PropertySourcesProcessor.addNamespaces暂存了namespaces,下面就先沿着 PropertySourcesProcessor来展开

2. 配置设置到environment的过程

PropertySourcesProcessor实现了BeanFactoryPostProcessor,并能获取到env

public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware, PriorityOrdered{    ...}

在Spring应用启动的时候

refresh() –> invokeBeanFactoryPostProcessors(beanFactory) –> PropertySourcesProcessor.postProcessBeanFactory

—> initializePropertySources();

—> initializeAutoUpdatePropertiesFeature(beanFactory);

就这样,Apollo的PropertySourcesProcessor就被调用起来了。

在它的postProcessBeanFactory方法中依次调用initializePropertySources和initializeAutoUpdatePropertiesFeature,先来看initializePropertySources做了啥事情:

  1. 将NAMESPACE_NAMES (Multimap<Integer, String>)排序;

  2. 遍历排序后的namespaces,依次调用 ConfigService.getConfig(namespace) 获取配置信息Config;

  3. 将config封装成ConfigPropertySource[Apollo的],保存到CompositePropertySource[spring-core的];

    ​ 此composite名为 ApolloPropertySources

    ​ ConfigPropertySource继承自spring-core的EnumerablePropertySource

    ​ 代码:composite.addPropertySource(XXX);

  4. 循环处理完 NAMESPACE_NAMES 之后,将其清空掉;

  5. 将前面循环处理好的compositePropertySource加入到env中;

    ​ 加到env时,判断env中是否存在 ApolloBootstrapPropertySources是否存在,确保其在第一的位置,而前面循环处理得到的ApolloPropertySources紧随其后。

    ​ 相关代码:

    ​ environment.getPropertySources().addAfter(“XXX source name”, composite);

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

这部分的逻辑,其实就是佐证了Apollo的 。

盗用官方的一张图来简单说明这个流程:

environment-remote-source.png

转载于:https://www.cnblogs.com/yejg1212/p/10232871.html

你可能感兴趣的文章
漫步天猫——新商路导航(全彩)
查看>>
HDU 4609 3-idiots(FFT)
查看>>
Makefile 中的.PHONY
查看>>
微软 Share Point “.NET研究”2010 企业应用解决方案
查看>>
十步教你ASP“.NET研究”.NET MVC2项目升级MVC 3 RC
查看>>
创建表-sqlite 基础教程(5)
查看>>
【转】Java transient关键字使用小记
查看>>
C#项目开发小记
查看>>
如何让在iframe框架内的按钮模拟HTA窗口的关闭功能? [复制链接]
查看>>
苹果中国应用商店改为人民币结算 可网银充值
查看>>
使用ARGV在命令行里接收参数
查看>>
分享一個用Mootools剛寫的小玩意
查看>>
iphone iPhone开发应用UIImage图片对象操作
查看>>
字符串json转换为xml xml转换json
查看>>
基于 Spring 和 iBATIS 的动态可更新多数据源持久层
查看>>
会移动的文字
查看>>
SQL XQuery的Action
查看>>
SpringMVC中利用@InitBinder来对页面数据进行解析绑定
查看>>
乐视云监控数据存放到influxdb中
查看>>
.NET Core容器化@Docker
查看>>