【dubbo源码】dubbo完美接入spring过程剖析

发布于:2022-10-17 ⋅ 阅读:(510) ⋅ 点赞:(0)

1、前言

使用spring框架来整合Dubbo服务,Dubbo中的各项配置最终都会成为spring中的Bean对象,并遵循spring bean的整个生命周期管理。本文使用xml配置文件的方式来配置Dubbo服务来分析下整个过程。

如果是spring项目,在启动时会加载并解析resources目录下的xml,然后将xml配置文件中的配置加载成spring容器的bean,dubbo需要定义dubbo_provider.xml或consumer_provider.xml并加入到applicationContext.xml中。
如果是springboot项目,则通过配置类的注解来导入

@SpringBootApplication
@ImportResource(locations = {"classpath:dubbo-provider.xml"})
public class DubboProviderApplication {

下面给出provider.xml和consumer.xml简单示例,他们分别为服务提供者和服务消费者配置文件

provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--配置应用名-->
    <dubbo:application name="dubbo-provider">
        <dubbo:parameter key="qos.enable" value="false"/>
    </dubbo:application>
    <!--配置注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181" file="${ZOOKEEPER_CACHE_FILE:/tmp/test/dubbo.cache}"/>
    <!--配置服务协议-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--配置服务暴露,暴露一个服务-->
    <bean id="userService" class="com.example.springboot.dubbo.dubboprovider.service.UserServiceImpl"/>
    <dubbo:service interface="com.example.springboot.dubbo.dubboapi.service.UserService" ref="userService"/>
</beans>

consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--配置应用名-->
    <dubbo:application name="dubbo-customer">
        <dubbo:parameter key="qos.enable" value="false"/>
    </dubbo:application>
    <dubbo:consumer timeout="50000" filter="">
     </dubbo:consumer>
    <!--配置注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--配置代理-->
    <dubbo:reference id="userService" check="false" interface="com.example.springboot.dubbo.dubboapi.service.UserService"/>

</beans>

2、dubbo框架与spring整合过程

如果是通过xml的方式来配置dubbo服务,无论是spring还是springboot,最终都是通过xml标签解析器来读取xml中的配置信息,将其转化为dubbo框架中对应的配置类对象。整合过程如下:
1、定义xsd标签定义META-INF/dubbo.xsd
2、在META-INF/spring.schemas文件中,定义xsd文件全限定名与一个在spring的xml配置文件中可以配置的命名空间url的映射;
在这里插入图片描述
3、定义一个实现了BeanDefinitionParser接口的实现类DubboBeanDefinitionParser,定义标签的处理逻辑;

public class DubboBeanDefinitionParser implements BeanDefinitionParser {
private static RootBeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
        //省略...
        if (ProtocolConfig.class.equals(beanClass)) {
            for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {
                BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);
                PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");
                if (property != null) {
                    Object value = property.getValue();
                    if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
                        definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
                    }
                }
            }
        } else if (ServiceBean.class.equals(beanClass)) {
            String className = resolveAttribute(element, "class", parserContext);
            if (StringUtils.isNotEmpty(className)) {
                RootBeanDefinition classDefinition = new RootBeanDefinition();
                classDefinition.setBeanClass(ReflectUtils.forName(className));
                classDefinition.setLazyInit(false);
                parseProperties(element.getChildNodes(), classDefinition, parserContext);
                beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
            }
        } else if (ProviderConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
        } else if (ConsumerConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
        }
        Set<String> props = new HashSet<>();
        ManagedMap parameters = null;
        for (Method setter : beanClass.getMethods()) {
            String name = setter.getName();
            if (name.length() > 3 && name.startsWith("set")
                    && Modifier.isPublic(setter.getModifiers())
                    && setter.getParameterTypes().length == 1) {
                Class<?> type = setter.getParameterTypes()[0];
                String beanProperty = name.substring(3, 4).toLowerCase() + name.substring(4);
                String property = StringUtils.camelToSplitName(beanProperty, "-");
                props.add(property);
                // check the setter/getter whether match
                Method getter = null;
                try {
                    getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
                } catch (NoSuchMethodException e) {
                    try {
                        getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
                    } catch (NoSuchMethodException e2) {
                        // ignore, there is no need any log here since some class implement the interface: EnvironmentAware,
                        // ApplicationAware, etc. They only have setter method, otherwise will cause the error log during application start up.
                    }
                }
                if (getter == null
                        || !Modifier.isPublic(getter.getModifiers())
                        || !type.equals(getter.getReturnType())) {
                    continue;
                }
                if ("parameters".equals(property)) {
                    parameters = parseParameters(element.getChildNodes(), beanDefinition, parserContext);
                } else if ("methods".equals(property)) {
                    parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
                } else if ("arguments".equals(property)) {
                    parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
                } else {
                    String value = resolveAttribute(element, property, parserContext);
                    if (value != null) {
                        value = value.trim();
                        if (value.length() > 0) {
                            if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
                                RegistryConfig registryConfig = new RegistryConfig();
                                registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
                                beanDefinition.getPropertyValues().addPropertyValue(beanProperty, registryConfig);
                            } else if ("provider".equals(property) || "registry".equals(property) || ("protocol".equals(property) && AbstractServiceConfig.class.isAssignableFrom(beanClass))) {
                                beanDefinition.getPropertyValues().addPropertyValue(beanProperty + "Ids", value);
                            } else {
                                Object reference;
                                if (isPrimitive(type)) {
                                    if ("async".equals(property) && "false".equals(value)
                                            || "timeout".equals(property) && "0".equals(value)
                                            || "delay".equals(property) && "0".equals(value)
                                            || "version".equals(property) && "0.0.0".equals(value)
                                            || "stat".equals(property) && "-1".equals(value)
                                            || "reliable".equals(property) && "false".equals(value)) {
                                        // backward compatibility for the default value in old version's xsd
                                        value = null;
                                    }
                                    reference = value;
                                } else if (ONRETURN.equals(property) || ONTHROW.equals(property) || ONINVOKE.equals(property)) {
                                    int index = value.lastIndexOf(".");
                                    String ref = value.substring(0, index);
                                    String method = value.substring(index + 1);
                                    reference = new RuntimeBeanReference(ref);
                                    beanDefinition.getPropertyValues().addPropertyValue(property + METHOD, method);
                                } else {
                                    if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
                                        BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
                                        if (!refBean.isSingleton()) {
                                            throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value + "\" scope=\"singleton\" ...>");
                                        }
                                    }
                                    reference = new RuntimeBeanReference(value);
                                }
                                beanDefinition.getPropertyValues().addPropertyValue(beanProperty, reference);
                            }
                        }
                    }
                }
            }
        }
        NamedNodeMap attributes = element.getAttributes();
        int len = attributes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = attributes.item(i);
            String name = node.getLocalName();
            if (!props.contains(name)) {
                if (parameters == null) {
                    parameters = new ManagedMap();
                }
                String value = node.getNodeValue();
                parameters.put(name, new TypedStringValue(value, String.class));
            }
        }
        if (parameters != null) {
            beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
        }
        return beanDefinition;
    }
}

