Dubbo—SPI及自适应扩展原理( 二 )

看到这么多扩展类(每一个配置文件中都有很多) , 我们首先应该思考一个问题:Dubbo一启动 , 就加载所有的扩展类么?作为一个优秀的RPC框架 , 肯定不会耗时耗力做这样的无用功 , 所以肯定会通过一种方式拿到指定的扩展才对 。 我们可以看到大多是以键值对方式(表示为extName-value)配置的扩展 , 那么不难猜测 , 这里的extName就是用来实现上面所说的功能的 。 那到底是不是呢?以上纯属猜测 , 下面就到源码中去验证 。
SPI源码Dubbo中实现SPI的核心类是ExtensionLoader , 该类并未提供公共的构造方法来初始化 , 而是通过getExtensionLoader方法获取一个loader对象:
// loader缓存private static final ConcurrentMap, ExtensionLoader> EXTENSION_LOADERS = new ConcurrentHashMap, ExtensionLoader>();public staticExtensionLoader getExtensionLoader(Class 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 loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);if (loader == null) {EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);}return loader;}private final ExtensionFactory objectFactory;private ExtensionLoader(Class type) {this.type = type;objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());}这里的class参数就是扩展点的接口类型 , 每一个loader都需要绑定一个扩展点类型 。 然后首先从缓存中获取loader , 未获取到就初始化一个loader并放入缓存 。 而在私有构造器初始化的时候我们需要注意objectFactory这个变量 , 先大概有个映像 , 后面会用到 。 拿到loader之后 , 就可以调用getExtension方法去获取指定的扩展点了,该方法传入了一个name参数 , 不难猜测这个就是配置文件中的键 , 可以debugger验证一下:
private final ConcurrentMap> cachedInstances = new ConcurrentHashMap>();public T getExtension(String name) { if (name == null || name.length() == 0)throw new IllegalArgumentException("Extension name == null"); if ("true".equals(name)) {return getDefaultExtension(); } // 从缓存中获取Holder对象 , 该对象的值就是扩展对象 Holder holder = cachedInstances.get(name); if (holder == null) {cachedInstances.putIfAbsent(name, new Holder());holder = cachedInstances.get(name); } // 缓存中没有该扩展 , 说明还没有加载扩展类 , 就去配置文件中加载并创建对应的扩展对象 // 这里通过双重校验锁的方式保证线程安全 , Dubbo中大量运用了该技巧 Object instance = holder.get(); if (instance == null) {synchronized (holder) {instance = holder.get();if (instance == null) {instance = createExtension(name);holder.set(instance);}} } return (T) instance;}同样的也是先从缓存拿 , 缓存没有就创建并添加到缓存 , 因此主要看createExtension方法:
// 扩展类实例缓存对象private static final ConcurrentMap, Object> EXTENSION_INSTANCES = new ConcurrentHashMap


推荐阅读