dubbo-源码阅读之dubboSpi实现原理

dubboSPI实现思想跟javaspi的思想差不多javaspi是ServiceLoad 而dubbo自己写的是ExtensionLoader

SPI接口定义

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface SPI {
    String value() default "";//扩展点的key
}

说明:标识接口是否是spi接口。只有spi接口才会去扫描相应的配置文件动态加载

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Adaptive {
    String[] value() default {};//扩展点的key
}

说明:如果打在类上 则这个类是适配器类一个程序(只能有一个),如果打在接口上则需要使用javassitis动态生成适配类。

ExtensionLoader类

静态成员

当前类的所有对象共享

//[1]扫描服务实现的文件存放
private static final String SERVICES_DIRECTORY = "META-INF/services/";
//[2]扫描服务实现的文件存放
private static final String DUBBO_DIRECTORY = "META-INF/dubbo/";
//[3]扫描服务实现的文件存放
private static final String DUBBO_INTERNAL_DIRECTORY = "META-INF/dubbo/internal/";
private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*");
//[4]每个class类都会创建一个对应ExtensionLoader 可以理解成缓存
private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap();
//[5]每个class类的对象缓存
private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap();

成员变量

//[6]
private
final Class<?> type;//当前接口class //[7]创建对象工厂 默认2种实现SPIExtensionFactory SpringExtensionFactory 一个是扫描文件加载 一个在spring容器查找 private final ExtensionFactory objectFactory; //[8]存储每个实现类的key 就是mate-info文件下dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap(); //[9]指定接口class 扫描到的所有实现类的class private final Holder<Map<String, Class<?>>> cachedClasses = new Holder(); //暂时不造干嘛的 private final Map<String, Activate> cachedActivates = new ConcurrentHashMap(); //[10]存储对应接口class实现类打上了@Adaptive 注解的class private volatile Class<?> cachedAdaptiveClass = null; //[11]存储当前接口class实现类的对象 private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap(); //[12]s private String cachedDefaultName; //[13]缓存适配器实现类的对象 private final Holder<Object> cachedAdaptiveInstance = new Holder(); private volatile Throwable createAdaptiveInstanceError; //[14]缓存扫描实现类 含有构造函数参数包含当前接口class类型的。 没创建一个对象就会遍历然后生成层层代理返回 private Set<Class<?>> cachedWrapperClasses; private Map<String, IllegalStateException> exceptions = new ConcurrentHashMap();

ExtensionLoader加载过程

dubbo里面有大量这样的代码,这些都是dubbo的扩展点。比如我们可以自定义协议。然后在标签配置好我们自定义协议属属性就好了

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

1.首先我们看Protocol.class是什么

@SPI("dubbo")
public interface Protocol {
    int getDefaultPort();

    @Adaptive
    <T> Exporter<T> export(Invoker<T> var1) throws RpcException;

    @Adaptive
    <T> Invoker<T> refer(Class<T> var1, URL var2) throws RpcException;

    void destroy();
}

2.我们看一下dubbo内置的协议实现

3.断点进入.getExtensionLoader(Protocol.class)

    public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
        if (type == null) {
            throw new IllegalArgumentException("Extension type == null");
        } else if (!type.isInterface()) {//如果不是接口抛出异常
            throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
        } else if (!withExtensionAnnotation(type)) {//如果没有打上@SPI注解抛出异常
            throw new IllegalArgumentException("Extension type(" + type + ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
        } else {
            //尝试从缓存中获取ExtensionLoader 静态成员[4]变量
            ExtensionLoader<T> loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);
            if (loader == null) {
                //创建一个对应类型ExtensionLoader并缓存
                EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));
                //返回
                loader = (ExloadertensionLoader) EXTENSION_LOADERS.get(type);
            }

            return loader;
        }
    }

