Dubbo-SPI源码分析

Dubbo SPI源码分析

总览

从官方文档http://dubbo.apache.org/zh-cn/docs/dev/SPI.html可以看出

Dubbo的SPI是加强了JDK的SPI实现:

1.根据注解来加载扩展点实现类,不会加载所有不需要的扩展点实现类

2.定义了扩展点的名称,即xxx=com.alibaba.xxx.XxxProtocol,xx就是它的名称

SPI的形式

Dubbo的SPI约定了,在Classpath扫描扩展点配置文件(下文叫SPI配置文件)META-INF/dubbo/接口全限定名,内容为 配置名=扩展实现类全限定名,如:

registry=org.apache.dubbo.registry.integration.RegistryProtocol

多个实现类用换行符分隔。

实现类的内容为

public class RegistryProtocol implements Protocol {
}

模块配置

在模块配置种,如applicationContext-dubbo.xml中,扩展点都有对应的属性配置和标签,配置指定使用哪个扩展实现如

指定的协议为dubbo协议,在META-INF/dubbo/dubbo=org.apache.dubbo.rpc.protocol配置文件中对应的实现为:

dubbo=org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol

使用的实现类为org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol

@SPI注解

我们先来看下SPI注解:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface SPI {
    /**
     * default extension name
     */
    String value() default "";

}

里面只有一个String类型,使用@SPI("dubbo")表示接口类的实现标识,对应了SPI配置文件中的前缀名称。

dubbo是什么时候去加载SPI呢?

首先,通过生成Spring管理的Bean,关于Dubbo是如何和Spring融合的,这里先不做讨论,我们先关注到ServiceConfig中获取protocol这行代码:

private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class)
        .getAdaptiveExtension();

可以在这里打上断点,启动dubbo的provider,即可进入此处。

查看getExtensionLoader方法:

/**
 * 根据扩展点的接口,获得扩展加载器
 */
public static  ExtensionLoader getExtensionLoader(Class type) {
    if (type == null)
        throw new IllegalArgumentException("Extension type == null");
    // 判断类型是否是接口,不是接口抛异常
    if (!type.isInterface()) {
        throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
    }
    // 是否有Annotation注解,没有注解抛异常
    if (!withExtensionAnnotation(type)) {
        throw new IllegalArgumentException("Extension type(" + type +
                ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
    }

    // 获得接口对应的扩展点加载器
    ExtensionLoader loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);
    if (loader == null) {
        EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));
        loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);
    }
    return loader;
}

如果参数类型是interface org.apache.dubbo.rpc.Protocol,即加载Protocol这个扩展点,则会先做一系列校验,接着从EXTENSION_LOADERS缓存中获取扩展点加载器它的定义如下:

private static final ConcurrentMap, ExtensionLoader> EXTENSION_LOADERS = new ConcurrentHashMap, ExtensionLoader>();

如果EXTENSION_LOADERS中为空,则会创建一个 ExtensionLoader ,也就是扩展类加载器。

进入它的构造方法

/**
 * 对象工厂
 * 用于调用 {@link #injectExtension(Object)} 方法,向拓展对象注入依赖属性。
 * 例如,StubProxyFactoryWrapper 中有 `Protocol protocol` 属性。
 * @param type
 */
private ExtensionLoader(Class type) {
    this.type = type;
    objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
}

做了2件事

1.保存传进来的type,interface org.apache.dubbo.rpc.Protocol

2.判断类型是否是ExtensionFactory.class ,也就是是否是扩展类工厂,如果是,则objectFactory = null,不是,则递归调用了 ExtensionLoader.getExtensionLoader(ExtensionFactory.class) 。

(1)首次进来,type == ExtensionFactory.class为false,调用ExtensionLoader.getExtensionLoader(ExtensionFactory.class),进入getExtensionLoader,在EXTENSION_LOADERS.putIfAbsent的时候会新建一个ExtensionLoader,再次进入ExtensionLoader构造器,这次的type为ExtensionFactory类型。此时会将objectFactory赋值为null

