3.dubbo源码-ExtensionLoader扩展机制

ExtensionLoader的使用

Dubbo中随处可见这样的代码:

ExtensionLoader.getExtensionLoader(Transporter.class).getAdaptiveExtension();
ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url);

这段代码非常重要,了解它的实现是掌握dubbo源码的重中之重,否则很多地方的源码会不知其所以然,接下来以获取ThreadPool为例,讲解dubbo如何通过ExtensionLoader根据url获取具体的ThreadPool;

Provider&ThreadPool

Dubbo服务端接收到所有ChannelState类型的请求,都在com.alibaba.dubbo.remoting.transport.dispatcher.all.AllChannelHandler中处理,以Provider端处理接收到的请求为例,处理的代码如下:

public void received(Channel channel, Object message) throws RemotingException {
    ExecutorService cexecutor = getExecutorService();
    try {
        cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message));
    } catch (Throwable t) {
        throw new ExecutionException(message, channel, getClass() + " error when process received event .", t);
    }
}

getExecutorService()就是获取连接池ExecutorService的;对应的就是父类WrappedChannelHandler中的申明protected final ExecutorService executor;在new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message)时,调用父类WrappedChannelHandler的构造方法时初始化,源码如下:

public WrappedChannelHandler(ChannelHandler handler, URL url) {
    this.handler = handler;
    this.url = url;
    executor = (ExecutorService) ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url);

    String componentKey = Constants.EXECUTOR_SERVICE_COMPONENT_KEY;
    if (Constants.CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(Constants.SIDE_KEY))) {
        componentKey = Constants.CONSUMER_SIDE;
    }
    DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension();
    dataStore.put(componentKey, Integer.toString(url.getPort()), executor);
}

从这段代码可知,获取ExecutorService的代码就是ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url);,接下来我们看看dubbo如何通过这一行代码获取具体的ThreadPool;

ExtensionLoader

这个类非常重要,几乎贯穿在dubbo中每一个模块,例如Protocol,LoadBalance等;因为dubbo采用SPI微内核实现方式,所以外部可以从容扩展自定义实现,例如自定义实现一种线程池AfeiThreadPool,而dubbo决定具体采用哪个实现类,都在这个类中决定,ExtensionLoader主要有两大功能:1、获取ExtensionLoader实例2、获取Adaptive实例。调用方得到Adaptive实例后就能调用具体业务实现,例如FixedThreadPool,下面的分析以获取ThreadPool的实现为例;

1. 获取ExtensionLoader实例

首先根据ThreadPool得到ExtensionLoader,实现源码如下:

public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
    if (type == null)
        throw new IllegalArgumentException("Extension type == null");
    // 获取ExtensionLoader时参数即ThreadPool必须是接口类型
    if(!type.isInterface()) {
        throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
    }
    // 获取ExtensionLoader时参数即ThreadPool必须有SPI注解,否则不可以扩展
    if(!withExtensionAnnotation(type)) {
        throw new IllegalArgumentException("Extension type(" + type + 
                ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
    }
    
    // 将接口类型与其对应的ExtensionLoader本地缓存到`ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS`中
    ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
    if (loader == null) {
        EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));
        loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
    }
    return loader;
}

2. 获取Adaptive实例

得到ExtensionLoader后,再得到具体的Adaptive实例;其中cachedAdaptiveInstance定义为:Holder<Object> cachedAdaptiveInstance,如果本地没有缓存实例,那么调用createAdaptiveExtension()获取,源码如下:

public T getAdaptiveExtension() {
    // 先从本地缓存中查询能否得到Adaptive实例
    Object instance = cachedAdaptiveInstance.get();
    if (instance == null) {
        // double-check方式保证线程安全(单实例的一种实现方式)
        if(createAdaptiveInstanceError == null) {
            synchronized (cachedAdaptiveInstance) {
                instance = cachedAdaptiveInstance.get();
                if (instance == null) {
                    try {
                        // 获取实例
                        instance = createAdaptiveExtension();
                        // 将实例本地缓存
                        cachedAdaptiveInstance.set(instance);
                    } catch (Throwable t) {
                        createAdaptiveInstanceError = t;
                        throw new IllegalStateException("fail to create adaptive instance: " + t.toString(), t);
                    }
                }
            }
        }
        else {
            throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
        }
    }

    return (T) instance;
}

