3、Dubbo的SPI机制分析2-Adaptive详解

一念花开

1、Dubbo的@Adaptive例子

@SPI("dubbo")
public interface AdaptiveExt {
    @Adaptive
    // 单元测试方法4的注解为@Adaptive({"t"})
    String echo(String msg, URL url);
}
public class DubboAdaptiveExt implements AdaptiveExt {
    @Override
    public String echo(String msg, URL url) {
        return "dubbo";
    }
}

public class SpringCloudAdaptiveExt implements AdaptiveExt {
    @Override
    public String echo(String msg, URL url) {
        return "spring cloud";
    }
}

// 单元测试3中加上@Adaptive注解,其余不加
@Adaptive
public class ThriftAdaptiveExt implements AdaptiveExt {
    @Override
    public String echo(String msg, URL url) {
        return "thrift";
    }
}

同时应当在resources目录下新建META-INF/dubbo文件夹,新建com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt,即接口的全限定名文件,文件内容为:

dubbo=com.alibaba.dubbo.demo.provider.adaptive.impl.DubboAdaptiveExt
cloud=com.alibaba.dubbo.demo.provider.adaptive.impl.SpringCloudAdaptiveExt
thrift=com.alibaba.dubbo.demo.provider.adaptive.impl.ThriftAdaptiveExt

下面是4个单元测试用例。观察4个测试用例的输出结果,我们可以得出以下结论:
1) 从测试3可以看出,若实现类加了@Adaptive注解,则它优先级最高,getAdaptiveExtension()创建的就是该类的实例
2) 从测试1看出,若SP注解上有值,且url参数中无值,并且没有类标注@Adaptive注解,则创建dubbo的key对应的类的实例
3) 从测试4看出,若方法上有注解@Adpative({"t"}),则URL中应当配上该参数t=cloud,创建cloud对应的实例
4) 从测试2看出,方法有注解@Adaptive,同时URL配置的是默认参数,该参数时通过AdaptiveExt通过转小写生成(adaptive.ext=cloud),则创建的就是cloud对应类的实例,可以看出,其实测试2和4类似,只要URL中有参数并且配置正确,则忽略@SPI注解上的值

所以可以得出优先级: @Adaptive标注的类 > URL参数 > @SPI注解中的值

/**
 * SPI上有注解,@SPI("dubbo"),url无参数,没有类上添加@Adaptive注解,方法@Adaptive注解上无参数,输出dubbo
 */
@Test
public void test1(){
    ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class);
    AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension();
    URL url = URL.valueOf("test://localhost/test");
    System.out.println(adaptiveExtension.echo("d", url));
}
/**
 * SPI上有注解,@SPI("dubbo"),URL中也有具体的值,输出spring cloud,注意这里对方法标注有@Adaptive注解,
 * 但是该注解没有值
 */
@Test
public void test2(){
    ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class);
    AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension();
    URL url = URL.valueOf("test://localhost/test?adaptive.ext=cloud");
    System.out.println(adaptiveExtension.echo("d", url));
}
/**
 * SPI上有注解,@SPI("dubbo"),URL中也有具体的值,ThriftAdaptiveExt实现类上面有@Adaptive注解,输出thrift
 */
@Test
public void test3(){
    ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class);
    AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension();
    URL url = URL.valueOf("test://localhost/test?adaptive.ext=cloud");
    System.out.println(adaptiveExtension.echo("d", url));
}
/**
 * SPI上有注解,@SPI("dubbo"),URL中也有具体的值,接口方法中加上注解@Adaptive({"t"}),各个实现类上面没有
 * @Adaptive注解,输出spring cloud
 */
@Test
public void test4(){
    ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class);
    AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension();
    URL url = URL.valueOf("test://localhost/test?t=cloud");
    System.out.println(adaptiveExtension.echo("d", url));
}

2、Dubbo的@Adaptive自适应拓展机制源码分析

2.1、测试用例1

首先先分析测试用例对应的源码,其余几种情况都差不多,1种情况分析透彻了,其余几种自然就清楚了.

