dubbo源碼之動態擴展 v1.0

一.參考
dubbo啓動,使用spi動態擴展的地方參考之前寫的<dubbo源碼之啓動、導出服務>

二.架構
1.每個接口或者ExtensionFactory都對應一個ExtensionLoader。對接口主要有四步操作:
(1).獲取接口的ExtensionLoader.
(2).從擴展文件中讀取對應接口的所有實現類
(3).創建優先級最高,真正使用的類的實例
(4).注入這個類的屬性,通過setxxx方法.
2.從優先級看,@Adaptive類 > url 路徑 > @Spi的值.
3.@Adaptive用在方法上時,key是url的參數名,通過這個參數名的取值判斷用哪個實現類.
@Activate用於配置不同實現類的分組,組內的優先級.


三.源碼細節


(一).Protocol接口加載


測試代碼是ServiceConfig的成員protocl,初始化代碼爲
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
加載dubbo的Protocol接口的ExtensionLoader.
1.ExtensionLoader#getExtensionLoader()方法,入參爲Protocol接口。代碼如下:

public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
	//接口不能爲空
    if (type == null)
        throw new IllegalArgumentException("Extension type == null");
    //傳入type必須是接口
    if (!type.isInterface()) {
        throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
    }
    //接口必須帶有@spi註解
    if (!withExtensionAnnotation(type)) {
        throw new IllegalArgumentException("Extension type(" + type +
                ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
    }
    //在緩存ExtensionLoader#EXTENSION_LOADERS是否已經有loader了,沒有再創建
    ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
    if (loader == null) {
    	//創建ExtensionLoader,操作都在構造方法裏面.構造方法在下面代碼.
        EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));
        loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
    }
    return loader;
}
private ExtensionLoader(Class<?> type) {
    this.type = type;
    //第一次使用ExtensionLoader,先創建ExtensionFactory接口工廠的ExtensionLoader.
    //然後調用ExtensionLoader#getAdaptiveExtension()獲取ExtensionFactory的實例.
    objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
}

2.獲取擴展接口的實例
入口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();
                        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;
}

創建擴展接口的實例
入口 ExtensionLoader#createAdaptiveExtension

private T createAdaptiveExtension() {
    try {
    	/* 先調用getAdaptiveExtensionClass()獲取接口實際使用的實現類,代碼如下.
         再調用newInstance()創建這個實現類的對象
         最後調用injectExtension()對這個對象的屬性進行注入.
         對於Protocol接口,這裏創建動態生成的Protocol$Adaptive類的實例 */
        return injectExtension((T) getAdaptiveExtensionClass().newInstance());
    } catch (Exception e) {
        throw new IllegalStateException("Can not create adaptive extension " + type + ", cause: " + e.getMessage(), e);
    }
}

獲取接口的擴展類
入口ExtensionLoader#getAdaptiveExtensionClass()

private Class<?> getAdaptiveExtensionClass() {
	//獲取擴展類
    getExtensionClasses();
    if (cachedAdaptiveClass != null) {
        //如果有帶Adaptive註解的,優先返回有帶Adaptive註解的實現類
        return cachedAdaptiveClass;
    }
    //Protocol接口的實現類沒有@Adaptive註解的,進到這裏,在後面6處分析
    return cachedAdaptiveClass = createAdaptiveExtensionClass();
}

方法ExtensionLoader#getExtensionClasses()獲取接口的所有實現類
 

private Map<String, Class<?>> getExtensionClasses() {
    Map<String, Class<?>> classes = cachedClasses.get();
    if (classes == null) {
        synchronized (cachedClasses) {
            classes = cachedClasses.get();
            if (classes == null) {
            	//讀取擴展接口文件,加載,代碼在下面
                classes = loadExtensionClasses();
                //對於ExtensionFactory,這裏有兩個,SpiExtensionFactory和SpringExtensionFactory.
                cachedClasses.set(classes);
            }
        }
    }
    return classes;
}

3.讀取spi文件的接口的所有的實現類
如果是protocol協議,這裏能獲取到{RegistryProtocol,InjvmProtocol,ThriftProtocol,DubboProtocol,MockProtocol,HttpProtocol,RedisProtocol,RmiProtocol}八個實現類,默認使用DubboProtocol.
入口是ExtensionLoader#loadExtensionClasses()

