1. 程式人生 > >dubbo中的spi 技術

dubbo中的spi 技術

我們從圖中的

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

這句程式碼入手。

一、擴充套件載入器

我們先看getExtensionLoader(Protocol.class)這行程式碼,進入內部:

   public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
        if (type == null)
            throw new IllegalArgumentException("Extension type == null");
        if(!type.isInterface()) {
            throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
        }
        if(!withExtensionAnnotation(type)) {
            throw new IllegalArgumentException("Extension type(" + type + 
                    ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
        }
        
        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;
    }

    private ExtensionLoader(Class<?> type) {
        this.type = type;
        objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
    }

這個type 是  interface com.alibaba.dubbo.rpc.Protocol;

ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);這個EXTENSION_LOADERS是一個currentHashMap.剛進入這個方法的時候 這個EXTENSION_LOADERS裡面是沒有值的,所以load ==null  所以進入了 下面的一個判斷,

  EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));這串程式碼會調到上面的構造方法

我們剛才很明確的說了 type 是 interface com.alibaba.dubbo.rpc.Protocol;所以下面的三目運算子明顯是false。於是會進入

ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()方法。這邊繼續呼叫了getExtensionLoader(ExtensionFactory.class)方法, 於是又進入了程式碼圖上的流程, 只不過這個EXTENSION_LOADERS,放入了 一個type==interface com.alibaba.dubbo.common.extension.ExtensionFactory。 值是 一個ExtensionLoader的例項,只是這個例項的objectFactory==null;注意是interface com.alibaba.dubbo.common.extension.ExtensionFactory的objectFatory==null這裡interface com.alibaba.dubbo.rpc.Protocol的obectFactory是執行getAdaptiveExtension()之後的值。

小結一

我看過了 ServiceConfig 裡面也有這個擴充套件載入, 所以我想做一個初步總結(ps:老師說的,但是我加了一些自己的理解。如諾不對歡迎糾正)每一個dubbo:service或者dubbo:reference 標籤被解析後,裡面都有很多ExtensionLoader(cluster、protocol、proxyFactory),這些cluster、protocol 等等這些每一個類 或者說介面 都會對應一個ExtensionLoader,然後,ExtensionLoad中的objectFactory屬性是不是空值, 得看type 是不是interface com.alibaba.dubbo.common.extension.ExtensionFactory,是則為空。否則不為空.

二、獲取Protocol

我們繼續進入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;
    }

cachedAdaptiveInstance.get()這個是從一個Holder的型別的類裡面去快取的值, Holder 其實就放了 一個屬性(value)。在ExtensionLoad中Holder 被final 修飾, 意義為不可修改的(其實final修飾的欄位是可以通過反射進行修改的但是要在修改之前執行 attr.setAcessible(ture))。具體的修改final 值的連線。Holder中value 又是被volatile修飾的(對於基本型別的修改可以在隨後對多個執行緒的讀保持一致,但是對於引用型別如陣列,實體bean,僅僅保持引用的可見性,並不保證內容的可見性),

上個程式碼圖中instance =null 執行兩次是用了雙檢鎖,提高了同步程式碼塊的執行效率。

1)讀取專案中對應的三個檔案

下面呼叫getAdaptiveExtensionClass()->getExtensionClasses()->loadExtensionClasses();直接按照這個順序。 我們可以看到

 private Map<String, Class<?>> loadExtensionClasses() {
        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<?>>();
        loadFile(extensionClasses, DUBBO_INTERNAL_DIRECTORY);
        loadFile(extensionClasses, DUBBO_DIRECTORY);
        loadFile(extensionClasses, SERVICES_DIRECTORY);
        return extensionClasses;
    }
    

 

關鍵方法就是loadFile();那麼extensionClasses是一個hashMap 用來存放各個spi 解析出來擴充套件類的,感覺因為不管哪個reference或者service 呼叫這個他們的擴充套件工廠都是一樣的所以可以用hashMap 而不用currentHashMap 等等執行緒安全的(理解不對望告知)。

我們再看看

DUBBO_INTERNAL_DIRECTORY、DUBBO_DIRECTORY、SERVICES_DIRECTORY三個變數到底是什麼值, 請看下圖。

------------------------------------------------------分割線-------------------------------------------------------------

