• dubbo源码分析14——DubboProtocol的export方法分析


    走到了这一步也挺不容易的,把之前的暴露入口代码再列出来回顾一下:

    //配置为none不暴露
            if (! Constants.SCOPE_NONE.toString().equalsIgnoreCase(scope)) {
    
                //配置不是remote的情况下做本地暴露 (配置为remote,则表示只暴露远程服务)
                if (!Constants.SCOPE_REMOTE.toString().equalsIgnoreCase(scope)) {
                    exportLocal(url);  //本地暴露
                }
                //如果配置不是local则暴露为远程服务.(配置为local,则表示只暴露远程服务)
                if (! Constants.SCOPE_LOCAL.toString().equalsIgnoreCase(scope) ){
                    if (logger.isInfoEnabled()) {
                        logger.info("Export dubbo service " + interfaceClass.getName() + " to url " + url);
                    }
    //远程暴露
    if (registryURLs != null && registryURLs.size() > 0 && url.getParameter("register", true)) { for (URL registryURL : registryURLs) { url = url.addParameterIfAbsent("dynamic", registryURL.getParameter("dynamic")); URL monitorUrl = loadMonitor(registryURL); if (monitorUrl != null) { url = url.addParameterAndEncoded(Constants.MONITOR_KEY, monitorUrl.toFullString()); } if (logger.isInfoEnabled()) { logger.info("Register dubbo service " + interfaceClass.getName() + " url " + url + " to registry " + registryURL); } Invoker<?> invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString())); Exporter<?> exporter = protocol.export(invoker); exporters.add(exporter); } } else { //远程暴露的直连方式 Invoker<?> invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, url); Exporter<?> exporter = protocol.export(invoker); exporters.add(exporter); } } }

    下面列出具体dubbo协议的暴露源码 (即 Exporter<?> exporter = protocol.export(invoker)):

    public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
            URL url = invoker.getUrl();
            
            // export service.
            String key = serviceKey(url);   // 详见注1
            DubboExporter<T> exporter = new DubboExporter<T>(invoker, key, exporterMap);
            exporterMap.put(key, exporter);
            
            //export an stub service for dispaching 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);
    
            // modified by lishen
            optimizeSerialization(url);
    
            return exporter;
        }

     注1:

      key的格式:   gruop/serviceName:version:port

    public static String serviceKey(int port, String serviceName, String serviceVersion, String serviceGroup) {
            StringBuilder buf = new StringBuilder();
            if (serviceGroup != null && serviceGroup.length() > 0) {
                buf.append(serviceGroup);
                buf.append("/");
            }
            buf.append(serviceName);
            if (serviceVersion != null && serviceVersion.length() > 0 && !"0.0.0".equals(serviceVersion)) {
                buf.append(":");
                buf.append(serviceVersion);
            }
            buf.append(":");
            buf.append(port);
            return buf.toString();
        }
  • 相关阅读:
    java项目配置域名(tomcat直接配置 or 使用nginx反向代理)
    java爬虫学习
    python爬虫
    log4j的日志级别(ssm中log4j的配置)
    Python中list,tuple,dict,set的区别和用法
    [转]C# Eval在asp.net中的用法及作用
    【转】一个运维经理的运维经验总结
    [转]使用 LVS 实现负载均衡原理及安装配置详解
    [转]33个网站足以使你成为一个天才
    搜狐邮箱想说爱你不容易!
  • 原文地址:https://www.cnblogs.com/hzhuxin/p/8228982.html
Copyright © 2020-2023  润新知