(2)经过步骤(1),返回新建的 ExtensionLoader (type = ExtensionFactory.class, objectFactory = null )并且存放在 EXTENSION_LOADERS 的ConcurrentHashMap中 ,key为type。此时返回这个 ExtensionLoader

(3)回到ExtensionLoader.getExtensionLoader(ExtensionFactory.class) ,调用生成的ExtensionLoader 的getAdaptiveExtension()方法

// 获取自适应扩展类
public T getAdaptiveExtension() {
    // 缓存的适配扩展类
    Object instance = cachedAdaptiveInstance.get();
    if (instance == null) {
        if (createAdaptiveInstanceError == null) {
            synchronized (cachedAdaptiveInstance) {
                instance = cachedAdaptiveInstance.get();
                if (instance == null) {
                    try {
                        // 首次进来创建一个自适应扩展类
                        instance = createAdaptiveExtension();
                        // 创建完Adaptive适配对象后保存到缓存中
                        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;
}



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

首先进入getAdaptiveExtensionClass()

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

再进入getExtensionClasses()

/**
 * 获得扩展点实现类数组
 *
 * @return 扩展点实现类数组
 */
private Map> getExtensionClasses() {
    Map> classes = cachedClasses.get();
    if (classes == null) {
        synchronized (cachedClasses) {
            classes = cachedClasses.get();
            if (classes == null) {
                classes = loadExtensionClasses();
                cachedClasses.set(classes);
            }
        }
    }
    return classes;
}

其中有一个double check的过程,防止并发,从缓存中获取,如果没有的话进入loadExtensionClasses(),方法如下。它会首先验证SPI注解的合法性,接下来进入方法中的重点loadDirectory();

/**
 * 加载扩展实现类数组
 * 不用加synchronized,因为唯一调用本方法的{@link #getExtensionClasses()} 已经声明
 * @return 扩展实现类数组
 */
private Map> loadExtensionClasses() {
    // 获得默认扩展类实现类名
    final SPI defaultAnnotation = type.getAnnotation(SPI.class);
    if (defaultAnnotation != null) {
        String value = defaultAnnotation.value();
        if ((value = value.trim()).length() > 0) {
            String[] names = NAME_SEPARATOR.split(value);
            // 验证注解value属性,多个则抛出异常
            if (names.length > 1) {
                throw new IllegalStateException("more than 1 default extension name on extension " + type.getName()
                        + ": " + Arrays.toString(names));
            }
            // 缓存默认的SPI属性名
            if (names.length == 1) cachedDefaultName = names[0];
        }
    }

    Map> extensionClasses = new HashMap>();
    // 重点方法
    loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName());
    loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName().replace("org.apache", "com.alibaba"));
    loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName());
    loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName().replace("org.apache", "com.alibaba"));
    loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName());
    loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName().replace("org.apache", "com.alibaba"));
    return extensionClasses;
}

其中的 DUBBO_INTERNAL_DIRECTORY 常量为 META-INF/dubbo/internal/也就是默认存放SPI文件的位置。

// 加载路径中的扩展类
private void loadDirectory(Map> extensionClasses, String dir, String type) {
    // fileName为具体SPI配置文件
    String fileName = dir + type;
    try {
        Enumeration urls;
        // 获取classLoader
        ClassLoader classLoader = findClassLoader();
        // 通过classLoader加载ClassPath中的url
        if (classLoader != null) {
            urls = classLoader.getResources(fileName);
        } else {
            urls = ClassLoader.getSystemResources(fileName);
        }
        if (urls != null) {
            // 遍历可用的SPI配置,进行资源加载
            while (urls.hasMoreElements()) {
                java.net.URL resourceURL = urls.nextElement();
                loadResource(extensionClasses, classLoader, resourceURL);
            }
        }
    } catch (Throwable t) {
        logger.error("Exception when load extension class(interface: " +
                type + ", description file: " + fileName + ").", t);
    }
}

接下来进入loadResource()方法中:

private void loadResource(Map> extensionClasses, ClassLoader classLoader, java.net.URL resourceURL) {
    try {
        // 通过流来读取文件
        BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "utf-8"));
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                final int ci = line.indexOf('#');
                if (ci >= 0) line = line.substring(0, ci);
                line = line.trim();
                if (line.length() > 0) {
                    try {
                        String name = null;
                        // 判断等号位置
                        int i = line.indexOf('=');
                        if (i > 0) {
                            // 截取名称
                            name = line.substring(0, i).trim();
                            // 截取具体实现类的全路径
                            line = line.substring(i + 1).trim();
                        }
                        if (line.length() > 0) {
                            loadClass(extensionClasses, resourceURL, Class.forName(line, true, classLoader), name);
                        }
                    } catch (Throwable t) {
                        IllegalStateException e = new IllegalStateException("Failed to load extension class(interface: " + type + ", class line: " + line + ") in " + resourceURL + ", cause: " + t.getMessage(), t);
                        exceptions.put(line, e);
                    }
                }
            }
        } finally {
            reader.close();
        }
    } catch (Throwable t) {
        logger.error("Exception when load extension class(interface: " +
                type + ", class file: " + resourceURL + ") in " + resourceURL, t);
    }
}

接下来进入loadClass()方法:

private void loadClass(Map> extensionClasses, java.net.URL resourceURL, Class clazz, String name) throws NoSuchMethodException {
    if (!type.isAssignableFrom(clazz)) {
        throw new IllegalStateException("Error when load extension class(interface: " +
                type + ", class line: " + clazz.getName() + "), class "
                + clazz.getName() + "is not subtype of interface.");
    }
    // <1> 如果有Adaptive注解
    if (clazz.isAnnotationPresent(Adaptive.class)) {
        if (cachedAdaptiveClass == null) {
            // 保存到缓存中
            cachedAdaptiveClass = clazz;
            // 如果有多个Adaptive注解,抛出异常
        } else if (!cachedAdaptiveClass.equals(clazz)) {
            throw new IllegalStateException("More than 1 adaptive class found: "
                    + cachedAdaptiveClass.getClass().getName()
                    + ", " + clazz.getClass().getName());
        }
        // 是否包装类,包装类有构造方法
    } else if (isWrapperClass(clazz)) {
        Set> wrappers = cachedWrapperClasses;
        // 缓存不存在,则创建
        if (wrappers == null) {
            cachedWrapperClasses = new ConcurrentHashSet>();
            wrappers = cachedWrapperClasses;
        }
        // 往包装类缓存中添加对象
        wrappers.add(clazz);
    } else {
        clazz.getConstructor();
        if (name == null || name.length() == 0) {
            name = findAnnotationName(clazz);
            if (name.length() == 0) {
                throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + resourceURL);
            }
        }
        String[] names = NAME_SEPARATOR.split(name);
        if (names != null && names.length > 0) {
            Activate activate = clazz.getAnnotation(Activate.class);
            if (activate != null) {
                cachedActivates.put(names[0], activate);
            } else {
                // support com.alibaba.dubbo.common.extension.Activate
                com.alibaba.dubbo.common.extension.Activate oldActivate = clazz.getAnnotation(com.alibaba.dubbo.common.extension.Activate.class);
                if (oldActivate != null) {
                    cachedActivates.put(names[0], oldActivate);
                }
            }
            for (String n : names) {
                // 名称缓存中添加扩展类
                if (!cachedNames.containsKey(clazz)) {
                    cachedNames.put(clazz, n);
                }
                Class c = extensionClasses.get(n);
                if (c == null) {
                    // 扩展类缓存中添加
                    extensionClasses.put(n, clazz);
                } else if (c != clazz) {
                    throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + n + " on " + c.getName() + " and " + clazz.getName());
                }
            }
        }
    }
}

方法中,cachedNames定义如下:

private final ConcurrentMap, String> cachedNames = new ConcurrentHashMap, String>();

key为扩展类,name为扩展名,如加载SpringExtensionFactory,则保存的key为spring。

