天天看點

架構師集合之dubbo源碼解析(一)-xml配置源碼解析

先看看dubbo官方文檔給出的初始化過程:
基于 dubbo.jar 内的 META-INF/spring.handlers 配置,Spring 在遇到 dubbo 名稱空間時,會回調DubboNamespaceHandler。 所有 dubbo 的标簽,都統一用 DubboBeanDefinitionParser 進行解析,基于一對一屬性映射,将 XML 标簽解析為 Bean 對象。
找到dubbo.jar中的spring.handlers:
http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
           
然後我們看看DubboNamespaceHandler:
/**
 * DubboNamespaceHandler
 *
 * @author william.liangf
 * @export
 */
public class DubboNamespaceHandler extends NamespaceHandlerSupport {

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

    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("monitor", new DubboBeanDefinitionParser(MonitorConfig.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, false));
        registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));
    }

}
           
繼上篇 架構師集合之spring的xml自定義名稱空間(NamespaceHandler以及BeanDefinitionParser) ,我們可以知道,這就是利用了這點,先定義了DubboNamespaceHandler,然後執行init方法,然後調用 DubboBeanDefinitionParser.parse(Element element, ParserContext parserContext)方法去解析xml标簽。
public BeanDefinition parse(Element element, ParserContext parserContext) {
        return parse(element, parserContext, beanClass, required);
    }
    ...
     private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(beanClass);
        beanDefinition.setLazyInit(false);
        // 解析配置對象的 id 。若不存在,則進行生成。
        String id = element.getAttribute("id");
        if ((id == null || id.length() == 0) && required) {
            // 生成 id 。不同的配置對象,會存在不同。
            String generatedBeanName = element.getAttribute("name");
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                if (ProtocolConfig.class.equals(beanClass)) {
                    generatedBeanName = "dubbo";
                } else {
                    generatedBeanName = element.getAttribute("interface");
                }
            }
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                generatedBeanName = beanClass.getName();
            }
            id = generatedBeanName;
            // 若 id 已存在,通過自增序列,解決重複。
            int counter = 2;
            while (parserContext.getRegistry().containsBeanDefinition(id)) {
                id = generatedBeanName + (counter++);
            }
        }
        if (id != null && id.length() > 0) {
            if (parserContext.getRegistry().containsBeanDefinition(id)) {
                throw new IllegalStateException("Duplicate spring bean id " + id);
            }
            // 添加到 Spring 的系統資料庫
            parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
            // 設定 Bean 的 `id` 屬性值
            beanDefinition.getPropertyValues().addPropertyValue("id", id);
        }
        System.out.println("id:" + id);
        // 處理 `<dubbo:protocol` /> 的特殊情況
        if (ProtocolConfig.class.equals(beanClass)) {
            // 需要滿足第 220 至 233 行。
            // 例如:【順序要這樣】
            // <dubbo:service inter protocol="dubbo" ref="demoService"/>
            // <dubbo:protocol id="dubbo" name="dubbo" port="20880"/>
            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));
                    }
                }
            }
        // 處理 `<dubbo:service />` 的屬性 `class`
        } else if (ServiceBean.class.equals(beanClass)) {
            // 處理 `class` 屬性。例如  <dubbo:service id="sa" inter class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" >
            String className = element.getAttribute("class");
            if (className != null && className.length() > 0) {
                // 建立 Service 的 RootBeanDefinition 對象。相當于内嵌了 <bean class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
                RootBeanDefinition classDefinition = new RootBeanDefinition();
                classDefinition.setBeanClass(ReflectUtils.forName(className));
                classDefinition.setLazyInit(false);
                // 解析 Service Bean 對象的屬性
                parseProperties(element.getChildNodes(), classDefinition);
                // 設定 `<dubbo:service ref="" />` 屬性
                beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
            }
        // 解析 `<dubbo:provider />` 的内嵌子元素 `<dubbo:service />`
        } else if (ProviderConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
        // 解析 `<dubbo:consumer />` 的内嵌子元素 `<dubbo:reference />`
        } else if (ConsumerConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
        }
        Set<String> props = new HashSet<String>(); // 已解析的屬性集合
        ManagedMap parameters = null; //
        // 循環 Bean 對象的 setting 方法,将屬性添加到 Bean 對象的屬性指派
        for (Method setter : beanClass.getMethods()) {
            String name = setter.getName();
            if (name.length() > 3 && name.startsWith("set")
                    && Modifier.isPublic(setter.getModifiers())
                    && setter.getParameterTypes().length == 1) { // setting && public && 唯一參數
                Class<?> type = setter.getParameterTypes()[0];
                // 添加 `props`
                String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
                props.add(property);
                // getting && public && 屬性值類型統一
                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) {
                    }
                }
                if (getter == null
                        || !Modifier.isPublic(getter.getModifiers())
                        || !type.equals(getter.getReturnType())) {
                    continue;
                }
                // 解析 `<dubbo:parameters />`
                if ("parameters".equals(property)) {
                    parameters = parseParameters(element.getChildNodes(), beanDefinition);
                // 解析 `<dubbo:method />`
                } else if ("methods".equals(property)) {
                    parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
                // 解析 `<dubbo:argument />`
                } else if ("arguments".equals(property)) {
                    parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
                } else {
                    String value = element.getAttribute(property);
                    if (value != null) {
                        value = value.trim();
                        if (value.length() > 0) {
                            // 不想注冊到注冊中心的情況,即 `registry=N/A` 。
                            if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
                                RegistryConfig registryConfig = new RegistryConfig();
                                registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
                                beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);
                            // 多注冊中心的情況
                            } else if ("registry".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("registries", value, beanDefinition, parserContext);
                            // 多服務提供者的情況
                            } else if ("provider".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("providers", value, beanDefinition, parserContext);
                            // 多協定的情況
                            } else if ("protocol".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("protocols", value, beanDefinition, parserContext);
                            } 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;
                                // 處理在 `<dubbo:provider />` 或者 `<dubbo:service />` 上定義了 `protocol` 屬性的 相容性。
                                } else if ("protocol".equals(property)
                                        && ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value) // 存在該注冊協定的實作
                                        && (!parserContext.getRegistry().containsBeanDefinition(value) // Spring 系統資料庫中不存在該 `<dubbo:provider />` 的定義
                                            || !ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName())) // Spring 系統資料庫中存在該編号,但是類型不為 ProtocolConfig 。
                                        ) {
                                    // 目前,`<dubbo:provider protocol="" />` 推薦獨立成 `<dubbo:protocol />`
                                    if ("dubbo:provider".equals(element.getTagName())) {
                                        logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
                                    }
                                    // backward compatibility
                                    ProtocolConfig protocol = new ProtocolConfig();
                                    protocol.setName(value);
                                    reference = protocol;
                                // 處理 `onreturn` 屬性
                                } else if ("onreturn".equals(property)) {
                                    // 按照 `.` 拆分
                                    int index = value.lastIndexOf(".");
                                    String returnRef = value.substring(0, index);
                                    String returnMethod = value.substring(index + 1);
                                    // 建立 RuntimeBeanReference ,指向回調的對象
                                    reference = new RuntimeBeanReference(returnRef);
                                    // 設定 `onreturnMethod` 到 BeanDefinition 的屬性值
                                    beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
                                    // 處理 `onthrow` 屬性
                                } else if ("onthrow".equals(property)) {
                                    // 按照 `.` 拆分
                                    int index = value.lastIndexOf(".");
                                    String throwRef = value.substring(0, index);
                                    String throwMethod = value.substring(index + 1);
                                    // 建立 RuntimeBeanReference ,指向回調的對象
                                    reference = new RuntimeBeanReference(throwRef);
                                    // 設定 `onthrowMethod` 到 BeanDefinition 的屬性值
                                    beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
                                // 通用解析
                                } else {
                                    // 指向的 Service 的 Bean 對象,必須是單例
                                    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\" ...>");
                                        }
                                    }
                                    // 建立 RuntimeBeanReference ,指向 Service 的 Bean 對象
                                    reference = new RuntimeBeanReference(value);
                                }
                                // 設定 BeanDefinition 的屬性值
                                beanDefinition.getPropertyValues().addPropertyValue(property, reference);
                            }
                        }
                    }
                }
            }
        }
        // 将 XML 元素,未在上面周遊到的屬性,添加到 `parameters` 集合中。目前測試下來,不存在這樣的情況。
        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;
    }
           
如果看了上篇架構師集合之spring的xml自定義名稱空間(NamespaceHandler以及BeanDefinitionParser) ,那麼dubbo的xml的原理就很簡單了。