zoukankan      html  css  js  c++  java
  • 第四章:(3)原理之 Dubbo 启动解析—标签解析

    一、启动解析

      Dubbo 可以很好的 Spring 框架进行整合,而且可以在 Spring 的 xml 配置文件中进行配置 Dubbo 的相关信息,那么是如何进行解析配置文件的呢?

      在 Spring 中 BeanDefinitionParser 接口是用来解析 xml 配置文件的顶级接口,来看一下它的继承结构:

      

       其中的 DubboBeanDefinitionParser 就是用来解析 dubbo 标签的信息。

      DubboBeanDefinitionParser 中的 parse() 方法就是用来解析 xml 标签信息:

        @SuppressWarnings("unchecked")
        private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
            RootBeanDefinition beanDefinition = new RootBeanDefinition();
            beanDefinition.setBeanClass(beanClass);
            beanDefinition.setLazyInit(false);
            String id = element.getAttribute("id");
            if ((id == null || id.length() == 0) && required) {
                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;
                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);
                }
                parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
                beanDefinition.getPropertyValues().addPropertyValue("id", id);
            }
            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 = element.getAttribute("class");
                if (className != null && className.length() > 0) {
                    RootBeanDefinition classDefinition = new RootBeanDefinition();
                    classDefinition.setBeanClass(ReflectUtils.forName(className));
                    classDefinition.setLazyInit(false);
                    parseProperties(element.getChildNodes(), classDefinition);
                    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<String>();
            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 property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
                    props.add(property);
                    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;
                    }
                    if ("parameters".equals(property)) {
                        parameters = parseParameters(element.getChildNodes(), beanDefinition);
                    } 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 = element.getAttribute(property);
                        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(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;
                                    } else if ("protocol".equals(property)
                                            && ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)
                                            && (!parserContext.getRegistry().containsBeanDefinition(value)
                                            || !ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
                                        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;
                                    } else if ("onreturn".equals(property)) {
                                        int index = value.lastIndexOf(".");
                                        String returnRef = value.substring(0, index);
                                        String returnMethod = value.substring(index + 1);
                                        reference = new RuntimeBeanReference(returnRef);
                                        beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
                                    } else if ("onthrow".equals(property)) {
                                        int index = value.lastIndexOf(".");
                                        String throwRef = value.substring(0, index);
                                        String throwMethod = value.substring(index + 1);
                                        reference = new RuntimeBeanReference(throwRef);
                                        beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
                                    } else if ("oninvoke".equals(property)) {
                                        int index = value.lastIndexOf(".");
                                        String invokeRef = value.substring(0, index);
                                        String invokeRefMethod = value.substring(index + 1);
                                        reference = new RuntimeBeanReference(invokeRef);
                                        beanDefinition.getPropertyValues().addPropertyValue("oninvokeMethod", invokeRefMethod);
                                    }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(property, 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;
        }

        这个方法就是根据不同的类型来解析,然后获取标签内的属性及值。

        那么,是怎么获取到不同的类型?就是入参部分的 beanClass 就是不同的类型。

        在构造器处打上断点,看这个 DubboBeanDefinitionParser 是怎么创建出来的。

        

         可以发现,在构造器的前一步是 DubboNamespaceHandler 这个类,那么这个类有做了什么?

    public class DubboNamespaceHandler extends NamespaceHandlerSupport {
    
        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("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 AnnotationBeanDefinitionParser());
        }
    
    }

      这个处理器在 init() 方法注册了特别多的 DubboBeanDefinitionParser 标签解析器。

      至此,可以看到,遇到不同的标签内容会使用不同的标签解析器来进行解析。

      

      

      标签解析会使用 DubboBeanDefinitionParser解析来解析配置文件的内容,然后把对应的值保存到对应的 xxxConfig 或者 ServiceBean 和 ReferenceBean 中。

     

  • 相关阅读:
    [原] JT SQL Server 性能调优札记之三
    [转]SQL Server 2000执行计划成本(4/5)
    [转]Oracle的Online Redo Log 相关操作
    [原]成功在ESX上搭建SQL Server 2005集群
    我的软设、系分考试经验(书)
    [转]Linux软RAID的技术概要及实现
    [原]Console小技巧——七彩输出
    SQL SERVER 2005 压缩日志及数据库文件大小
    [转]SQL Server 2000执行计划成本(2/5)
    [原]在SQL Server 2005 中使用.net程序集的一项注意
  • 原文地址:https://www.cnblogs.com/niujifei/p/15807286.html
Copyright © 2011-2022 走看看