美文网首页
1.dubbo服务暴露机制

1.dubbo服务暴露机制

作者: 未知的证明 | 来源:发表于2019-10-15 12:43 被阅读0次

1.dubbo解析xml标签

涉及的类为DubboBeanDefinitionParser,parse类会调用重载方法parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required),然后会根据读取的不同的标签进行解析。

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);
            }
        }
    }
}


return beanDefinition;

2.服务暴露

关于服务暴露主要是ServiceBeanServiceBean分为实现了InitializingBeanApplicationListener
afterPropertiesSet()完成了配置参数的赋值。例如 application呀,module呀,registry呀等等。数据封装好了就可以暴露服务了。

  public void afterPropertiesSet() throws Exception {
      
        ……

        if (getApplication() == null
                && (getProvider() == null || getProvider().getApplication() == null)) {
            Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false);
            if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
                ApplicationConfig applicationConfig = null;
                for (ApplicationConfig config : applicationConfigMap.values()) {
                    if (config.isDefault() == null || config.isDefault().booleanValue()) {
                        if (applicationConfig != null) {
                            throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config);
                        }
                        applicationConfig = config;
                    }
                }
                if (applicationConfig != null) {
                    setApplication(applicationConfig);
                }
            }
        }
      ……
}

3.服务暴露

由于实现了ApplicationListener,所以在整个Spring刷新容器的时候,将服务暴露出去

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (isDelay() && !isExported() && !isUnexported()) {
            if (logger.isInfoEnabled()) {
                logger.info("The service ready on spring started. service: " + getInterface());
            }
            export();
        }
    }

4. export()的最核心的部分,具体暴露情况:

 doExportUrls();
 private void doExportUrls() {
        List<URL> registryURLs = loadRegistries(true);
        for (ProtocolConfig protocolConfig : protocols) {
            doExportUrlsFor1Protocol(protocolConfig, registryURLs);
        }
    }

5.会调用doLocalExport(final Invoker<T> originInvoker),在这里会调用dubboProtocol

private <T> ExporterChangeableWrapper<T> doLocalExport(final Invoker<T> originInvoker) {
    String key = getCacheKey(originInvoker);
    ExporterChangeableWrapper<T> exporter = (ExporterChangeableWrapper<T>) bounds.get(key);
    if (exporter == null) {
        synchronized (bounds) {
            exporter = (ExporterChangeableWrapper<T>) bounds.get(key);
            if (exporter == null) {
                final Invoker<?> invokerDelegete = new InvokerDelegete<T>(originInvoker, getProviderUrl(originInvoker));
                exporter = new ExporterChangeableWrapper<T>((Exporter<T>) protocol.export(invokerDelegete), originInvoker);
                bounds.put(key, exporter);
            }
        }
    }
    return exporter;
}

6.dubbo的export主要做Netty的服务调用,端口20880

  @Override
    public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
        URL url = invoker.getUrl();

        // export service.
        String key = serviceKey(url);
        DubboExporter<T> exporter = new DubboExporter<T>(invoker, key, exporterMap);
        exporterMap.put(key, exporter);

        //export an stub service for dispatching event
        Boolean isStubSupportEvent = url.getParameter(Constants.STUB_EVENT_KEY, Constants.DEFAULT_STUB_EVENT);
        Boolean isCallbackservice = url.getParameter(Constants.IS_CALLBACK_SERVICE, false);
        if (isStubSupportEvent && !isCallbackservice) {
            String stubServiceMethods = url.getParameter(Constants.STUB_EVENT_METHODS_KEY);
            if (stubServiceMethods == null || stubServiceMethods.length() == 0) {
                if (logger.isWarnEnabled()) {
                    logger.warn(new IllegalStateException("consumer [" + url.getParameter(Constants.INTERFACE_KEY) +
                            "], has set stubproxy support event ,but no stub methods founded."));
                }
            } else {
                stubServiceMethodsMap.put(url.getServiceKey(), stubServiceMethods);
            }
        }

        openServer(url);
        optimizeSerialization(url);
        return exporter;
    }

相关文章

  • 1.dubbo服务暴露机制

    1.dubbo解析xml标签 涉及的类为DubboBeanDefinitionParser,parse类会调用重载...

  • Dubbo源码解析

    Dubbo与spring整合、SPI拓展机制、服务暴露、服务引用、容错机制、预热。 Dubbo架构图(取自dubb...

  • dubbo-Reference注解改进

    1.dubbo服务过多引用的问题 1.1 dubbo服务以xml配置消费者 由于引用多个dubbo服务时,在未使用...

  • SpringBootAdmin微服务监控

    创建Server SpringBootAdmin通过收集actuator暴露出来的服务信息以及通过心跳检测的机制判...

  • Dubbo服务暴露

    1 Dubbo服务暴露介绍 Dubbo服务暴露的整体流程如下: 2 Dubbo服务暴露源码 2.1 延时暴露 Se...

  • dubbo框架原理

    (1)介绍dubbo原理,暴露服务过程。下图是暴露服务流程: 首先dubbo暴露服务有两种情况: (1)延时暴露(...

  • 001--最简单Dubbo项目

    话题一:搭建Dubbo项目 1.Dubbo依赖zookeeper进行服务注册与发现:先搭建zookeeper并运行...

  • Dubbo 服务暴露 总结(五)

    笔记简述Dubbo服务暴露之前分为了两小节Dubbo 服务暴露 源码学习(上)(三) 和Dubbo 服务暴露 源码...

  • Druid 监控

    1.dubbo服务提供者非web项目 项目启动加上如下参数:-Djava.net.preferIPv4Stack=...

  • 1.dubbo概论

    1.dubbo 一个分布式、高性能、透明化的RPC服务框架 提供服务自动注册、自动发现等高效服务治理方案 其功能主...

网友评论

      本文标题:1.dubbo服务暴露机制

      本文链接:https://www.haomeiwen.com/subject/qitsmctx.html