美文网首页
nacos源码4-spring客户端

nacos源码4-spring客户端

作者: modou1618 | 来源:发表于2019-01-14 21:26 被阅读0次

一 xml支持

1.1 相关配置文件

xml配置文件.png
配置 说明
<nacos:annotation-driven/> 添加对nacos相关注解的支持
<nacos:global-properties/> 配置服务端配置
<nacos:property-source/> 配置集信息

1.2 NacosNamespaceHandler

  • 注册xml配置解析函数
public void init() {
    registerBeanDefinitionParser("annotation-driven", new NacosAnnotationDrivenBeanDefinitionParser());
    registerBeanDefinitionParser("global-properties", new GlobalNacosPropertiesBeanDefinitionParser());
    registerBeanDefinitionParser("property-source", new NacosPropertySourceBeanDefinitionParser());
}

1.3 NacosAnnotationDrivenBeanDefinitionParser

  • 解析annotation-driven配置
registrar.registerNacosAnnotationBeans(registry);

public void registerNacosAnnotationBeans(BeanDefinitionRegistry registry) {
    registerNacosCommonBeans(registry);
    registerNacosConfigBeans(registry, environment);
    registerNacosDiscoveryBeans(registry);
}

1.3.1 registerNacosCommonBeans

  • 注册配置和服务公共的处理函数
  • registerNacosServiceFactory注册nacos服务工厂函数
  • registerAnnotationNacosInjectedBeanPostProcessor注册NacosInjected注解处理函数

1.3.2 registerNacosConfigBeans

  • 注册配置相关注解处理函数
  • registerPropertySourcesPlaceholderConfigurer占位符替换处理函数
  • registerNacosConfigPropertiesBindingPostProcessor注册NacosConfigurationProperties注解处理函数,类似于spring注解@ConfigurationProperties
  • registerNacosConfigListenerMethodProcessor注册NacosConfigListener注解处理函数,注册配置变更监听回调
  • registerNacosConfigListenerExecutor注册配置变更监听回调的异步执行线程池
  • registerNacosPropertySourcePostProcessor,注册配置集获取处理函数,支持xml配置集获取和注解配置集获取。
  • registerAnnotationNacosPropertySourceBuilder注册NacosPropertySource和NacosPropertySources注解处理函数,注解配置集信息。类似于spring注解@PropertySource和@PropertySources
  • registerNacosValueAnnotationBeanPostProcessor注册NacosValue注解处理函数,参数和方法注入。类似于spring注解 @Value。
  • registerConfigServiceBeanBuilder注册配置服务工厂函数
  • registerLoggingNacosConfigMetadataEventListener注册spring事件回调函数ApplicationListener<NacosConfigMetadataEvent>,基于NacosConfigMetadataEvent事件相关的配置元数据信息。

1.3.3 registerNacosDiscoveryBeans

  • 注册服务管理注解处理函数
  • registerNamingServiceBeanBuilder注册服务管理工厂函数

1.4 GlobalNacosPropertiesBeanDefinitionParser

  • 配置服务器配置信息处理
  • 注册配置信息为一个单例bean
public static void registerGlobalNacosProperties(Map<?, ?> globalPropertiesAttributes,
                                                 BeanDefinitionRegistry registry,
                                                 PropertyResolver propertyResolver,
                                                 String beanName) {
    Properties globalProperties = resolveProperties(globalPropertiesAttributes, propertyResolver);
    registerSingleton(registry, beanName, globalProperties);
}

1.5 NacosPropertySourceBeanDefinitionParser

  • 配置集配置处理
  • registerXmlNacosPropertySourceBuilder(registry) 注册xml配置bean NacosPropertySourceXmlBeanDefinition解析函数
  • registerNacosPropertySourcePostProcessor(registry)注册配置集获取处理函数。调用xml配置bean或配置集注解解析函数,解析配置集信息,加载配置,注册配置变更监听回调。

二 注解支持

  • BeanFactoryPostProcessor.postProcessBeanFactory接口函数处理注解@NacosPropertySource和xml property source配置,获取配置,插入spring配置源,注册配置变更监听回调函数
  • InstantiationAwareBeanPostProcessor.postProcessPropertyValues接口函数
    处理@NacosInjected注入configservice服务
    处理@NacosValue注入属性的配置值信息,
  • BeanPostProcessor.postProcessBeforeInitialization
    处理@NacosConfigurationProperties,注入配置到目标bean,注册配置变更监听回调更新目标bean
    处理@NacosValue,缓存标记的配置项用做后续动态更新
  • ContextRefreshedEvent监听,spring初始化完成时触发。
    处理@NacosConfigListener, 注册配置变更监听回调。
  • ApplicationListener.onApplicationEvent监听spring事件NacosConfigReceivedEvent,每次从配置服务端获取配置时都会触发该事件,用于动态变更@NacosValue注解目标。

相关文章

网友评论

      本文标题:nacos源码4-spring客户端

      本文链接:https://www.haomeiwen.com/subject/mhtrrqtx.html