4、定义一个实现了NamespaceHandler接口或者继承Spring的NamespaceHandlerSupport的实现类DubboNamespaceHandler。在init方法中,使用registerBeanDefinitionParser方法配置标签名称和BeanDefinitionParser实现类的映射关系。

public class DubboNamespaceHandler extends NamespaceHandlerSupport implements ConfigurableSourceBeanMetadataElement {

    static {
        Version.checkDuplicate(DubboNamespaceHandler.class);
    }

    @Override
    public void init() {
        registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
        registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
        registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
        registerBeanDefinitionParser("config-center", new DubboBeanDefinitionParser(ConfigCenterBean.class, true));
        registerBeanDefinitionParser("metadata-report", new DubboBeanDefinitionParser(MetadataReportConfig.class, true));
        registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
        registerBeanDefinitionParser("metrics", new DubboBeanDefinitionParser(MetricsConfig.class, true));
        registerBeanDefinitionParser("ssl", new DubboBeanDefinitionParser(SslConfig.class, true));
        registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
        registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
        registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
        registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
        registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, true));
        registerBeanDefinitionParser("annotation", new AnnotationBeanDefinitionParser());
    }
    
     ......
}

5、在META-INF/spring.handlers文件中,定义以上命名空间url和该NamespaceHandler实现类的映射。
在这里插入图片描述