4.我们进入new ExtensionLoader(type)构造函数看做了什么

 

    private ExtensionLoader(Class<?> type) {
        this.type = type;//先把当前class存起来 对应成员变量[6]
        /**首先判断当前类型是不是加载工厂类型,如果是工厂则为null因为工厂
        loader不需要objectFactory。如果不是用同样的方式获取工厂并赋值给laoder的成员变量[7]
         **/
        this.objectFactory = type == ExtensionFactory.class ?
null :
(ExtensionFactory) getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension(); }

Protocol.class!=ExtensionFactory.class  所以会执行(ExtensionFactory) getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()

(ExtensionFactory) getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()是不是很熟悉。表示创建对象的工厂 也是我们可以自定义的。

4.我们看一下ExtensionFactory 

@SPI
public interface ExtensionFactory {
    <T> T getExtension(Class<T> var1, String var2);
}

可以发现也是一个SPI接口

5.ExtensionFactory 实现类

6.SpringExtensionFactory

会根据name在spring容器里面找

public <T> T getExtension(Class<T> type, String name) {
        Iterator i$ = contexts.iterator();

        while(i$.hasNext()) {
            ApplicationContext context = (ApplicationContext)i$.next();
            if (context.containsBean(name)) {
                Object bean = context.getBean(name);
                if (type.isInstance(bean)) {
                    return bean;
                }
            }
        }

        return null;
    }

7.SpiExtensionFactory

public <T> T getExtension(Class<T> type, String name) {
        if (type.isInterface() && type.isAnnotationPresent(SPI.class)) {
            ExtensionLoader<T> loader = ExtensionLoader.getExtensionLoader(type);
            if (loader.getSupportedExtensions().size() > 0) {
                return loader.getAdaptiveExtension();
            }
        }

        return null;
    }

等同于getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()

8.AdaptiveExtensionFactory

@Adaptive
public class AdaptiveExtensionFactory implements ExtensionFactory {
    private final List<ExtensionFactory> factories;//存放了上面2种适配器的实现

    public AdaptiveExtensionFactory() {
        ExtensionLoader<ExtensionFactory> loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
        List<ExtensionFactory> list = new ArrayList();
        Iterator i$ = loader.getSupportedExtensions().iterator();
  
        while(i$.hasNext()) {
            String name = (String)i$.next();
            list.add(loader.getExtension(name));
        }

        this.factories = Collections.unmodifiableList(list);
    }

//会遍历SPIFactory和SpringFactory找到对应的key的bean
public <T> T getExtension(Class<T> type, String name) { Iterator i$ = this.factories.iterator(); Object extension; do { if (!i$.hasNext()) { return null; } ExtensionFactory factory = (ExtensionFactory)i$.next(); extension = factory.getExtension(type, name); } while(extension == null); return extension; } }

这个类打上了@Adaptive 表示是适配器类

9.getAdaptiveExtension()

回到我们前面 ExtensionLoader.getExtensionLoader(type) 只是单例创建一个对应接口class的Loader 真正加载在getAdaptiveExtension()

    public T getAdaptiveExtension() {
        Object instance = this.cachedAdaptiveInstance.get();
        if (instance == null) {
            if (this.createAdaptiveInstanceError != null) {
                throw new IllegalStateException("fail to create adaptive instance: " + this.createAdaptiveInstanceError.toString(), this.createAdaptiveInstanceError);
            }
            Holder var2 = this.cachedAdaptiveInstance;
            synchronized (this.cachedAdaptiveInstance) {
                instance = this.cachedAdaptiveInstance.get();
                //首先判断这个loader有没有默认的适配器类 如果没有 则加载(也可以理解成是否加载过一次了。因为每次加载都会生成一个适配器类)
                if (instance == null) {
                    try {
                        instance = this.createAdaptiveExtension();
                        //这里就是加载过后赋值 上面的判断,第二次再获取就直接从缓存里面拿 对应成员变量[10]
                        this.cachedAdaptiveInstance.set(instance);
                    } catch (Throwable var5) {
                        this.createAdaptiveInstanceError = var5;
                        throw new IllegalStateException("fail to create adaptive instance: " + var5.toString(), var5);
                    }
                }
            }
        }

        return instance;
    }

10.再进入这个方法this.createAdaptiveExtension()

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