Hold申明如下,用volatile来保证数据的一致性:

/**
 * Helper Class for hold a value.
 *
 * @author william.liangf
 */
public class Holder<T> {
    
    private volatile T value;
    
    public void set(T value) {
        this.value = value;
    }
    
    public T get() {
        return value;
    }

}

如果本地还没有缓存实例,那么调用createAdaptiveExtension()获取Adaptive实例,源码如下:

private T createAdaptiveExtension() {
    try {
        return injectExtension((T) getAdaptiveExtensionClass().newInstance());
    } catch (Exception e) {
        throw new IllegalStateException("Can not create adaptive extenstion " + type + ", cause: " + e.getMessage(), e);
    }
}

getAdaptiveExtensionClass()源码如下,先调用getExtensionClasses();,如果不能得到Adaptive类,那么调用createAdaptiveExtensionClass()获取Adaptive类;

private Class<?> getAdaptiveExtensionClass() {
    getExtensionClasses();
    if (cachedAdaptiveClass != null) {
        return cachedAdaptiveClass;
    }
    return cachedAdaptiveClass = createAdaptiveExtensionClass();
}

接下来我们看getExtensionClasses()方法中如何得到Adaptive扩展类,其主要分为如下几个步骤:

  1. 得到ThreadPool接口上申明的SPI注解的值("fixed"),就是默认名称cachedDefaultName ;
  2. 遍历类路径下三个目录("META-INF/dubbo/internal","META-INF/dubbo/","META-INF/services/"),查找所有命名为type.getName()即com.alibaba.dubbo.common.threadpool.ThreadPool的文件,得到文件中所有申明的实现集合;
  3. 查找有Adaptive注解的类,就是要找的Adaptive类;
  4. 如果没有Adaptive类,那么尝试能否通过Wrapper方式获取类,如果可以获取,那么缓存到Set<Class<?>> cachedWrapperClasses;中;
  5. com.alibaba.dubbo.common.threadpool.ThreadPool文件中得到的所有类缓存到Holder<Map<String, Class<?>>> cachedClasses中;
private Map<String, Class<?>> loadExtensionClasses() {
    // 如果入参类型申明了SPI注解,那么SPI注解中的value就是默认实现名称,即cachedDefaultName (SPI注解中value的值只能有1个,超过1个会抛出异常);
    final SPI defaultAnnotation = type.getAnnotation(SPI.class);
    if(defaultAnnotation != null) {
        String value = defaultAnnotation.value();
        if(value != null && (value = value.trim()).length() > 0) {
            String[] names = NAME_SEPARATOR.split(value);
            if(names.length > 1) {
                throw new IllegalStateException("more than 1 default extension name on extension " + type.getName()
                        + ": " + Arrays.toString(names));
            }
            if(names.length == 1) cachedDefaultName = names[0];
        }
    }
    
    // 遍历类路径下的(DUBBO_INTERNAL_DIRECTORY,DUBBO_DIRECTORY,SERVICES_DIRECTORY)三个目录,将符合type类型的所有实现存到Map中;
    Map<String, Class<?>> extensionClasses = new HashMap<String, Class<?>>();
    loadFile(extensionClasses, DUBBO_INTERNAL_DIRECTORY);
    loadFile(extensionClasses, DUBBO_DIRECTORY);
    loadFile(extensionClasses, SERVICES_DIRECTORY);
    return extensionClasses;
}

2.2 创建Adaptive扩展类

由于getExtensionClasses()中不能得到ThreadPool类型的Adaptive类(带有Adaptive注解),所以需要通过createAdaptiveExtensionClass()创建Adaptive扩展类,由源码可知,其创建Adaptive扩展类分为如下几个步骤:

  1. 通过createAdaptiveExtensionClassCode()得到用于生成Adaptive扩展类的源代码code
  2. 得到类加载器ClassLoader
  3. 通过扩展机制得到编译器(默认用JavassistCompiler)将扩展类源代码code编译成字节码从而得到Adaptive类
private Class<?> createAdaptiveExtensionClass() {
    String code = createAdaptiveExtensionClassCode();
    ClassLoader classLoader = findClassLoader();
    com.alibaba.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.compiler.Compiler.class).getAdaptiveExtension();
    return compiler.compile(code, classLoader);
}