3、实现原理剖析

springboot项目在启动的核心流程refresh方法中执行一个类名为ConfigurationClassPostProcessor的BeanFactoryPostProcessor,主要是调用postProcessBeanDefinitionRegistry方法,用于通过解析配置类加载bean definition, 我们最终关注的逻辑是通过ConfigurationClassBeanDefinitionReader来处理loadBeanDefinitionsFromImportedResources,即配置文件上通过@ImportResource导入的xml配置文件中的配置信息,通过解析配置标签来转成相应的Bean对象,后续的处理与spring项目基本一致,
核心流程在DefaultNamespaceHandlerResolver.resolve方法中调用NamespaceHandler.init()方法来注册所支持的标签解析处理器BeanDefinitionParser,即DubboBeanDefinitionParser,每个标签的解析过程主要在parse方法中执行。最终,xml中所有配置标签转换为dubbo中的config配置对象。
核心的几个配置类如下图

在这里插入图片描述

3.1 服务端暴露服务入口

通过上图可知,这些配置类都共同继承于顶层抽象类AbstractConfig,在AbstractConfig类内部有一个使用@PostConstruct注解的方法addIntoConfigManager,将所有配置统一添加到dubbo的配置管理器ConfigManager中

    @PostConstruct
    public void addIntoConfigManager() {
        ApplicationModel.getConfigManager().addConfig(this);
    }

这个addconfig方法是线程安全的,所有配置类保存在内部的一个缓存Map中

 final Map<String, Map<String, AbstractConfig>> configsCache = newMap();

此外,Dubbo内部定义了一个事件监听器DubboBootstrapApplicationListener,继承OnceApplicationContextEventListener,在ApplicationContext执行refresh的最后阶段finishRefresh方法中会调用publishEvent来通知事件监听器,我们看下DubboBootstrapApplicationListener的源码

public class DubboBootstrapApplicationListener extends OnceApplicationContextEventListener implements Ordered {

    //...省略...
    
    @Override
    public void onApplicationContextEvent(ApplicationContextEvent event) {
        if (DubboBootstrapStartStopListenerSpringAdapter.applicationContext == null) {
            DubboBootstrapStartStopListenerSpringAdapter.applicationContext = event.getApplicationContext();
        }
        if (event instanceof ContextRefreshedEvent) {
            onContextRefreshedEvent((ContextRefreshedEvent) event);
        } else if (event instanceof ContextClosedEvent) {
            onContextClosedEvent((ContextClosedEvent) event);
        }
    }

    private void onContextRefreshedEvent(ContextRefreshedEvent event) {
        dubboBootstrap.start();
    }

    //...省略...
}

DubboBootstrapApplicationListener接收到spring容器的事件通知后会调用 dubboBootstrap.start(),至此进入dubbo的世界。

我们看下dubboBootstrap的start方式做了什么