1.先调用this.getAdaptiveExtensionClass()获取一个class

2.再创建.newInstance()反射创建实例

3.injectExtension再将实例交给这个方法处理

11.1 先看getAdaptiveExtensionClass

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

11.1.1 getExtensionClasses

 private Map<String, Class<?>> getExtensionClasses() {
        //对应成员变量9
        Map<String, Class<?>> classes = (Map) this.cachedClasses.get();
        if (classes == null) {//判断是否加载过了 如果加载过了直接返回
            Holder var2 = this.cachedClasses;
            synchronized (this.cachedClasses) {
                classes = (Map) this.cachedClasses.get();
                if (classes == null) {
                    classes = this.loadExtensionClasses();
                    this.cachedClasses.set(classes);//对应上面的判断加载后设置到缓存
                }
            }
        }

        return classes;
    }

11.1.1.1this.loadExtensionClasses()

   private Map<String, Class<?>> loadExtensionClasses() {
        SPI defaultAnnotation = (SPI) this.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 " + this.type.getName() + ": " + Arrays.toString(names));
                }

                if (names.length == 1) {
                    this.cachedDefaultName = names[0];
                }
            }
        }

        Map<String, Class<?>> extensionClasses = new HashMap();
        //dubboSPI类信息存放目录 逐个加载一遍
        this.loadFile(extensionClasses, "META-INF/dubbo/internal/");
        this.loadFile(extensionClasses, "META-INF/dubbo/");
        this.loadFile(extensionClasses, "META-INF/services/");
        return extensionClasses;
    }

11.1.1.1.1 loadFile

代码太长 截取部分关键代码

 //判断实现类是否打上了Adaptive注解
    if (clazz.isAnnotationPresent(Adaptive.class)) {
        if (this.cachedAdaptiveClass == null) {
            //如果打上了存入成员变量[10]
            this.cachedAdaptiveClass = clazz;
        } else if (!this.cachedAdaptiveClass.equals(clazz)) {
            throw new IllegalStateException("More than 1 adaptive class found: " + this.cachedAdaptiveClass.getClass().getName() + ", " + clazz.getClass().getName());
        }
    } else {
        try {
            //这里是判断实现类是否有包含当前类型的构造函数。
            clazz.getConstructor(this.type);
            Set<Class<?>> wrappers = this.cachedWrapperClasses;
            if (wrappers == null) {
                this.cachedWrapperClasses = new ConcurrentHashSet();
                wrappers = this.cachedWrapperClasses;
            }
            //如果有存入代理类集合 
            wrappers.add(clazz);
        } catch (NoSuchMethodException var27) {

遍历扫描spi文件扫描类信息  我们来看一下spi文件到底是什么

是不是跟javaspi一样 只是dubbo的多了一个key=类名  可以存到成员变量[9]的key就是他。根据key可以快速找到对应的实现类

11.2 我们回到11.1那里继续往下看

return this.cachedAdaptiveClass != null ? this.cachedAdaptiveClass : (this.cachedAdaptiveClass = this.createAdaptiveExtensionClass())

这里会有一个问题。留意 11.1.1.1.1 如果当打上了 注解之后cachedAdaptiveClass 这个就不会为空 直接返回到10.injectExtension 方法创建对应的实例

11.3 createAdaptiveExtensionClass()

调用这个方法 是spi接口没有实现类打上Adaptive接口才会走

  private Class<?> createAdaptiveExtensionClass() {
        String code = this.createAdaptiveExtensionClassCode();
        ClassLoader classLoader = findClassLoader();
        Compiler compiler = (Compiler)getExtensionLoader(Compiler.class).getAdaptiveExtension();
        return compiler.compile(code, classLoader);
    }

这个方法就是通过javassist动态生成适配器类的class 我找了几个动态生成的代码一看就懂啦  现在我吧code变量的值复制几个出来

首先Protocol 的实现类都没有打上Adaptive 所以会动态生成适配器类

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);
    }
}
ProxyFactory 接口的适配器类
package com.alibaba.dubbo.rpc;

