dubbo原始碼系列之filter的前生

qixiaobo發表於2018-01-05

title: dubbo原始碼系列之filter的前生 tags:

  • dubbo
  • filter
  • spi
  • extensionLoader categories: dubbo date: 2017-06-25 18:18:53

  1. 為什麼說dubbo的宣告式快取不好用!!!
  2. dubbo原始碼系列之filter的今世

dubbo的filter類似於spring的aop,提供了環繞增強功能。

參考dubbo快取程式碼分析

快取是filter的一個很典型的實現。

那麼filter是如何被呼叫的呢?

收到spring荼毒多年很自然的想到了spring的aop實現===》換湯不換藥的動態代理

114745_DV8E_871390.png

整篇故事要從invoker講起。

首先介紹一下Protocol介面

    /*
     * Copyright 1999-2011 Alibaba Group.
     * 
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     * 
     *      http://www.apache.org/licenses/LICENSE-2.0
     * 
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package com.alibaba.dubbo.rpc;
     
    import com.alibaba.dubbo.common.URL;
    import com.alibaba.dubbo.common.extension.Adaptive;
    import com.alibaba.dubbo.common.extension.SPI;
     
    /**
     * Protocol. (API/SPI, Singleton, ThreadSafe)
     *
     * @author william.liangf
     */
    @SPI("dubbo")
    public interface Protocol {
         
        /**
         * 獲取預設埠,當使用者沒有配置埠時使用。
         *
         * @return 預設埠
         */
        int getDefaultPort();
     
        /**
         * 暴露遠端服務:<br>
         * 1. 協議在接收請求時,應記錄請求來源方地址資訊:RpcContext.getContext().setRemoteAddress();<br>
         * 2. export()必須是冪等的,也就是暴露同一個URL的Invoker兩次,和暴露一次沒有區別。<br>
         * 3. export()傳入的Invoker由框架實現並傳入,協議不需要關心。<br>
         *
         * @param <T> 服務的型別
         * @param invoker 服務的執行體
         * @return exporter 暴露服務的引用,用於取消暴露
         * @throws RpcException 當暴露服務出錯時丟擲,比如埠已佔用
         */
        @Adaptive
        <T> Exporter<T> export(Invoker<T> invoker) throws RpcException;
     
        /**
         * 引用遠端服務:<br>
         * 1. 當使用者呼叫refer()所返回的Invoker物件的invoke()方法時,協議需相應執行同URL遠端export()傳入的Invoker物件的invoke()方法。<br>
         * 2. refer()返回的Invoker由協議實現,協議通常需要在此Invoker中傳送遠端請求。<br>
         * 3. 當url中有設定check=false時,連線失敗不能丟擲異常,並內部自動恢復。<br>
         *
         * @param <T> 服務的型別
         * @param type 服務的型別
         * @param url 遠端服務的URL地址
         * @return invoker 服務的本地代理
         * @throws RpcException 當連線服務提供方失敗時丟擲
         */
        @Adaptive
        <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException;
     
        /**
         * 釋放協議:<br>
         * 1. 取消該協議所有已經暴露和引用的服務。<br>
         * 2. 釋放協議所佔用的所有資源,比如連線和埠。<br>
         * 3. 協議在釋放後,依然能暴露和引用新的服務。<br>
         */
        void destroy();
     
    }
複製程式碼

SPI在上文已經描述過,可以得出結論 預設的protocol為dubbo

我們檢視檢視一下所有的protocol

    registry=com.alibaba.dubbo.registry.integration.RegistryProtocol
    filter=com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper
    listener=com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper
    mock=com.alibaba.dubbo.rpc.support.MockProtocol
    injvm=com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol
    dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol
    rmi=com.alibaba.dubbo.rpc.protocol.rmi.RmiProtocol
    hessian=com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol
    com.alibaba.dubbo.rpc.protocol.http.HttpProtocol
    com.alibaba.dubbo.rpc.protocol.webservice.WebServiceProtocol
    thrift=com.alibaba.dubbo.rpc.protocol.thrift.ThriftProtocol
    memcached=memcom.alibaba.dubbo.rpc.protocol.memcached.MemcachedProtocol
    redis=com.alibaba.dubbo.rpc.protocol.redis.RedisProtocol