// SPI上有注解,@SPI("dubbo"),url无参数,没有类上添加@Adaptive注解,方法@Adaptive注解上无参数,输出dubbo
@Test
public void test1(){
    ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class);
    AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension();
    URL url = URL.valueOf("test://localhost/test");
    System.out.println(adaptiveExtension.echo("d", url));
}
public T getAdaptiveExtension() {
    Object instance = cachedAdaptiveInstance.get();
    if (instance == null) {
        if (createAdaptiveInstanceError == null) {
            synchronized (cachedAdaptiveInstance) {
                instance = cachedAdaptiveInstance.get();
                if (instance == null) {
                    try {
                        // 创建自适应拓展代理类对象并放入缓存
                        instance = createAdaptiveExtension();
                        cachedAdaptiveInstance.set(instance);
                    } catch (Throwable t) {
                        // 抛异常
                    }
                }
            }
        } else {
            // 抛异常
        }
    }
    return (T) instance;
}
private T createAdaptiveExtension() {
    try {
        // 分为3步:1是创建自适应拓展代理类Class对象,2是通过反射创建对象,3是给创建的对象按需依赖注入
        return injectExtension((T) getAdaptiveExtensionClass().newInstance());
    } catch (Exception e) {
        // 抛异常
    }
}

private Class<?> getAdaptiveExtensionClass() {
    // 从默认目录中加载标注了@SPI注解的实现类
    getExtensionClasses();
    // 如果有标注了@Adaptive注解实现类,那么cachedAdaptiveClass不为空,直接返回
    if (cachedAdaptiveClass != null) {
        return cachedAdaptiveClass;
    }
    // 创建自适应拓展代理类class文件
    return cachedAdaptiveClass = createAdaptiveExtensionClass();
}
private Class<?> createAdaptiveExtensionClass() {
    // code就是保存了创建的class字符串数据
    String code = createAdaptiveExtensionClassCode();
    ClassLoader classLoader = findClassLoader();
    Compiler compiler = ExtensionLoader.getExtensionLoader(Compiler.class).getAdaptiveExtension();
    return compiler.compile(code, classLoader);
}
private String createAdaptiveExtensionClassCode() {
    // 用来存放生成的代理类class文件
    StringBuilder codeBuilder = new StringBuilder();
    // 遍历标注有@SPI注解的接口的所有方法,这里分析的是com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt
    Method[] methods = type.getMethods();

    // 这些方法中应当致至少有一个方法被@Adaptive注解标注,否则不需要生成自适应代理类,直接抛出异常
    boolean hasAdaptiveAnnotation = false;
    for (Method m : methods) {
        if (m.isAnnotationPresent(Adaptive.class)) {
            hasAdaptiveAnnotation = true;
            break;
        }
    }
    // no need to generate adaptive class since there's no adaptive method found.
    if (!hasAdaptiveAnnotation)
        // 抛异常
    // 生成包信息,形如package com.alibaba.dubbo.demo.provider.adaptive;
    codeBuilder.append("package ").append(type.getPackage().getName()).append(";");
    // 生成导包信息,形如import com.alibaba.dubbo.common.extension.ExtensionLoader;
    codeBuilder.append("\nimport ").append(ExtensionLoader.class.getName()).append(";");
    // 生成类名,形如public class AdaptiveExt$Adaptive 
    //                             implements com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt {
    codeBuilder.append("\npublic class ").append(type.getSimpleName()).append("$Adaptive").
                                 append(" implements ").append(type.getCanonicalName()).append(" {");
    // 遍历所有方法,为SPI接口的所有方法生成代理方法
    for (Method method : methods) {
        // 方法返回值、参数、抛出异常
        Class<?> rt = method.getReturnType();
        Class<?>[] pts = method.getParameterTypes();
        Class<?>[] ets = method.getExceptionTypes();
        // 获取方法上的Adaptive注解,如果方法上没有该注解,直接为该方法抛出异常
        Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class);
        StringBuilder code = new StringBuilder(512);
        if (adaptiveAnnotation == null) {
            code.append("throw new UnsupportedOperationException(\"method ")
                    .append(method.toString()).append(" of interface ")
                    .append(type.getName()).append(" is not adaptive method!\");");
        } else {
            // urlTypeIndex用来记录URL这个参数在第几个参数位置上,这里String echo(String msg, URL url);
            // 在位置1上
            int urlTypeIndex = -1;
            for (int i = 0; i < pts.length; ++i) {
                if (pts[i].equals(URL.class)) {
                    urlTypeIndex = i;
                    break;
                }
            }
            // 找到了URL参数
            if (urlTypeIndex != -1) {
                // 空指针检查
                // s形如:if (arg1 == null) throw new IllegalArgumentException("url == null");
                String s = String.format("\nif (arg%d == null) 
                              throw new IllegalArgumentException(\"url == null\");",urlTypeIndex);
                code.append(s);

                // s形如:com.alibaba.dubbo.common.URL url = arg1;
                s = String.format("\n%s url = arg%d;", URL.class.getName(), urlTypeIndex);
                code.append(s);
            }
            // 没找到,暂不分析,TODO
            // 获取方法上的Adaptive注解的值,@Adaptive({"t"}),这里是t
            String[] value = adaptiveAnnotation.value();
            // 如果@Adaptive注解没有值,对应第二种测试情况,从接口名生成从url中获取参数的key,
            // key为adaptive.ext,获取参数为url.getParameter("adaptive.ext", "dubbo")
            // 因为第二种情况URL中传递了adaptive.ext这个参数,
            // 所以String extName = url.getParameter("t", "dubbo");中获取的是cloud
            if (value.length == 0) {
                char[] charArray = type.getSimpleName().toCharArray();
                StringBuilder sb = new StringBuilder(128);
                for (int i = 0; i < charArray.length; i++) {
                    if (Character.isUpperCase(charArray[i])) {
                        if (i != 0) {
                            sb.append(".");
                        }
                        sb.append(Character.toLowerCase(charArray[i]));
                    } else {
                        sb.append(charArray[i]);
                    }
                }
                value = new String[]{sb.toString()};
            }
            // hasInvocation 暂不分析,TODO
            // defaultExtName是dubbo,cachedDefaultName = names[0],这个值是@SPI("dubbo")里的
            String defaultExtName = cachedDefaultName;
            String getNameCode = null;
            for (int i = value.length - 1; i >= 0; --i) {
                if (i == value.length - 1) {
                    if (null != defaultExtName) {
                        if (!"protocol".equals(value[i]))
                            if (hasInvocation)
                                getNameCode = 
                                     String.format("url.getMethodParameter(methodName, 
                                                   \"%s\", \"%s\")", value[i], defaultExtName);
                            else
                                // 形如:url.getParameter("t", "dubbo");
                                // 理解就是看url中有没有传t参数,传了就以url中为准,否则就取
                                // @SPI("dubbo")中的为默认值
                                getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", 
                                                                     value[i], defaultExtName);
                        else
                            getNameCode = String.format("( url.getProtocol() == null ? 
                                               \"%s\" : url.getProtocol() )", defaultExtName);
                    } 
                    else {
                        if (!"protocol".equals(value[i]))
                            if (hasInvocation)
                                getNameCode = String.format("url.getMethodParameter(methodName, 
                                                   \"%s\", \"%s\")", value[i], defaultExtName);
                            else
                                getNameCode = String.format("url.getParameter(\"%s\")", value[i]);
                        else
                            getNameCode = "url.getProtocol()";
                    }
                } else {
                    if (!"protocol".equals(value[i]))
                        if (hasInvocation)
                            getNameCode = String.format("url.getMethodParameter(methodName, 
                                                     \"%s\", \"%s\")", value[i], defaultExtName);
                        else
                            getNameCode = String.format("url.getParameter(\"%s\", %s)", 
                                                                          value[i], getNameCode);
                    else
                        getNameCode = String.format("url.getProtocol() == null ? 
                                                         (%s) : url.getProtocol()", getNameCode);
                }
            }
            // 形如:String extName = url.getParameter("t", "dubbo");
            // 这个extName就是要为@SPI标注的接口生成哪个代理类
            code.append("\nString extName = ").append(getNameCode).append(";");
            // check extName == null?
            // 形如:if (extName == null)  throw new IllegalStateException("...");
            String s = String.format("\nif(extName == null) " +
                            "throw new IllegalStateException(\"Fail to get extension(%s)
                                  name from url(\" + url.toString() + \") use keys(%s)\");",
                                                         type.getName(), Arrays.toString(value));
            code.append(s);

            // AdaptiveExt extension = (AdaptiveExt)
            //       ExtensionLoader.getExtensionLoader(AdaptiveExt.class).getExtension(extName);
            s = String.format("\n%s extension = (%<s)%s.getExtensionLoader(%s.class).
                                                   getExtension(extName);",type.getName(), 
                                          ExtensionLoader.class.getSimpleName(), type.getName());
            code.append(s);
            // return statement
            if (!rt.equals(void.class)) {
                code.append("\nreturn ");
            }

            // 形如:return extension.echo(arg0, arg1);
            s = String.format("extension.%s(", method.getName());
            code.append(s);
            for (int i = 0; i < pts.length; i++) {
                if (i != 0)
                    code.append(", ");
                code.append("arg").append(i);
            }
            code.append(");");
        }
        // 加上方法名,形如:public java.lang.String echo(java.lang.String arg0, 
        //                                                     com.alibaba.dubbo.common.URL arg1) {
        codeBuilder.append("\npublic ").append(rt.getCanonicalName()).append(" ").
                                                              append(method.getName()).append("(");
        for (int i = 0; i < pts.length; i++) {
            if (i > 0) {
                codeBuilder.append(", ");
            }
            codeBuilder.append(pts[i].getCanonicalName());
            codeBuilder.append(" ");
            codeBuilder.append("arg").append(i);
        }
        codeBuilder.append(")");
        // 异常
        if (ets.length > 0) {
            codeBuilder.append(" throws ");
            for (int i = 0; i < ets.length; i++) {
                if (i > 0) {
                    codeBuilder.append(", ");
                }
                codeBuilder.append(ets[i].getCanonicalName());
            }
        }
        codeBuilder.append(" {");
        codeBuilder.append(code.toString());
        codeBuilder.append("\n}");
    }
    codeBuilder.append("\n}");
    if (logger.isDebugEnabled()) {
        logger.debug(codeBuilder.toString());
    }
    return codeBuilder.toString();
}

通过这一系列代码,Dubbo就为AdaptiveExt根据@SPI的注解值dubbo生成了一个自适应拓展代理类,类代码如下:

package com.alibaba.dubbo.demo.provider.adaptive;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class AdaptiveExt$Adaptive implements com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt {
    public java.lang.String echo(java.lang.String arg0, com.alibaba.dubbo.common.URL arg1) {
        if (arg1 == null) 
            throw new IllegalArgumentException("url == null");
        com.alibaba.dubbo.common.URL url = arg1;
        // 核心,通过上面的分析我们知道,并没有配置t参数,所以URL取不到t参数,则以默认值dubbo代替,而dubbo就是
        // @SPI注解的值,adaptiveExtension.echo("d", url),执行这句代码时,adaptiveExtension实际上是
        // AdaptiveExt$Adaptive的实例对象,因此会走到它的echo方法中
        String extName = url.getParameter("t", "dubbo");
        if (extName == null)
            throw new IllegalStateException("Fail to get extension(AdaptiveExt) name 
                                                 from url(" + url.toString() + ") use keys([t])");
        // 为了排版布局,使用了简写AdaptiveExt.class,但是应当知道这里应当是全限定名
        // 这里根据extName去获取Adaptive实例对象,获取的是dubbo的key对应的DubboAdaptiveExt实例对象
        com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt extension = 
                          (com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt) 
                                    ExtensionLoader.getExtensionLoader(AdaptiveExt.class).
                                                                           getExtension(extName);
        // 所以会走DubboAdaptiveExt的echo方法,输出dubbo
        return extension.echo(arg0, arg1);
    }
}

2.2、测试用例2和4

分析完了测试用例1,再来分析2和4就简单多了,看代码.归纳起来就是,如果方法上配置了@Adaptive,就将接口名转小写(adaptive.ext),去URL中取这个参数对应的值,即url.getParameter("adaptive.ext", "dubbo")的值作为extName,生成的也是extName对应的类.如果方法上配置了@Adaptive({"t"}),则以url.getParameter("t", "dubbo")这种方式去取值作为extName.

// 获取方法上的Adaptive注解的值,@Adaptive({"t"}),这里是t
String[] value = adaptiveAnnotation.value();
// 如果@Adaptive注解没有值,对应第二种测试情况,从接口名生成从url中获取参数的key,key为adaptive.ext,获取参数
// 为url.getParameter("adaptive.ext", "dubbo")
// 因为第二种情况URL中传递了adaptive.ext这个参数,所以String extName = url.getParameter("t", "dubbo");
// 中获取的是cloud
if (value.length == 0) {
    char[] charArray = type.getSimpleName().toCharArray();
    StringBuilder sb = new StringBuilder(128);
    for (int i = 0; i < charArray.length; i++) {
        if (Character.isUpperCase(charArray[i])) {
            if (i != 0) {
                sb.append(".");
            }
            sb.append(Character.toLowerCase(charArray[i]));
        } else {
            sb.append(charArray[i]);
        }
    }
    value = new String[]{sb.toString()};
}
// defaultExtName是dubbo,cachedDefaultName = names[0],这个值是@SPI("dubbo")里的
String defaultExtName = cachedDefaultName;
String getNameCode = null;
for (int i = value.length - 1; i >= 0; --i) {
    if (i == value.length - 1) {
        if (null != defaultExtName) {
            if (!"protocol".equals(value[i]))
                if (hasInvocation)
                    // 删除部分代码
                else
                    // 形如:url.getParameter("t", "dubbo");
                    // 理解就是看url中有没有传t参数,传了就以url中为准,否则就取@SPI("dubbo")中的为默认值
                    getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", 
                                                                         value[i], defaultExtName);
            else
                // 删除部分代码
        } else {
            // 删除部分代码
        }
    } 
}
// 形如:String extName = url.getParameter("t", "dubbo");
// 这个extName就是要为@SPI标注的接口生成哪个代理类
code.append("\nString extName = ").append(getNameCode).append(";");

2.3、测试用例3

接下来分析测试用例3,即ThriftAdaptiveExt类上面标注了@Adaptive注解,前面也说过,它的优先级最高,下面看代码.

 private Class<?> getAdaptiveExtensionClass() {
    // 1.从默认目录中加载标注了@SPI注解的实现类
    getExtensionClasses();
    // 2.如果有标注了@Adaptive注解实现类,那么cachedAdaptiveClass不为空,直接返回
    if (cachedAdaptiveClass != null) {
        return cachedAdaptiveClass;
    }
    // 3.创建自适应拓展代理类class文件
    return cachedAdaptiveClass = createAdaptiveExtensionClass();
}

前面我们分析没有类上面标注@Adaptive注解时,dubbo需要根据配置情况为接口生成自适应拓展代理类,也就是上述对应的步骤3代码.但是当有类标注了@Adaptive注解时,情况就不一样了.看上面步骤1getExtensionClasses()会走到下面loadClass方法,当解析到ThriftAdaptiveExt类时,发现它满足clazz.isAnnotationPresent(Adaptive.class)条件,因此cachedAdaptiveClass = clazz被缓存起来,不会再走后面的逻辑.这样当走步骤2时,直接返回cachedAdaptiveClass.那么dubbo为AdaptiveExt接口生成的自适应拓展就是ThriftAdaptiveExt.

private void loadClass(Map<String, Class<?>> extensionClasses, java.net.URL resourceURL,
                                    Class<?> clazz, String name) throws NoSuchMethodException {
    // 判断clazz是否为标注了@Adaptive注解,后面分析
    if (clazz.isAnnotationPresent(Adaptive.class)) {
        if (cachedAdaptiveClass == null) {
            cachedAdaptiveClass = clazz;
        } else if (!cachedAdaptiveClass.equals(clazz)) {
            throw new IllegalStateException("More than 1 adaptive class found: "
                    + cachedAdaptiveClass.getClass().getName()
                    + ", " + clazz.getClass().getName());
        }
    }
    // 删除无关代码
}

参考: Dubbo SPI之Adaptive详解Dubbo SPI自适应拓展机制

阅读 2.2k

Dubbo源码浅析
Dubbo源码学习交流
97 声望
15 粉丝
0 条评论
你知道吗?

97 声望
15 粉丝
文章目录
宣传栏