import com.alibaba.dubbo.common.extension.ExtensionLoader;

public class ProxyFactory$Adpative implements com.alibaba.dubbo.rpc.ProxyFactory {
    public java.lang.Object getProxy(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.getParameter("proxy", "javassist");
        if (extName == null)
            throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url(" + url.toString() + ") use keys([proxy])");
        com.alibaba.dubbo.rpc.ProxyFactory extension = (com.alibaba.dubbo.rpc.ProxyFactory) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName);
        return extension.getProxy(arg0);
    }

    public com.alibaba.dubbo.rpc.Invoker getInvoker(java.lang.Object arg0, java.lang.Class arg1, com.alibaba.dubbo.common.URL arg2) throws java.lang.Object {
        if (arg2 == null) throw new IllegalArgumentException("url == null");
        com.alibaba.dubbo.common.URL url = arg2;
        String extName = url.getParameter("proxy", "javassist");
        if (extName == null)
            throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url(" + url.toString() + ") use keys([proxy])");
        com.alibaba.dubbo.rpc.ProxyFactory extension = (com.alibaba.dubbo.rpc.ProxyFactory) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName);
        return extension.getInvoker(arg0, arg1, arg2);
    }
}

就先复制2个 。注意看标红部分。dubbo动态生成的适配器类是根据url里面的参数来适配对应的处理器 key则是前面2个注解里面的value

getExtension是传入对应的key。直接去成员变量【10】根据key去取
12。回到10 injectExtension方法。根据适配器class反射创建对象后内部做了什么
 private T injectExtension (T instance){
            try {
                if (this.objectFactory != null) {
                    Method[] arr$ = instance.getClass().getMethods();
                    int len$ = arr$.length;

                    for (int i$ = 0; i$ < len$; ++i$) {
                        Method method = arr$[i$];
                        //获得对象的set方法
                        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) : "";
                               //记得这个工厂吗。回到第4步。这里相当于ioc 去spi扫描和根据spring容器找相应的key的对象 注入
                                Object object = this.objectFactory.getExtension(pt, property);
                                if (object != null) {
                                    method.invoke(instance, object);
                                }
                            } catch (Exception var9) {
                                logger.error("fail to inject via method " + method.getName() + " of interface " + this.type.getName() + ": " + var9.getMessage(), var9);
                            }
                        }
                    }
                }
            } catch (Exception var10) {
                logger.error(var10.getMessage(), var10);
            }

            return instance;
        }
回到11.3 对应接口没有打上Adaptive注解 最终都是通过javassist生成的动态适配器类。这个类里面都是根据url的对应参数key调用.getExtension(extName)
这里面的代码
try {
                T instance = EXTENSION_INSTANCES.get(clazz);//根据key获得缓存实例。静态成员存
                if (instance == null) {
                    EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance());
                    instance = EXTENSION_INSTANCES.get(clazz);
                }

                this.injectExtension(instance);//ioc注入
                Set<Class<?>> wrapperClasses = this.cachedWrapperClasses;
                Class wrapperClass;
                //这个wapperClass成员变量[14]  和参考11.1.1.1.1 loadFile
                if (wrapperClasses != null && wrapperClasses.size() > 0) {
                    //这里是装饰者模式 层层代理 最终返回我们的waper代理
                    for (Iterator i$ = wrapperClasses.iterator(); i$.hasNext(); instance = this.injectExtension(wrapperClass.getConstructor(this.type).newInstance(instance))) {
                        wrapperClass = (Class) i$.next();
                    }
                }

                return instance;

 好累啊!

总结一下怎么利用这些扩展

1.创建spi文件 和实现类。然后将我们的实现类添加一个当前扩展接口的构造函数 就能够实现对应扩展的aop

2.创建spi文件和实现类。 对应的我们自定义key 然后在dubbo对应配置 配置我们的key(对了  dubbox的rest协议 可能是根据这个扩展点扩展的哦)

转载于:https://www.cnblogs.com/LQBlog/p/9402694.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值