複製程式碼

我們獲得了這些Protocol。 而Invoker的產生來自於

    @Adaptive
        <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException;
複製程式碼

該方法有一個Adaptive標籤

    package com.alibaba.dubbo.common.extension;
     
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
     
    import com.alibaba.dubbo.common.URL;
     
    /**
     * 在{@link ExtensionLoader}生成Extension的Adaptive Instance時,為{@link ExtensionLoader}提供資訊。
     *
     * @author ding.lid
     * @export
     *
     * @see ExtensionLoader
     * @see URL
     */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE, ElementType.METHOD})
    public @interface Adaptive {
         
        /**
         * 從{@link URL}的Key名,對應的Value作為要Adapt成的Extension名。
         * <p>
         * 如果{@link URL}這些Key都沒有Value,使用 用 預設的擴充套件(在介面的{@link SPI}中設定的值)。<br>
         * 比如,<code>String[] {"key1", "key2"}</code>,表示
         * <ol>
         * <li>先在URL上找key1的Value作為要Adapt成的Extension名;
         * <li>key1沒有Value,則使用key2的Value作為要Adapt成的Extension名。
         * <li>key2沒有Value,使用預設的擴充套件。
         * <li>如果沒有設定預設擴充套件,則方法呼叫會丟擲{@link IllegalStateException}。
         * </ol>
         * <p>
         * 如果不設定則預設使用Extension介面類名的點分隔小寫字串。<br>
         * 即對於Extension介面{@code com.alibaba.dubbo.xxx.YyyInvokerWrapper}的預設值為<code>String[] {"yyy.invoker.wrapper"}</code>
         *
         * @see SPI#value()
         */
        String[] value() default {};
         
    }
複製程式碼

該註解可以同時出現在類或者方法上

這個註釋也很有意思,當使用該註解的時候會在Url引數上找到對應的param以此作為擴充套件,如果沒有則使用註解的value,否則就使用以點分割單詞的方式。

我們從原始碼分析一下

    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);
        }
    }
複製程式碼

這段程式碼負責從指定的資料夾下面載入指定Class的SPI(此處使用Dubbo自己實現而不是ServiceLoader)

為了便於說明 我們從Protocl來說明。

因此查詢檔案

META-INF/services/com.alibaba.dubbo.rpc.Protocol

    META-INF/dubbo/com.alibaba.dubbo.rpc.Protocol

    META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol
複製程式碼
    registry=com.alibaba.dubbo.registry.integration.RegistryProtocol
    filter=com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper
    listener=com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper
    mock=com.alibaba.dubbo.rpc.support.MockProtocol
    injvm=com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol
    dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol
    rmi=com.alibaba.dubbo.rpc.protocol.rmi.RmiProtocol
    hessian=com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol
    com.alibaba.dubbo.rpc.protocol.http.HttpProtocol
    com.alibaba.dubbo.rpc.protocol.webservice.WebServiceProtocol
    thrift=com.alibaba.dubbo.rpc.protocol.thrift.ThriftProtocol
    memcached=memcom.alibaba.dubbo.rpc.protocol.memcached.MemcachedProtocol
    redis=com.alibaba.dubbo.rpc.protocol.redis.RedisProtocol
複製程式碼

