dubbo源码学习(四)初始化过程细节:解析服务

阅读更多
今天将真正去看dubbo内部的实现过程,看dubbo的源码前我先把dubbo的用户指南和开发指指南大概的看了一遍,这样再看dubbo源码比较轻松。从用户指南和开发指指南可以找到相应的切入点,今天将介绍的是dubbo的初始化解析bean的过程:

解析服务
基于dubbo.jar内的META-INF/spring.handlers配置,Spring在遇到dubbo名称空间时,会回调DubboNamespaceHandler。
所有dubbo的标签,都统一用DubboBeanDefinitionParser进行解析,基于一对一属性映射,将XML标签解析为Bean对象。
在ServiceConfig.export()或ReferenceConfig.get()初始化时,将Bean对象转换URL格式,所有Bean属性转成URL的参数。
然后将URL传给Protocol扩展点,基于扩展点的Adaptive机制,根据URL的协议头,进行不同协议的服务暴露或引用。

dubbo服务的暴露调用的是:ServiceConfig.export()代码如下:
com.alibaba.dubbo.config.ServiceConfig#export

//暴露服务 
    public synchronized void export() { 
        if (provider != null) { 
            if (export == null) { 
                export = provider.getExport(); 
            } 
            if (delay == null) { 
                delay = provider.getDelay(); 
            } 
        } 
        if (export != null && ! export.booleanValue()) { 
            return; 
        } 
        if (delay != null && delay > 0) { 
            Thread thread = new Thread(new Runnable() { 
                public void run() { 
                    try { 
                        Thread.sleep(delay); 
                    } catch (Throwable e) { 
                    } 
                    doExport(); 
                } 
            }); 
            thread.setDaemon(true); 
            thread.setName("DelayExportServiceThread"); 
            thread.start(); 
        } else { 
            doExport(); 
        } 
    } 

在查看export调用链时,可看到2个地方调用了该方法:
1、com.alibaba.dubbo.config.spring.AnnotationBean#postProcessAfterInitialization:注解的方式暴露时
2、com.alibaba.dubbo.config.spring.ServiceBean#afterPropertiesSet:以spring配置文件暴露时

AnnotationBean类的继承关系
public class AnnotationBean extends AbstractConfig implements DisposableBean, BeanFactoryPostProcessor, BeanPostProcessor, ApplicationContextAware { 

AnnotationBean实现了spring bean和context相关的接口,在spring扫描完注解类,并解析完时调用 export()方法对服务进行暴露

ServiceBean
public class ServiceBean extends ServiceConfig implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware { 
在spring初始化解析bean完成,主要是在对spring标签的解析,bean的定义,bean的属性解析设值等完成后 进行 export()

因为dubbo是自己的自定义标签,所以对于bean的解析是 export 前最重要的部分,今天先不看服务的暴露,先看dubbo对于服务的解析,重要的两个类:
com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
com.alibaba.dubbo.config.spring.schema.DubboBeanDefinitionParser#parse

以下为DubboNamespaceHandler代码,加上了我的注释(自己的理解)
public class DubboNamespaceHandler extends NamespaceHandlerSupport { 
 
    static { 
        /**
         * 检索是否有重复的命名空间处理器
         */ 
            Version.checkDuplicate(DubboNamespaceHandler.class); 
    } 
 