private Map<String, Class<?>> loadExtensionClasses() {
	//獲取接口的spi註解,比如ExtensionFactory,Protocol
    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];
        }
    }

    Map<String, Class<?>> extensionClasses = new HashMap<String, Class<?>>();
    //加載META-INF/dubbo/internal/目錄下的擴展文件
    loadFile(extensionClasses, DUBBO_INTERNAL_DIRECTORY);
    //加載META-INF/dubbo/目錄下的擴展文件
    loadFile(extensionClasses, DUBBO_DIRECTORY);
    //加載META-INF/services/目錄下的擴展文件
    loadFile(extensionClasses, SERVICES_DIRECTORY);
    return extensionClasses;
}

讀取文件,入口是ExtensionLoader#loadFile().加載到的實現類都放到extensionClasses.key是別名,value是實現類.
 

private void loadFile(Map<String, Class<?>> extensionClasses, String dir) {
	//格式是META-INF/dubbo/internal/com.alibaba.dubbo.common.extension.ExtensionFactory 
	//目錄名+接口路徑文件名
    String fileName = dir + type.getName();
    try {
        Enumeration<java.net.URL> urls;
        //調用ExtensionLoader.class.getClassLoader()獲取ClassLoader
        ClassLoader classLoader = findClassLoader();
        if (classLoader != null) {
        	//獲取文件路徑的url格式
            urls = classLoader.getResources(fileName);
        } else {
            urls = ClassLoader.getSystemResources(fileName);
        }
        if (urls != null) {
        	//遍歷所有文件路徑的url
            while (urls.hasMoreElements()) {
            	/* 接口文件的本地磁盤路徑file:/xxx/dubbo-config/xxx/internal/xxx.ExtensionFactory格式 */
                java.net.URL url = urls.nextElement();
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
                    try {
                    	/* 一行行遍歷接口文件,對於ExtensionFactory接口,在dubbo-config只有下面一行 spring=com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory
                    	在dubbo-common下面有adaptive=com.alibaba.dubbo.common.extension.factory.AdaptiveExtensionFactory
						spi=com.alibaba.dubbo.common.extension.factory.SpiExtensionFactory兩行 */

                        String line = null;
                        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別名部分
                                        name = line.substring(0, i).trim();
                                        //接口實現類賦值給line,這裏是SpringExtensionFactory類全路徑
                                        line = line.substring(i + 1).trim();
                                    }
                                    if (line.length() > 0) {
                                    	//創建實現類的Class對象
                                        Class<?> clazz = Class.forName(line, true, classLoader);
                                        //判斷實現類是否是該接口的
                                        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.");
                                        }
                                        //實現類上是否有Adaptive註解
                                        if (clazz.isAnnotationPresent(Adaptive.class)) {
                                            if (cachedAdaptiveClass == null) {
                                                //有Adaptive註解的放到成員cachedAdaptiveClass裏面,這個優先級最高
                                                cachedAdaptiveClass = clazz;
                                            } else if (!cachedAdaptiveClass.equals(clazz)) {
                                                throw new IllegalStateException("More than 1 adaptive class found: "
                                                        + cachedAdaptiveClass.getClass().getName()
                                                        + ", " + clazz.getClass().getName());
                                            }
                                        } else {
                                            try {
                                            	//實現類是否有參數爲本接口類型的構造方法,這樣後面可以把多個實現類鏈接起來
                                                clazz.getConstructor(type);
                                                Set<Class<?>> wrappers = cachedWrapperClasses;
                                                if (wrappers == null) {
                                                    cachedWrapperClasses = new ConcurrentHashSet<Class<?>>();
                                                    wrappers = cachedWrapperClasses;
                                                }
                                                wrappers.add(clazz);
                                            } catch (NoSuchMethodException e) {
                                            	//實現類是否有無參構造方法
                                                clazz.getConstructor();
                                                if (name == null || name.length() == 0) {
                                                    name = findAnnotationName(clazz);
                                                    if (name == null || name.length() == 0) {
                                                        if (clazz.getSimpleName().length() > type.getSimpleName().length()
                                                                && clazz.getSimpleName().endsWith(type.getSimpleName())) {
                                                            name = clazz.getSimpleName().substring(0, clazz.getSimpleName().length() - type.getSimpleName().length()).toLowerCase();
                                                        } else {
                                                            throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + url);
                                                        }
                                                    }
                                                }
                                                //做正則表達式匹配name,對於SpringExtensionFactory,這裏是spring
                                                String[] names = NAME_SEPARATOR.split(name);
                                                if (names != null && names.length > 0) {
                                                    Activate activate = clazz.getAnnotation(Activate.class);
                                                    if (activate != null) {
                                                    	//如果實現類有Activate註解,加入cachedActivates緩存.
                                                        cachedActivates.put(names[0], activate);
                                                    }
                                                    for (String n : names) {
                                                        if (!cachedNames.containsKey(clazz)) {
                                                        	//加入已經解析的名稱緩存cachedNames
                                                            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());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } catch (Throwable t) {
                                   ...
                                }
                            }
                        } // end of while read lines
                    } finally {
                    	//關閉讀文件句柄
                        reader.close();
                    }
                } catch (Throwable t) {
                    ...
                }
            } // end of while urls
        }
    } catch (Throwable t) {
       ...
    }
}