而extensionClasses为 Map> extensionClasses 的类型,key和value 与 cachedNames 相反。

加载ExtensionFactory的扩展类完成后extensionClasses的缓存中会有两种实现类:

SpringExtensionFactory和SpiExtensionFactory,另一种AdaptiveExtensionFactory会再loadClass方法的<1>处保存到cachedAdaptiveClass中。

当回到getAdaptiveExtensionClass()中时,在该方法的<1> 处,如果cachedAdaptiveClass不为null,也就是加了@Adaptive的可扩展类被加载完成。

再回到createAdaptiveExtension()的newInstance(),通过反射创建对象AdaptiveExtensionFactory()

public AdaptiveExtensionFactory() {
    ExtensionLoader loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
    List list = new ArrayList();
    for (String name : loader.getSupportedExtensions()) {
        list.add(loader.getExtension(name));
    }
    factories = Collections.unmodifiableList(list);
}

构造方法中将两种ExtensionFactory放入list中。之后回到getAdaptiveExtension()做对象的缓存,缓存了AdaptiveExtensionFactory。这一步之后,回到了创建Protocol的ExtensionLoader处也就是ExtensionLoader构造方法。

往缓存EXTENSION_LOADERS中添加Protocol类的ExtensionLoader ,也即上面的AdaptiveExtensionFactory,这里有点啰嗦,大家可以自己去跟源码。

总结如下:

->this.type = type;
->objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
->this.type = type;
->objectFactory = null;

1.每个ExtensionLoader有两个属性type,objectFactory
Class type; --初始化时需要得到的接口名称
ExtensionFactory objectFactory; -- AdaptiveExtensionFactory[SpiExtensionFactory, SpringExtensionFactory]

2.new 一个ExtensionLoader存储在ConcurrentMap, ExtensionLoader> EXTENSION_LOADERS

objectFactory就是ExtensionFactory,它是通过ExtensionLoader.getExtensionLoader(ExtensionFactory.class)实现的,它的objectFactory = null
objectFactory为dubbo的IOC提供了所有的对象

Protocol的扩展实现有 RegistryProtocol,InjvmProtocol,DubboProtocol,MockProtocol,这几个类都没有加Adaptive注解,再getAdaptiveExtensionClass()中会进入createAdaptiveExtensionClass();

private Class createAdaptiveExtensionClass() {
    String code = createAdaptiveExtensionClassCode();
    ClassLoader classLoader = findClassLoader();
    org.apache.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(org.apache.dubbo.common.compiler.Compiler.class).getAdaptiveExtension();
    return compiler.compile(code, classLoader);
}

createAdaptiveExtensionClassCode()代码较长,就不列出来了

逻辑如下:

1.获取接口Protocol中的所有方法

2.遍历每个方法,查看是否有@Adaptive注解,有则构建一个类,拼接代码

3.根据每个方法的入参出参,构建方法的代码

构造出来的Protocol代理对象为:

package org.apache.dubbo.rpc;

import org.apache.dubbo.common.extension.ExtensionLoader;

