我是靠谱客的博主 迷路爆米花,最近开发中收集的这篇文章主要介绍[总结]Dubbo 自适应扩展学习笔记,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Dubbo 自适应扩展学习笔记

文章目录

      • Dubbo 自适应扩展学习笔记
        • 1.创建自适应扩展类-思维导图
        • 2.创建流程
          • 创建流程概述
          • 源码赏析

1.创建自适应扩展类-思维导图

在这里插入图片描述

2.创建流程

创建流程概述
  • dubbo在启动的时候,会扫描SPI接口,对配置@Adaptive 注解的SPI方法,自动创建以下自适应扩展类,顺序如下:Protocol$AdaptiveProxyFactory$AdaptiveCacheFactory$AdaptiveValidation$AdaptiveCluster$AdaptiveRegistryFactory$AdaptiveTransporter$AdaptiveDispatcher$AdaptiveThreadPool$AdaptiveZookeeperTransporter$AdaptiveRouterFactory$AdaptiveConfiguratorFactory$Adaptive

    如果SPI 接口方法没有@Adaptive 也会生成对应方法,只不过是直接抛异常,不对外提供服务。

源码赏析
  • Protocol$Adaptive.java 源码
package com.alibaba.dubbo.rpc;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class Protocol$Adaptive implements com.alibaba.dubbo.rpc.Protocol {
  public void destroy() {
    throw new UnsupportedOperationException("method public abstract void com.alibaba.dubbo.rpc.Protocol.destroy() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
  }
  public int getDefaultPort() {
    throw new UnsupportedOperationException("method public abstract int com.alibaba.dubbo.rpc.Protocol.getDefaultPort() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
  }
  public com.alibaba.dubbo.rpc.Exporter export(com.alibaba.dubbo.rpc.Invoker arg0) throws com.alibaba.dubbo.rpc.RpcException {
    if (arg0 == null)
      throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");

    if (arg0.getUrl() == null)
      throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");

    com.alibaba.dubbo.common.URL url = arg0.getUrl();
    String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );

    if(extName == null)
      throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");

    com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class)
        .getExtension(extName);

    return extension.export(arg0);
  }
  public com.alibaba.dubbo.rpc.Invoker refer(java.lang.Class arg0, com.alibaba.dubbo.common.URL arg1) throws com.alibaba.dubbo.rpc.RpcException {
    if (arg1 == null)
      throw new IllegalArgumentException("url == null");

    com.alibaba.dubbo.common.URL url = arg1;
    String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );

    if(extName == null)
      throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");

    com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class)
        .getExtension(extName);
    
    return extension.refer(arg0, arg1);
  }
}
  • ProxyFactory$Adaptive
package com.alibaba.dubbo.rpc;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class ProxyFactory$Adaptive implements com.alibaba.dubbo.rpc.ProxyFactory {

  public com.alibaba.dubbo.rpc.Invoker getInvoker(java.lang.Object arg0, java.lang.Class arg1,
      com.alibaba.dubbo.common.URL arg2) throws com.alibaba.dubbo.rpc.RpcException {
    if (arg2 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg2;
    String extName = url.getParameter("proxy", "javassist");
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url(" + url
              .toString() + ") use keys([proxy])");
    }
    com.alibaba.dubbo.rpc.ProxyFactory extension = (com.alibaba.dubbo.rpc.ProxyFactory) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName);
    return extension.getInvoker(arg0, arg1, arg2);
  }

  public java.lang.Object getProxy(com.alibaba.dubbo.rpc.Invoker arg0)
      throws com.alibaba.dubbo.rpc.RpcException {
    if (arg0 == null) {
      throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");
    }
    if (arg0.getUrl() == null) {
      throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");
    }
    com.alibaba.dubbo.common.URL url = arg0.getUrl();
    String extName = url.getParameter("proxy", "javassist");
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url(" + url
              .toString() + ") use keys([proxy])");
    }
    com.alibaba.dubbo.rpc.ProxyFactory extension = (com.alibaba.dubbo.rpc.ProxyFactory) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName);
    return extension.getProxy(arg0);
  }
}
  • CacheFactory$Adaptive 源码