4.創建接口的實現類的對象,以ExtensionFactory爲例,實現類是AdaptiveExtensionFactory,它的優先級最高,帶有@Adaptive註解.代碼如下:
 

public AdaptiveExtensionFactory() {

    //進到這裏,可以從緩存直接拿到了,因爲是調用ExtensionLoader.getExtensionLoader(ExtensionFactory.class)纔到這裏
    ExtensionLoader<ExtensionFactory> loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
    List<ExtensionFactory> list = new ArrayList<ExtensionFactory>();
    //遍歷所有ExtensionFactory接口的實現類,這裏與兩個,spi和spring
    for (String name : loader.getSupportedExtensions()) {
        /* 創建所有ExtensionFactory接口實現類的對象,這裏先去創建SpiExtensionFactory類的對象,然後調用ExtensionLoader#injectExtension()方法注入對象 */
        list.add(loader.getExtension(name));
    }
    factories = Collections.unmodifiableList(list);
}

ExtensionLoader#getExtension()調用ExtensionLoader#createExtension()完成操作.代碼如下:
比如傳入的name是"spi",

private T createExtension(String name) {
    //獲取"spi"的實現類SpiExtensionFactory
    Class<?> clazz = getExtensionClasses().get(name);
    if (clazz == null) {
        throw findException(name);
    }
    try {
        T instance = (T) EXTENSION_INSTANCES.get(clazz);
        //第一次獲取爲空
        if (instance == null) {
            //創建實現類的對象,比如創建SpiExtensionFactory類的對象
            EXTENSION_INSTANCES.putIfAbsent(clazz, (T) clazz.newInstance());
            instance = (T) EXTENSION_INSTANCES.get(clazz);
        }
        //對對象進行注入,比如對SpiExtensionFactory對象進行注入,主要是調用它的setxxx方法.
        injectExtension(instance);
        Set<Class<?>> wrapperClasses = cachedWrapperClasses;
        if (wrapperClasses != null && wrapperClasses.size() > 0) {
            for (Class<?> wrapperClass : wrapperClasses) {
                instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));
            }
        }
        return instance;
    } catch (Throwable t) {
        throw new IllegalStateException("Extension instance(name: " + name + ", class: " +
                type + ")  could not be instantiated: " + t.getMessage(), t);
    }
}

5.ioc,注入對象屬性