此處按照順序載入

  1. check該類是否在類上標有註解Adaptive 如果有那麼將該類標記為Adaptive類(校驗是都存在多個,如果存在多個則直接報錯)
  2. 如果不包含該註解那麼將該類初始化(check是否包含單個引數為com.alibaba.dubbo.rpc.Protocol的建構函式,如果有那麼將該類放入wrapper集合,否則使用午餐建構函式進行初始化)===》我們可以得出結論 如果spi檔案中對應的類為包含介面型別的單引數建構函式 那麼將被標記為wrapper,否則呼叫無引數建構函式進行初始化也就是真正的spi實現
  3. 那麼Adaptive的含義究竟是什麼呢?中文叫適配,通常來說我們會把這種理解為自適應。那麼自適應的結果是根據Url中的引數返回不同的實現(頗有依賴反轉的含義),當spi檔案中存在的型別不包含Adaptive的結果時我們會生成程式碼
        private Class<?> getAdaptiveExtensionClass() {
            getExtensionClasses();
            if (cachedAdaptiveClass != null) {
                return cachedAdaptiveClass;
            }
            return cachedAdaptiveClass = createAdaptiveExtensionClass();
        }
         
        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);
        }
         
        private String createAdaptiveExtensionClassCode() {
            StringBuilder codeBuidler = new StringBuilder();
            Method[] methods = type.getMethods();
            boolean hasAdaptiveAnnotation = false;
            for(Method m : methods) {
                if(m.isAnnotationPresent(Adaptive.class)) {
                    hasAdaptiveAnnotation = true;
                    break;
                }
            }
            // 完全沒有Adaptive方法,則不需要生成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() + "$Adpative" + " implements " + type.getCanonicalName() + " {");
             
            for (Method method : methods) {
                Class<?> rt = method.getReturnType();
                Class<?>[] pts = method.getParameterTypes();
                Class<?>[] ets = method.getExceptionTypes();
         
                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 {
                    int urlTypeIndex = -1;
                    for (int i = 0; i < pts.length; ++i) {
                        if (pts[i].equals(URL.class)) {
                            urlTypeIndex = i;
                            break;
                        }
                    }
                    // 有型別為URL的引數
                    if (urlTypeIndex != -1) {
                        // 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);
                    }
                    // 引數沒有URL型別
                    else {
                        String attribMethod = null;
                         
                        // 找到引數的URL屬性
                        LBL_PTS:
                        for (int i = 0; i < pts.length; ++i) {
                            Method[] ms = pts[i].getMethods();
                            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;
                                }
                            }
                        }
                        if(attribMethod == null) {
                            throw new IllegalStateException("fail to create adative class for interface " + type.getName()
                                  + ": not found url parameter or url attribute in parameters of method " + method.getName());
                        }
                         
                        // Null point check
                        String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"%s argument == null\");",
                                        urlTypeIndex, pts[urlTypeIndex].getName());
                        code.append(s);
                        s = String.format("\nif (arg%d.%s() == null) throw new IllegalArgumentException(\"%s argument %s() == null\");",
                                        urlTypeIndex, attribMethod, pts[urlTypeIndex].getName(), attribMethod);
                        code.append(s);
         
                        s = String.format("%s url = arg%d.%s();",URL.class.getName(), urlTypeIndex, attribMethod);
                        code.append(s);
                    }
                     
                    String[] value = adaptiveAnnotation.value();
                    // 沒有設定Key,則使用“擴充套件點介面名的點分隔 作為Key
                    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()};
                    }
                     
                    boolean hasInvocation = false;
                    for (int i = 0; i < pts.length; ++i) {
                        if (pts[i].getName().equals("com.alibaba.dubbo.rpc.Invocation")) {
                            // Null Point check
                            String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"invocation == null\");", i);
                            code.append(s);
                            s = String.format("\nString methodName = arg%d.getMethodName();", i);
                            code.append(s);
                            hasInvocation = true;
                            break;
                        }
                    }
                     
                    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);
                        }
                    }
                    code.append("\nString extName = ").append(getNameCode).append(";");
                    // check extName == null?
                    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);
                     
                    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 ");
                    }
         
                    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(");");
                }
                 
                codeBuidler.append("\npublic " + rt.getCanonicalName() + " " + method.getName() + "(");
                for (int i = 0; i < pts.length; i ++) {
                    if (i > 0) {
                        codeBuidler.append(", ");
                    }
                    codeBuidler.append(pts[i].getCanonicalName());
                    codeBuidler.append(" ");
                    codeBuidler.append("arg" + i);
                }
                codeBuidler.append(")");
                if (ets.length > 0) {
                    codeBuidler.append(" throws ");
                    for (int i = 0; i < ets.length; i ++) {
                        if (i > 0) {
                            codeBuidler.append(", ");
                        }
                        codeBuidler.append(pts[i].getCanonicalName());
                    }
                }
                codeBuidler.append(" {");
                codeBuidler.append(code.toString());
                codeBuidler.append("\n}");
            }
            codeBuidler.append("\n}");
            if (logger.isDebugEnabled()) {
                logger.debug(codeBuidler.toString());
            }
            return codeBuidler.toString();
        }
    
    這段程式碼負責生成Java程式碼,然後使用Compiler進行編譯,其次生成對應的class===》注意這段程式碼首先校驗對應的介面包含Adaptive的方法,如果不包含直接不生成(因為無需適配)
    
    其次找到介面對應Adaptive方法的Url引數(也可能在JavaBean的引數中)根據URL獲取對應引數 依次來找到對應的適配(通過getExtension方法)
    
        /**
            * 返回指定名字的擴充套件。如果指定名字的擴充套件不存在,則拋異常 {@link IllegalStateException}.
            *
            * @param name
            * @return
            */
        @SuppressWarnings("unchecked")
        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;
        }
    
    此處獲取指定名稱的Extension如果快取中沒有則去建立
    
        @SuppressWarnings("unchecked")
        private T createExtension(String name) {
            Class<?> clazz = getExtensionClasses().get(name);
            if (clazz == null) {
                throw findException(name);
            }
            try {
                T instance = (T) EXTENSION_INSTANCES.get(clazz);
                if (instance == null) {
                    EXTENSION_INSTANCES.putIfAbsent(clazz, (T) clazz.newInstance());
                    instance = (T) EXTENSION_INSTANCES.get(clazz);
                }
                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);
            }
        }
         
        private T injectExtension(T instance) {
            try {
                if (objectFactory != null) {
                    for (Method method : instance.getClass().getMethods()) {
                        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) : "";
                                Object object = objectFactory.getExtension(pt, property);
                                if (object != null) {
                                    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;
        }
    
    建立指定名稱的Extension(此處就是將spi檔案的對應檔案歸類後放在map中進行對映查詢)
    
    此時會執行injectExtension這樣實現了依賴注入。
    
    上文中還有個關鍵點,使用Wrapper來包裝對應的extension,比如Protocol此時就會有兩個包裝類
    
        ProtocolFilterWrapper和ProtocolListenerWrapper
    
        而filter的主要實現通過ProtocolFilterWrapper來完成。

    public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
        if (Constants.REGISTRY_PROTOCOL.equals(invoker.getUrl().getProtocol())) {
            return protocol.export(invoker);
        }
        return protocol.export(buildInvokerChain(invoker, Constants.SERVICE_FILTER_KEY, Constants.PROVIDER));
    }
     
    public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
        if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
            return protocol.refer(type, url);
        }
        return buildInvokerChain(protocol.refer(type, url), Constants.REFERENCE_FILTER_KEY, Constants.CONSUMER);
    }
     
    public void destroy() {
        protocol.destroy();
    }
     
    private static <T> Invoker<T> buildInvokerChain(final Invoker<T> invoker, String key, String group) {
        Invoker<T> last = invoker;
        List<Filter> filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);
        if (filters.size() > 0) {
            for (int i = filters.size() - 1; i >= 0; i --) {
                final Filter filter = filters.get(i);
                final Invoker<T> next = last;
                last = new Invoker<T>() {
     
                    public Class<T> getInterface() {
                        return invoker.getInterface();
                    }
     
                    public URL getUrl() {
                        return invoker.getUrl();
                    }
     
                    public boolean isAvailable() {
                        return invoker.isAvailable();
                    }
     
                    public Result invoke(Invocation invocation) throws RpcException {
                        return filter.invoke(next, invocation);
                    }
     
                    public void destroy() {
                        invoker.destroy();
                    }
     
                    @Override
                    public String toString() {
                        return invoker.toString();
                    }
                };
            }
        }
        return last;
    }

 
複製程式碼

filter的實現完全由buildInvokerChain來控制

下一篇再來細細分說filter的載入過程

相關文章