Spring与Dubbo整合原理与源码分析【1】注解@EnableDubbo
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documented@EnableDubboConfig// @EnableDubboConfig注解用来将properties文件中的配置项转化为对应的Bean@DubboComponentScan// @DubboComponentScan注解用来扫描服务提供者和引用者(@Service与@Reference)public @interface EnableDubbo {@AliasFor(annotation = DubboComponentScan.class, attribute = "basePackages")String[] scanBasePackages() default {};@AliasFor(annotation = DubboComponentScan.class, attribute = "basePackageClasses")Class<?>[] scanBasePackageClasses() default {};@AliasFor(annotation = EnableDubboConfig.class, attribute = "multiple")boolean multipleConfig() default true;}【2】注解@EnableDubboConfig
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documented@Import(DubboConfigConfigurationRegistrar.class)public @interface EnableDubboConfig {boolean multiple() default true;}1)DubboConfigConfigurationRegistrar类的作用
//因为实现了ImportBeanDefinitionRegistrar接口,spring容器就会实例化该类,并且调用其registerBeanDefinitions方法;public class DubboConfigConfigurationRegistrar implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {//执行DubboConfigConfigurationRegistrar;AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableDubboConfig.class.getName()));boolean multiple = attributes.getBoolean("multiple"); //默认值是true// Single Config BindingsregisterBeans(registry, DubboConfigConfiguration.Single.class);if (multiple) { // Since 2.6.6 https://github.com/apache/dubbo/issues/3193registerBeans(registry, DubboConfigConfiguration.Multiple.class);}}}2)registerBeans做了什么
public static void registerBeans(BeanDefinitionRegistry registry, Class<?>... annotatedClasses) {if (ObjectUtils.isEmpty(annotatedClasses)) {return;}...AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry);...// 利用Spring中的AnnotatedBeanDefinitionReader来解析annotatedClasses// 会解析该类上的注解,然后进行处理reader.register(annotatedClasses);}3)DubboConfigConfiguration类展示
public class DubboConfigConfiguration {/*** Single Dubbo {@link AbstractConfig Config} Bean Binding*/@EnableDubboConfigBindings({@EnableDubboConfigBinding(prefix = "dubbo.application", type = ApplicationConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.module", type = ModuleConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.registry", type = RegistryConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.protocol", type = ProtocolConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.monitor", type = MonitorConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.provider", type = ProviderConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.consumer", type = ConsumerConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.config-center", type = ConfigCenterBean.class),@EnableDubboConfigBinding(prefix = "dubbo.metadata-report", type = MetadataReportConfig.class),@EnableDubboConfigBinding(prefix = "dubbo.metrics", type = MetricsConfig.class)})public static class Single {}/*** Multiple Dubbo {@link AbstractConfig Config} Bean Binding*/@EnableDubboConfigBindings({@EnableDubboConfigBinding(prefix = "dubbo.applications", type = ApplicationConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.modules", type = ModuleConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.registries", type = RegistryConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.protocols", type = ProtocolConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.monitors", type = MonitorConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.providers", type = ProviderConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.consumers", type = ConsumerConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.config-centers", type = ConfigCenterBean.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.metadata-reports", type = MetadataReportConfig.class, multiple = true),@EnableDubboConfigBinding(prefix = "dubbo.metricses", type = MetricsConfig.class, multiple = true)})public static class Multiple {}}
经验总结扩展阅读
- 四 SpringBoot - 整合Mybatis,逆向工程,JPA
- 属鸡2023年多大年龄了 属鸡人2023全年运势详解
- 华为手环6怎么样_华为手环6功能详解
- logback.xml详解
- iptables使用详解
- 一篇文章带你掌握主流办公框架——SpringBoot
- RAID5 IO处理之条带读代码详解
- RAID5 IO处理之写请求代码详解
- RAID5 IO处理之重构代码详解
- RAID5 IO处理之replace代码详解