private T injectExtension(T instance) {
    try {
        if (objectFactory != null) {
            for (Method method : instance.getClass().getMethods()) {
                //只處理setxxx()方法,方法參數只有一個
                if (method.getName().startsWith("set")
                        && method.getParameterTypes().length == 1
                        && Modifier.isPublic(method.getModifiers())) {
                    //獲取要注入的方法的參數類型
                    Class<?> pt = method.getParameterTypes()[0];
                    try {
                        String property = method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : "";
                        /* 調用ExtensionFactory#getExtension()獲取要注入的參數的對象實例,property是參數名,主要是去容器中獲取bean的實例對象,比如spring的singleObjects*/
                        Object object = objectFactory.getExtension(pt, property);
                        if (object != null) {
                            //調用setxxx()方法注入參數對象
                            method.invoke(instance, object);
                        }
                    } catch (Exception e) {
                        logger.error("fail to inject via method " + method.getName()
                                + " of interface " + type.getName() + ": " + e.getMessage(), e);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return instance;
}

6.創建自適應的擴展類,在接口的多個實現類中選一個.
從前面的2調用過來,比如Protocol接口 

private Class<?> createAdaptiveExtensionClass() {
    //代碼在下面分析,Protocol這裏返回Protocol$Adaptive類的實現代碼字串
    String code = createAdaptiveExtensionClassCode();
    //獲取當前類的ClassLoader
    ClassLoader classLoader = findClassLoader();
    //獲取Compiler接口的實現類JavassistCompiler
    com.alibaba.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.compiler.Compiler.class).getAdaptiveExtension();
    //調用Compiler接口的實現類JavassistCompiler把上面的字符串代碼編譯成Class
    return compiler.compile(code, classLoader);
}

創建擴展類的字符串代碼最後生成的字符串如下圖,

代碼如下:

private String createAdaptiveExtensionClassCode() {
    StringBuilder codeBuidler = new StringBuilder();
    //獲取接口內部的類,比如Protocol接口的類
    Method[] methods = type.getMethods();
    boolean hasAdaptiveAnnotation = false;
    //遍歷所有方法,判斷是否有帶@Adaptive註解的方法
    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.
    //所有類都沒有adaptive方法,則拋出異常
    if (!hasAdaptiveAnnotation)
        throw new IllegalStateException("No adaptive method on extension " + type.getName() + ", refuse to create the adaptive class!");

    codeBuidler.append("package " + type.getPackage().getName() + ";");
    codeBuidler.append("\nimport " + ExtensionLoader.class.getName() + ";");
    codeBuidler.append("\npublic class " + type.getSimpleName() + "$Adaptive" + " implements " + type.getCanonicalName() + " {");

    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) {
            //沒有Adaptive註解
            code.append("throw new UnsupportedOperationException(\"method ")
                    .append(method.toString()).append(" of interface ")
                    .append(type.getName()).append(" is not adaptive method!\");");
        } else {
            //有Adaptive註解的
            int urlTypeIndex = -1;
            //取出參數類型是url的參數位置索引
            for (int i = 0; i < pts.length; ++i) {
                if (pts[i].equals(URL.class)) {
                    urlTypeIndex = i;
                    break;
                }
            }
            // found parameter in URL type
            if (urlTypeIndex != -1) {
                //參數中有url類型的
                // Null Point check
                String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"url == null\");",
                        urlTypeIndex);
                code.append(s);

                s = String.format("\n%s url = arg%d;", URL.class.getName(), urlTypeIndex);
                code.append(s);
            }
            // did not find parameter in URL type
            else {
                //參數中沒有url類型的,比如Protocol#export(Invoker invoker).沒有url參數,帶有@Adaptive註解,返回Exporter
                String attribMethod = null;

                // find URL getter method
                //遍歷所有參數
                LBL_PTS:
                for (int i = 0; i < pts.length; ++i) {
                    Method[] ms = pts[i].getMethods();
                    //比如獲取到Invoker類型的參數,遍歷Invoker的所有成員方法
                    //找到一個getxxx方法,返回值是URL類型,記下方法名.
                    for (Method m : ms) {
                        String name = m.getName();
                        if ((name.startsWith("get") || name.length() > 3)
                                && Modifier.isPublic(m.getModifiers())
                                && !Modifier.isStatic(m.getModifiers())
                                && m.getParameterTypes().length == 0
                                && m.getReturnType() == URL.class) {
                            urlTypeIndex = i;
                            attribMethod = name;
                            break LBL_PTS;
                        }
                    }
                }
               ...
            }

            //獲取方法上@Adaptive註解的value值
            String[] value = adaptiveAnnotation.value();
            // value is not set, use the value generated from class name as the key
            if (value.length == 0) {
                //沒有值的話,默認取dubbo接口的名稱,比如protocol
                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()};
            }

            //獲取方法的所有參數,看是否有Invocation類型.
            boolean hasInvocation = false;
            for (int i = 0; i < pts.length; ++i) {
                if (pts[i].getName().equals("com.alibaba.dubbo.rpc.Invocation")) {
                   ...
                }
            }

            //獲取接口的默認擴展的實現名
            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
                                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);
                }
            }
           ...
        }
        ...
    }
    codeBuidler.append("\n}");
    return codeBuidler.toString();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章