public DubboBootstrap start() {
        if (started.compareAndSet(false, true)) {
            destroyed.set(false);
            ready.set(false);
            //一系列初始化
            initialize();
            if (logger.isInfoEnabled()) {
                logger.info(NAME + " is starting...");
            }
            // 1. 导出provider服务
            exportServices();

            // Not only provider register
            if (!isOnlyRegisterProvider() || hasExportedServices()) {
                // 2. export MetadataService
                exportMetadataService();
                //3. Register the local ServiceInstance if required
                registerServiceInstance();
            }
            
            referServices();
            if (asyncExportingFutures.size() > 0) {
                new Thread(() -> {
                    try {
                        this.awaitFinish();
                    } catch (Exception e) {
                        logger.warn(NAME + " exportAsync occurred an exception.");
                    }
                    ready.set(true);
                    if (logger.isInfoEnabled()) {
                        logger.info(NAME + " is ready.");
                    }
                    ExtensionLoader<DubboBootstrapStartStopListener> exts = getExtensionLoader(DubboBootstrapStartStopListener.class);
                    exts.getSupportedExtensionInstances().forEach(ext -> ext.onStart(this));
                }).start();
            } else {
                ready.set(true);
                if (logger.isInfoEnabled()) {
                    logger.info(NAME + " is ready.");
                }
                ExtensionLoader<DubboBootstrapStartStopListener> exts = getExtensionLoader(DubboBootstrapStartStopListener.class);
                exts.getSupportedExtensionInstances().forEach(ext -> ext.onStart(this));
            }
            if (logger.isInfoEnabled()) {
                logger.info(NAME + " has started.");
            }
        }
        return this;
    }

我们想探寻Dubbo是如何将服务端的服务导出供客户端调用的话,重点看上面的exportServices方法,这里是服务导出的入口。

主要过程是先在配置中心中获取所有的ServiceBean配置类,然后遍历每个serviceBean,通过其父类ServiceConfig的export方法来进行真正的服务暴露流程,这个后面单独拿出一篇来讲。

private void exportServices() {
        configManager.getServices().forEach(sc -> {
            // TODO, compatible with ServiceConfig.export()
            ServiceConfig serviceConfig = (ServiceConfig) sc;
            serviceConfig.setBootstrap(this);

            if (exportAsync) {
                ExecutorService executor = executorRepository.getServiceExporterExecutor();
                Future<?> future = executor.submit(() -> {
                    try {
                        exportService(serviceConfig);
                    } catch (Throwable t) {
                        logger.error("export async catch error : " + t.getMessage(), t);
                    }
                });
                asyncExportingFutures.add(future);
            } else {
                exportService(serviceConfig);
            }
        });
    }

3.2 消费端引用服务入口

服务消费端的引用过程与服务提供者的暴露过程有点区别,由于在消费端是对服务端的Service的引用reference,不是真实的service bean,故获取这个reference时,其实是获取了一个到服务端Service的proxy。故通过spring容器获取这个Service的bean时,需要返回一个proxy。
为了之后客户端调用的性能考虑,在spring启动时,dubbo提前初始化好refer代理,之后spring容器获取该bean,而不是等到调用时再初始化生成refer代理。
消费端通过ReferenceBean来开启整个引用的过程,看下类的定义:

public class ReferenceBean<T> extends ReferenceConfig<T> implements FactoryBean,
        ApplicationContextAware, InitializingBean, DisposableBean {
        }

RefrenceBean实现了FactoryBean说明他是一个工厂Bean,在spring容器中获取Bean实例时得到的并不是ReferenceBean的自身实例,而是getObject方法返回的对象。
此外,它还是实现了InitializingBean接口,在bean的所有属性都设好值后,调用afterPropertiesSet方法来执行自定义bean的实例化过程。
真正的过程在其父类ReferenceConfig中的get方法中执行,具体过程后面再细说。

public void afterPropertiesSet() throws Exception {

        // Initializes Dubbo's Config Beans before @Reference bean autowiring
        prepareDubboConfigBeans();

        // lazy init by default.
        if (init == null) {
            init = false;
        }

        // eager init if necessary.
        if (shouldInit()) {
            getObject();
        }
    }
        @Override
    public Object getObject() {
        return get();
    }

网站公告

今日签到

点亮在社区的每一天
去签到