package com.alibaba.dubbo.cache;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class CacheFactory$Adaptive implements com.alibaba.dubbo.cache.CacheFactory {

  public com.alibaba.dubbo.cache.Cache getCache(com.alibaba.dubbo.common.URL arg0) {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getParameter("cache", "lru");
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.cache.CacheFactory) name from url(" + url
              .toString() + ") use keys([cache])");
    }
    com.alibaba.dubbo.cache.CacheFactory extension = (com.alibaba.dubbo.cache.CacheFactory) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.cache.CacheFactory.class).getExtension(extName);
    return extension.getCache(arg0);
  }
}
  • Validation$Adaptive 源码
package com.alibaba.dubbo.validation;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class Validation$Adaptive implements com.alibaba.dubbo.validation.Validation {

  public com.alibaba.dubbo.validation.Validator getValidator(com.alibaba.dubbo.common.URL arg0) {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getParameter("validation", "jvalidation");
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.validation.Validation) name from url(" + url
              .toString() + ") use keys([validation])");
    }
    com.alibaba.dubbo.validation.Validation extension = (com.alibaba.dubbo.validation.Validation) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.validation.Validation.class).getExtension(extName);
    return extension.getValidator(arg0);
  }
}
  • Cluster$Adaptive 源码
package com.alibaba.dubbo.rpc.cluster;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class Cluster$Adaptive implements com.alibaba.dubbo.rpc.cluster.Cluster {

  public com.alibaba.dubbo.rpc.Invoker join(com.alibaba.dubbo.rpc.cluster.Directory arg0)
      throws com.alibaba.dubbo.rpc.RpcException {
    if (arg0 == null) {
      throw new IllegalArgumentException(
          "com.alibaba.dubbo.rpc.cluster.Directory argument == null");
    }
    if (arg0.getUrl() == null) {
      throw new IllegalArgumentException(
          "com.alibaba.dubbo.rpc.cluster.Directory argument getUrl() == null");
    }
    com.alibaba.dubbo.common.URL url = arg0.getUrl();
    String extName = url.getParameter("cluster", "failover");
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.rpc.cluster.Cluster) name from url(" + url
              .toString() + ") use keys([cluster])");
    }
    com.alibaba.dubbo.rpc.cluster.Cluster extension = (com.alibaba.dubbo.rpc.cluster.Cluster) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.rpc.cluster.Cluster.class).getExtension(extName);
    return extension.join(arg0);
  }
}
  • RegistryFactory$Adaptive 源码
package com.alibaba.dubbo.registry;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class RegistryFactory$Adaptive implements com.alibaba.dubbo.registry.RegistryFactory {

  public com.alibaba.dubbo.registry.Registry getRegistry(com.alibaba.dubbo.common.URL arg0) {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.registry.RegistryFactory) name from url(" + url
              .toString() + ") use keys([protocol])");
    }
    com.alibaba.dubbo.registry.RegistryFactory extension = (com.alibaba.dubbo.registry.RegistryFactory) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.registry.RegistryFactory.class).getExtension(extName);
    return extension.getRegistry(arg0);
  }
}
  • Transporter$Adapter 源码
package com.alibaba.dubbo.remoting;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class Transporter$Adaptive implements com.alibaba.dubbo.remoting.Transporter {

  public com.alibaba.dubbo.remoting.Client connect(com.alibaba.dubbo.common.URL arg0,
      com.alibaba.dubbo.remoting.ChannelHandler arg1)
      throws com.alibaba.dubbo.remoting.RemotingException {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getParameter("client", url.getParameter("transporter", "netty"));
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.remoting.Transporter) name from url(" + url
              .toString() + ") use keys([client, transporter])");
    }
    com.alibaba.dubbo.remoting.Transporter extension = (com.alibaba.dubbo.remoting.Transporter) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.remoting.Transporter.class).getExtension(extName);
    return extension.connect(arg0, arg1);
  }

  public com.alibaba.dubbo.remoting.Server bind(com.alibaba.dubbo.common.URL arg0,
      com.alibaba.dubbo.remoting.ChannelHandler arg1)
      throws com.alibaba.dubbo.remoting.RemotingException {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getParameter("server", url.getParameter("transporter", "netty"));
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.remoting.Transporter) name from url(" + url
              .toString() + ") use keys([server, transporter])");
    }
    com.alibaba.dubbo.remoting.Transporter extension = (com.alibaba.dubbo.remoting.Transporter) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.remoting.Transporter.class).getExtension(extName);
    return extension.bind(arg0, arg1);
  }
}
  • Dispatcher$Adaptive 源码