通过DEBUG方式得到ThreadPool的Adaptive扩展类的源代码如下,由下面的源码可知获取ThreadPool类型的Adaptive类的步骤:

  1. 判断请求URL中是否申明threadpool,如果没有申明threadpool,那么默认为fixed,即扩展名称;
  2. 根据扩展名称调用ExtensionLoader.getExtension(extName)得到具体的实现类
    从用于生成Adaptive扩展类的源码可以得知,URL中对threadpool申明的优先级要高于SPI注解申明;所以如果我们想修改默认的ThreadPool实现方式,可以通过URL中指定threadpool;
package com.alibaba.dubbo.common.threadpool;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class ThreadPool$Adpative 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);
    }
}

创建Adaptive扩展类后,看.getExtension(String)方法如何得到具体的实现类,实现源码如下,即首先尝试从本地缓存cachedInstances中获取,如果获取不到,那么调用createExtension(name)创建扩展类实例并本地缓存;这里dubbo的作者又用到了double-check方式保证线程安全实现单实例;

public T getExtension(String name) {
    if (name == null || name.length() == 0)
        throw new IllegalArgumentException("Extension name == null");
    if ("true".equals(name)) {
        return getDefaultExtension();
    }
    Holder<Object> holder = cachedInstances.get(name);
    if (holder == null) {
        cachedInstances.putIfAbsent(name, new Holder<Object>());
        holder = cachedInstances.get(name);
    }
    Object instance = holder.get();
    if (instance == null) {
        synchronized (holder) {
            instance = holder.get();
            if (instance == null) {
                instance = createExtension(name);
                holder.set(instance);
            }
        }
    }
    return (T) instance;
}

createExtension(String name)部分实现源码如下,由于前面已经调用了getExtensionClasses()得到所有ThreadPool类型并本地缓存在Holder<Map<String, Class<?>>> cachedClasses中,cachedClasses.get()得到保存的数据如下:

fixed=com.alibaba.dubbo.common.threadpool.support.fixed.FixedThreadPool
cached=com.alibaba.dubbo.common.threadpool.support.cached.CachedThreadPool
limited=com.alibaba.dubbo.common.threadpool.support.limited.LimitedThreadPool

根据name的值就能够取得ThreadPool的具体实现类,例如默认fixed对应的具体实现类就是FixedThreadPool;如果设置threadpool="limited",那么对应的具体实现类就是LimitedThreadPool;如果扩展了自定义AfeiThreadPool并在META-INF/dubbo/com.alibaba.dubbo.common.threadpool.ThreadPool文件中申明afei=com.alibaba.dubbo.common.threadpool.support.limited.LimitedThreadPool,那么设置threadpool="afei",就能得到自定义的AfeiThreadPool:

private T createExtension(String name) {
    Class<?> clazz = getExtensionClasses().get(name);
    if (clazz == null) {
        throw findException(name);
    }
    ... ...
}

配置ThreadPool

得到具体的ThreadPool实现后,dubbo根据url调整ThreadPoolExecutor,以FixedThreadPool为例(dubbo provider端默认采用的连接池处理方式),源码如下:

public class FixedThreadPool implements ThreadPool {

    public Executor getExecutor(URL url) {
        String name = url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME);
        int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS);
        int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
        return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, 
                queues == 0 ? new SynchronousQueue<Runnable>() : 
                    (queues < 0 ? new LinkedBlockingQueue<Runnable>() 
                            : new LinkedBlockingQueue<Runnable>(queues)),
                new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url));
    }

}
  1. 如果url中配置了线程名称,那么进行设置,否则基于Constants.DEFAULT_THREAD_NAME得到默认线程名称;
  2. 如果url中配置了线程数量,那么进行设置,否则用默认线程数量:Constants.DEFAULT_THREADS,即默认200个线程;
  3. 如果url中配置了线程队列,那么进行设置,否则用默认线程队列:Constants.DEFAULT_QUEUES,即默认为0;
    例如我们可以在provider.xml中对线程池做如下自定义:
    <dubbo:protocol threadpool="fixed" threads="288" queues="10"/>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容