    public void init() { 
        /**
         * 注册bean,真正负解析的是DubboBeanDefinitionParser
         * DubboBeanDefinitionParser将解析所有的属性,并将属性值放入BeanDefinition
         */ 
        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)); 
    } 
 

所有的解析工作都在 DubboBeanDefinitionParser 中

/**
     * 解析dubbo自定义标签,往BeanDefinition设置属性值,这个时候bean还没有创建
     * @param element
     * @param parserContext
     * @param beanClass
     * @param required
     * @return
     */ 
    @SuppressWarnings("unchecked") 
    private static BeanDefinition parse(Element element, ParserContext parserContext, Class beanClass, boolean required) { 
        RootBeanDefinition beanDefinition = new RootBeanDefinition(); 
        beanDefinition.setBeanClass(beanClass); 
        //设置懒加载为false,表示立即加载,spring启动时,立刻进行实例化 
        //如果设置为true,那么要第一次向容器通过getBean索取bean时实例化,在spring bean的配置里可以配置 
        //这里会设置懒加载为false其实还可以得到一个推断就是:dubbo标签创建的bean就是单例bean(singleton bean) 
        //因为lazy-init的设置只对singleton bean有效,对原型bean(prototype无效) 
        beanDefinition.setLazyInit(false); 
        String id = element.getAttribute("id"); 
        //如果没有设置bean的id 
        if ((id == null || id.length() == 0) && required) { 
            String generatedBeanName = element.getAttribute("name"); 
            //name没有配置 
            if (generatedBeanName == null || generatedBeanName.length() == 0) { 
                //如果是ProtocolConfig类型,bean name默认为 dubbo,其他的为配置的interface值 
                if (ProtocolConfig.class.equals(beanClass)) { 
                    generatedBeanName = "dubbo"; 
                } else { 
                    generatedBeanName = element.getAttribute("interface"); 
                } 
            } 
            /*
             * 如果还为null 那么取 beanClass 的名字,beanClass 其实就是要解析的类型
             * 如:com.alibaba.dubbo.config.ApplicationConfig
             */ 
            if (generatedBeanName == null || generatedBeanName.length() == 0) { 
                generatedBeanName = beanClass.getName(); 
            } 
            //如果id没有设置那么 id = generatedBeanName,如果是ProtocolConfig类型的话自然就是 dubbo 
            id = generatedBeanName;  
            int counter = 2; 
            /*
             * 由于spring的bean id不能重复,但有些标签可能会配置多个如:             * 所以 id 在后面加数字 2、3、4 区分
             */ 
            while(parserContext.getRegistry().containsBeanDefinition(id)) { 
                id = generatedBeanName + (counter ++); 
            } 
        } 
        if (id != null && id.length() > 0) { 
            //检查是否有 bean id 相同的 
            if (parserContext.getRegistry().containsBeanDefinition(id))  { 
                throw new IllegalStateException("Duplicate spring bean id " + id); 
            } 
            /*
             * 注册 bean 定义
             * org.springframework.beans.factory.support.DefaultListableBeanFactory#registerBeanDefinition
             * 会按照 id 即beanName做一些检查,判断是否重载已加载过的bean等等
             * 跟到代码里其实 bean 的注册也是放到 ConcurrentHashMap 里
             * beanName也就是这里的 id 会放到 list 里
             */ 
            parserContext.getRegistry().registerBeanDefinition(id, beanDefinition); 
            //给bean添加属性值 
            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); 
                /*
                    解析子节点,有些配置可能是:
                                             executes="10" >
                       
                       
                    

                 */ 
                parseProperties(element.getChildNodes(), classDefinition); 
                /*
                    ref直接设置成了 接口名 + Impl 的bean ?
                    如:com.alihealth.dubbo.api.drugInfo.service.DemoService  + Impl 的bean为啥?
                    那                 */ 
                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 props = new HashSet(); 
        ManagedMap parameters = null; 
        for (Method setter : beanClass.getMethods()) { 
            String name = setter.getName(); 
            //给model注入值时,如ServiceConfig,方法必须是set开头,并且参数只能为1 
            if (name.length() > 3 && name.startsWith("set") 
                    && Modifier.isPublic(setter.getModifiers()) 
                    && setter.getParameterTypes().length == 1) { 
                //方法参数类型,因为参数只能是1,所以直接取[0] 
                Class type = setter.getParameterTypes()[0]; 
                //根据set方法名获取属性值,如:setListener 得到的属性为:listener 
                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,如ProtocolConfig里的setParameters(Map parameters)
                     * 那么去子节点获取                      *
                        
                        
                    

                     */ 
                    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) { 
                            //不发布到任何注册中心时 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)) {//如果参数类型为 java 的基本类型 
                                    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)) { 
                                      /*
                                        兼容旧版本xsd中的default值,以上配置的值在xsd中有配置defalt值
                                       
                                      */ 
                                        value = null; 
                                    } 
                                    reference = value; 
                                } else if ("protocol".equals(property) 
                                        //如果属性为 protocol 那么要判断protocol对应的拓展点配置有没有 
                                        && 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 to "); 
                                    } 
                                    // 兼容旧版本配置 
                                    ProtocolConfig protocol = new ProtocolConfig(); 
                                    protocol.setName(value); 
                                    reference = protocol; 
                                } else if ("monitor".equals(property) 
                                        //同上 
                                        && (! parserContext.getRegistry().containsBeanDefinition(value) 
                                                || ! MonitorConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) { 
                                    // 兼容旧版本配置 
                                    reference = convertMonitor(value); 
                                } else if ("onreturn".equals(property)) { 
                                    //回调方法 类似onSuccess 
                                    int index = value.lastIndexOf("."); 
                                    // bean的名字 
                                    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)) { 
                                    //回调 异常执行的方法 ,类似 onError 
                                    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 ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) { 
                                        BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value); 
                                        /*
                                            必须是单例bean(singleton),原型bean(prototype)不行,sevice初始化一次,在spring容器里也只有一个 实例
                                            是不是和dubbo的幂等有关,如果为原型bean,那么服务就变成有状态的了
                                         */ 
                                        if (! refBean.isSingleton()) { 
                                            throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: "); 
                                        } 
                                    } 
                                    reference = new RuntimeBeanReference(value); 
                                } 
                                /*
                                    设置属性,值为另外一个关联的bean
                                    RuntimeBeanReference 固定占位符类,当在beanfactory中作为另外一个bean的引用时,作为属性值对象,将在运行时进行解析
                                 */ 
                                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; 
    } 
 
    private static final Pattern GROUP_AND_VERION = Pattern.compile("^[\\-.0-9_a-zA-Z]+(\\:[\\-.0-9_a-zA-Z]+)?$"); 
     
    protected static MonitorConfig convertMonitor(String monitor) { 
        if (monitor == null || monitor.length() == 0) { 
            return null; 
        } 
        if (GROUP_AND_VERION.matcher(monitor).matches()) { 
            String group; 
            String version; 
            int i = monitor.indexOf(':'); 
            if (i > 0) { 
                group = monitor.substring(0, i); 
                version = monitor.substring(i + 1); 
            } else { 
                group = monitor; 
                version = null; 
            } 
            MonitorConfig monitorConfig = new MonitorConfig(); 
            monitorConfig.setGroup(group); 
            monitorConfig.setVersion(version); 
            return monitorConfig; 
        } 
        return null; 
    } 
  
    private static boolean isPrimitive(Class cls) { 
        return cls.isPrimitive() || cls == Boolean.class || cls == Byte.class 
                || cls == Character.class || cls == Short.class || cls == Integer.class 
                || cls == Long.class || cls == Float.class || cls == Double.class 
                || cls == String.class || cls == Date.class || cls == Class.class; 
    } 
     
    @SuppressWarnings("unchecked") 
    private static void parseMultiRef(String property, String value, RootBeanDefinition beanDefinition, 
            ParserContext parserContext) { 
        //解析 registries 、providers、protocols 时支持多引用 
        String[] values = value.split("\\s*[,]+\\s*"); 
        ManagedList list = null; 
        for (int i = 0; i < values.length; i++) { 
            String v = values[i]; 
            if (v != null && v.length() > 0) { 
                if (list == null) { 
                    list = new ManagedList(); 
                } 
                list.add(new RuntimeBeanReference(v)); 
            } 
        } 
        beanDefinition.getPropertyValues().addPropertyValue(property, list); 
    } 
     
    private static void parseNested(Element element, ParserContext parserContext, Class beanClass, 
                                    boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) { 
        NodeList nodeList = element.getChildNodes(); 
        if (nodeList != null && nodeList.getLength() > 0) { 
            boolean first = true; 
            for (int i = 0; i < nodeList.getLength(); i++) { 
                Node node = nodeList.item(i); 
                if (node instanceof Element) { 
                    if (tag.equals(node.getNodeName()) 
                            || tag.equals(node.getLocalName())) { 
                        if (first) { 
                            first = false; 
                            String isDefault = element.getAttribute("default"); 
                            /*
                                如果                                 这样做的目的是为了让                              */ 
                            if (isDefault == null || isDefault.length() == 0) { 
                                beanDefinition.getPropertyValues().addPropertyValue("default", "false"); 
                            } 
                        } 
                        BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required); 
                        if (subDefinition != null && ref != null && ref.length() > 0) { 
                            subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref)); 
                        } 
                    } 
                } 
            } 
        } 
    } 
 
    private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) { 
        if (nodeList != null && nodeList.getLength() > 0) { 
            for (int i = 0; i < nodeList.getLength(); i++) { 
                Node node = nodeList.item(i); 
                if (node instanceof Element) { 
                    //如果是                     if ("property".equals(node.getNodeName()) 
                            || "property".equals(node.getLocalName())) { 
                        String name = ((Element) node).getAttribute("name"); 
                        if (name != null && name.length() > 0) { 
                            String value = ((Element) node).getAttribute("value"); 
                            //获取 ref 
                            String ref = ((Element) node).getAttribute("ref"); 
                            if (value != null && value.length() > 0) { 
                                beanDefinition.getPropertyValues().addPropertyValue(name, value); 
                            } else if (ref != null && ref.length() > 0) { 
                                beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref)); 
                            } else { 
                                /*
                                    只支持两种property的设置方法:
                                   
                                   
                                 */ 
                                throw new UnsupportedOperationException("Unsupported sub tag, Only supported or "); 
                            } 
                        } 
                    } 
                } 
            } 
        } 
    } 
 
    @SuppressWarnings("unchecked") 
    private static ManagedMap parseParameters(NodeList nodeList, RootBeanDefinition beanDefinition) { 
        if (nodeList != null && nodeList.getLength() > 0) { 
            ManagedMap parameters = null; 
            for (int i = 0; i < nodeList.getLength(); i++) { 
                Node node = nodeList.item(i); 
                if (node instanceof Element) { 
                    //解析                     if ("parameter".equals(node.getNodeName()) 
                            || "parameter".equals(node.getLocalName())) { 
                        if (parameters == null) { 
                            parameters = new ManagedMap(); 
                        } 
                        String key = ((Element) node).getAttribute("key"); 
                        String value = ((Element) node).getAttribute("value"); 
                        boolean hide = "true".equals(((Element) node).getAttribute("hide")); 
                        if (hide) { 
                            key = Constants.HIDE_KEY_PREFIX + key; 
                        } 
                        //添加参数,String 类型 
                        parameters.put(key, new TypedStringValue(value, String.class)); 
                    } 
                } 
            } 
            return parameters; 
        } 
        return null; 
    } 
 
    @SuppressWarnings("unchecked") 
    private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition, 
                              ParserContext parserContext) { 
        if (nodeList != null && nodeList.getLength() > 0) { 
            ManagedList methods = null; 
            for (int i = 0; i < nodeList.getLength(); i++) { 
                Node node = nodeList.item(i); 
                if (node instanceof Element) { 
                    Element element = (Element) node; 
                    //                    if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) { 
                        String methodName = element.getAttribute("name"); 
                        if (methodName == null || methodName.length() == 0) { 
                            throw new IllegalStateException(" name attribute == null"); 
                        } 
                        if (methods == null) { 
                            methods = new ManagedList(); 
                        } 
                        //解析                         BeanDefinition methodBeanDefinition = parse(((Element) node), 
                                parserContext, MethodConfig.class, false); 
                        String name = id + "." + methodName; 
                        BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder( 
                                methodBeanDefinition, name); 
                        methods.add(methodBeanDefinitionHolder); 
                    } 
                } 
            } 
            if (methods != null) { 
                beanDefinition.getPropertyValues().addPropertyValue("methods", methods); 
            } 
        } 
    } 
     
解析的最终目的是返回 RootBeanDefinition 对象,RootBeanDefinition包含了解析出来的关于bean的所有信息,注意在bean的解析完后其实只是spring将其转化成spring内部的一种抽象的数据对象结构,bean的创建(实例化)是第一次调用 getBean 时创建的。以上是dubbo对配置文件,服务定义的解析过程。后面再写dubbo服务的暴露。

关注我获取视频
dubbo源码学习(四)初始化过程细节:解析服务_第1张图片
  • dubbo源码学习(四)初始化过程细节:解析服务_第2张图片
  • 大小: 39.9 KB
  • 查看图片附件

你可能感兴趣的:(spring,java,dubbo)