package com.alibaba.dubbo.remoting;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class Dispatcher$Adaptive implements com.alibaba.dubbo.remoting.Dispatcher {

  public com.alibaba.dubbo.remoting.ChannelHandler dispatch(
      com.alibaba.dubbo.remoting.ChannelHandler arg0, com.alibaba.dubbo.common.URL arg1) {
    if (arg1 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg1;
    String extName = url.getParameter("dispatcher",
        url.getParameter("dispather", url.getParameter("channel.handler", "all")));
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.remoting.Dispatcher) name from url(" + url
              .toString() + ") use keys([dispatcher, dispather, channel.handler])");
    }
    com.alibaba.dubbo.remoting.Dispatcher extension = (com.alibaba.dubbo.remoting.Dispatcher) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.remoting.Dispatcher.class).getExtension(extName);
    return extension.dispatch(arg0, arg1);
  }
}
  • ThreadPool$Adaptive 源码
package com.alibaba.dubbo.common.threadpool;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class ThreadPool$Adaptive implements com.alibaba.dubbo.common.threadpool.ThreadPool {

  public java.util.concurrent.Executor getExecutor(com.alibaba.dubbo.common.URL arg0) {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getParameter("threadpool", "fixed");
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.common.threadpool.ThreadPool) name from url("
              + url.toString() + ") use keys([threadpool])");
    }
    com.alibaba.dubbo.common.threadpool.ThreadPool extension = (com.alibaba.dubbo.common.threadpool.ThreadPool) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.common.threadpool.ThreadPool.class)
        .getExtension(extName);
    return extension.getExecutor(arg0);
  }
}
  • ZookeeperTransporter$Adaptive 源码
package com.alibaba.dubbo.remoting.zookeeper;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class ZookeeperTransporter$Adaptive implements
    com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter {

  public com.alibaba.dubbo.remoting.zookeeper.ZookeeperClient connect(
      com.alibaba.dubbo.common.URL arg0) {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getParameter("client", url.getParameter("transporter", "curator"));
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter) name from url("
              + url.toString() + ") use keys([client, transporter])");
    }
    com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter extension = (com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter.class)
        .getExtension(extName);
    return extension.connect(arg0);
  }
}
  • RouterFactory$Adaptive 源码
package com.alibaba.dubbo.rpc.cluster;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class RouterFactory$Adaptive implements com.alibaba.dubbo.rpc.cluster.RouterFactory {

  public com.alibaba.dubbo.rpc.cluster.Router getRouter(com.alibaba.dubbo.common.URL arg0) {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getProtocol();
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.rpc.cluster.RouterFactory) name from url(" + url
              .toString() + ") use keys([protocol])");
    }
    com.alibaba.dubbo.rpc.cluster.RouterFactory extension = (com.alibaba.dubbo.rpc.cluster.RouterFactory) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.rpc.cluster.RouterFactory.class)
        .getExtension(extName);
    return extension.getRouter(arg0);
  }
}
  • ConfiguratorFactory$Adaptive 源码
package com.alibaba.dubbo.rpc.cluster;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class ConfiguratorFactory$Adaptive implements
    com.alibaba.dubbo.rpc.cluster.ConfiguratorFactory {

  public com.alibaba.dubbo.rpc.cluster.Configurator getConfigurator(
      com.alibaba.dubbo.common.URL arg0) {
    if (arg0 == null) {
      throw new IllegalArgumentException("url == null");
    }
    com.alibaba.dubbo.common.URL url = arg0;
    String extName = url.getProtocol();
    if (extName == null) {
      throw new IllegalStateException(
          "Fail to get extension(com.alibaba.dubbo.rpc.cluster.ConfiguratorFactory) name from url("
              + url.toString() + ") use keys([protocol])");
    }
    com.alibaba.dubbo.rpc.cluster.ConfiguratorFactory extension = (com.alibaba.dubbo.rpc.cluster.ConfiguratorFactory) ExtensionLoader
        .getExtensionLoader(com.alibaba.dubbo.rpc.cluster.ConfiguratorFactory.class)
        .getExtension(extName);
    return extension.getConfigurator(arg0);
  }
}

最后

以上就是迷路爆米花为你收集整理的[总结]Dubbo 自适应扩展学习笔记的全部内容,希望文章能够帮你解决[总结]Dubbo 自适应扩展学习笔记所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部