public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol {
    private static final org.apache.dubbo.common.logger.Logger logger = org.apache.dubbo.common.logger.LoggerFactory.getLogger(ExtensionLoader.class);
    private java.util.concurrent.atomic.AtomicInteger count = new java.util.concurrent.atomic.AtomicInteger(0);

    public void destroy() {
        throw new UnsupportedOperationException("method public abstract void org.apache.dubbo.rpc.Protocol.destroy() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
    }

    public int getDefaultPort() {
        throw new UnsupportedOperationException("method public abstract int org.apache.dubbo.rpc.Protocol.getDefaultPort() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
    }

    public org.apache.dubbo.rpc.Invoker refer(java.lang.Class arg0, org.apache.dubbo.common.URL arg1) throws org.apache.dubbo.rpc.RpcException {
        if (arg1 == null) throw new IllegalArgumentException("url == null");
        org.apache.dubbo.common.URL url = arg1;
        String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
        if (extName == null)
            throw new IllegalStateException("Fail to get extension(org.apache.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");
        org.apache.dubbo.rpc.Protocol extension = null;
        try {
            extension = (org.apache.dubbo.rpc.Protocol) ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
        } catch (Exception e) {
            if (count.incrementAndGet() == 1) {
                logger.warn("Failed to find extension named " + extName + " for type org.apache.dubbo.rpc.Protocol, will use default extension dubbo instead.", e);
            }
            extension = (org.apache.dubbo.rpc.Protocol) ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension("dubbo");
        }
        return extension.refer(arg0, arg1);
    }

    public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
        if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
        if (arg0.getUrl() == null)
            throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
        org.apache.dubbo.common.URL url = arg0.getUrl();
        String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
        if (extName == null)
            throw new IllegalStateException("Fail to get extension(org.apache.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");
        org.apache.dubbo.rpc.Protocol extension = null;
        try {
            extension = (org.apache.dubbo.rpc.Protocol) ExtensionLoader
                    .getExtensionLoader(org.apache.dubbo.rpc.Protocol.class)
                    .getExtension(extName);
        } catch (Exception e) {
            if (count.incrementAndGet() == 1) {
                logger.warn("Failed to find extension named " + extName + " for type org.apache.dubbo.rpc.Protocol, will use default extension dubbo instead.", e);
            }
            extension = (org.apache.dubbo.rpc.Protocol) ExtensionLoader
                    .getExtensionLoader(org.apache.dubbo.rpc.Protocol.class)
                    .getExtension("dubbo");
        }
        return extension.export(arg0);
    }
}

其中关键方法的refer的关键步骤为extension.refer(arg0, arg1)

关键方法的export的关键步骤为extension.export(arg0)

回到createAdaptiveExtensionClass()

private Class createAdaptiveExtensionClass() {
    String code = createAdaptiveExtensionClassCode();
    ClassLoader classLoader = findClassLoader();
    org.apache.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(org.apache.dubbo.common.compiler.Compiler.class).getAdaptiveExtension();
    return compiler.compile(code, classLoader);
}

再通过SPI机制去加载Compiler扩展类,然后动态编译。这里加载出来的Compiler也是AdaptiveCompiler,AdaptiveCompiler中getDefaultExtension()获取到的扩展类是JavassistCompiler。

@Override
public Class compile(String code, ClassLoader classLoader) {
    Compiler compiler;
    ExtensionLoader loader = ExtensionLoader.getExtensionLoader(Compiler.class);
    String name = DEFAULT_COMPILER; // copy reference
    if (name != null && name.length() > 0) {
        compiler = loader.getExtension(name);
    } else {
        compiler = loader.getDefaultExtension();
    }
    return compiler.compile(code, classLoader);
}

使用该JavassistCompiler来编译Protocol$Adaptive动态代理类。生成该类的Class文件,随后设置到缓存中,代码继续调用回到:

private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class)
        .getAdaptiveExtension();

初始调用处,完成Protocol的扩展类加载,之后的扩展类如ProxyFactory,都是通过Dubbo的SPI机制来加载的,保证了框架的可扩展。

总结一下整个流程

1.ExtensionLoader.getExtensionLoader(Protocol.class) ,参数传入需要加载扩展类的接口,缓存中有,则直接返回

2.缓存中没有,则从配置文件加载,一个配置文件可能有多个扩展类实现,如果这些类上有@Adaptive注解,则直接作为AdaptiveExtension保存到变量中

3.没有@Adaptive注解,则根据接口的方法是否有@Adaptive注解,去判断是否创建Adaptive类的代码,创建类的代码完成后,加载Compiler(加载流程也是SPI方式,默认使用Javaassist的Compiler),编译生成Adaptive类,返回,生成Adaptive扩展类完成。

其中细节流程较复杂,但是只要读者跟着源码耐心debug,加上阅读官方文档,相信对于Dubbo的SPI实现还是能理解的。(^-^)

你可能感兴趣的:(Dubbo-SPI源码分析)