2)讀取檔案,通過反射機制生成類

loadFile()方法我今天來寫了,請看下面:

   private void loadFile(Map<String, Class<?>> extensionClasses, String dir) {
        String fileName = dir + type.getName();
        try {
            Enumeration<java.net.URL> urls;
            ClassLoader classLoader = findClassLoader();
            if (classLoader != null) {
                urls = classLoader.getResources(fileName);
            } else {
                urls = ClassLoader.getSystemResources(fileName);
            }
            if (urls != null) {
                while (urls.hasMoreElements()) {
                    java.net.URL url = urls.nextElement();
                    try {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
                        try {
                            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 = line.substring(0, i).trim();
                                            line = line.substring(i + 1).trim();
                                        }
                                        if (line.length() > 0) {
                                            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.");
                                            }
                                            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());
                                                }
                                            } 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);
                                                            }
                                                        }
                                                    }
                                                    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);
                                                        }
                                                        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());
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    } catch (Throwable t) {
                                        IllegalStateException e = new IllegalStateException("Failed to load extension class(interface: " + type + ", class line: " + line + ") in " + url + ", cause: " + t.getMessage(), t);
                                        exceptions.put(line, e);
                                    }
                                }
                            } // end of while read lines
                        } finally {
                            reader.close();
                        }
                    } catch (Throwable t) {
                        logger.error("Exception when load extension class(interface: " +
                                            type + ", class file: " + url + ") in " + url, t);
                    }
                } // end of while urls
            }
        } catch (Throwable t) {
            logger.error("Exception when load extension class(interface: " +
                    type + ", description file: " + fileName + ").", t);
        }
    }

我們的type=META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol.開頭的dir是要讀取dubbo.jar中 的某個位置,具體的值 是上面三個值中的一個比如(META-INF/dubbo/internal/)所以

fileName=META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol,findClassLoad也是獲取的ExtendFactory的一個類載入器,具體請自己看。首先這個classLoad不是null, 不管怎樣獲取BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream() "utf-8"));再之後我們正常的readline() 如果line 中有"#"號意思為註釋我們就pass。之後按等於號分割,左邊是key 右邊是value,

這邊會出現三種情況, 第一種如果類的註解有Adaptive.class。那麼將會賦值給cachedAdaptiveClass,根據以下程式碼我們可以得知一個type 型別只能有一個子類有Adaptive註解,否則報錯:

 (! cachedAdaptiveClass.equals(clazz)) {
                                                    throw new IllegalStateException("More than 1 adaptive class found: "
                                                            + cachedAdaptiveClass.getClass().getName()
                                                            + ", " + clazz.getClass().getName());
                                                }

 

第二如果讀取的類的建構函式裡面單放type型別的則放入cachedWrapperClasses中根據getConstructor(type)這句程式碼可以看出,

第三如果以上都不行則執行下面:如果一行讀取後name 無值,則取預設值:


                                                            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);
                                                            }
                                                        

如果有值, 我們先按,號分割, 這說明同一個子類可以有多個名字。然後將之存入傳進來的extesionClasses,而且讀取的三個檔案中如若相同的父類有相同的name即key 則報錯,因為這三個檔案的extesionClasses是同一個:

  Class<?> c = extensionClasses.get(n);
                                                            if (c == null) {
                                                                extensionClasses.put(n, clazz);
                                                            }

我們回到調取getExtensionClasses()方法的地方。:

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

我們來看看createAdaptiveExtensionClass()方法, 這個方法很總要,是另外一種proxy 的實現(不同於spring中), 很多人看不懂dubbo 就是因為這裡。

3)編譯動態代理類

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);
    }

這裡用字串拼接成一個code 然後通過編譯器生成class 檔案。這裡我會把code 拿出來。

package com.alibaba.dubbo.rpc;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class Protocol$Adpative 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.Invoker {
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 java.lang.Class {
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);
}
}

這下如果debugProtocol 型別的時候, 看到Protocol$Adaptive 就不要好奇在哪出現的,而且dubbo 到底走哪個類是根據url 的protocol屬性的值。。看上面程式碼就知道了,伏筆在getExtension(extName);正好利用了spi 技術。溜了12點(夜裡)了睡覺。