文章

拓展机制SPI

拓展机制SPI

1. 概述

艿艿的友情提示:

这是一篇相对长的文章。

胖友可以带着这样的思维来理解 Dubbo SPI ,它提供了 Spring IOC、AOP 的功能。

本文主要分享 Dubbo 的拓展机制 SPI

想要理解 Dubbo ,理解 Dubbo SPI 是非常必须的。在 Dubbo 中,提供了大量的拓展点,基于 Dubbo SPI 机制加载。如下图所示:

Dubbo拓展点

Dubbo 拓展点

2. 改进

在看具体的 Dubbo SPI 实现之前,我们先理解 Dubbo SPI 产生的背景:

FROM 《Dubbo 开发指南 —— 拓展点加载》

Dubbo 的扩展点加载从 JDK 标准的 SPI (Service Provider Interface) 扩展点发现机制加强而来。

Dubbo 改进了 JDK 标准的 SPI 的以下问题:

  1. JDK 标准的 SPI 会一次性实例化扩展点所有实现,如果有扩展实现初始化很耗时,但如果没用上也加载,会很浪费资源。
  2. 如果扩展点加载失败,连扩展点的名称都拿不到了。比如:JDK 标准的 ScriptEngine,通过 getName() 获取脚本类型的名称,但如果 RubyScriptEngine 因为所依赖的 jruby.jar 不存在,导致 RubyScriptEngine 类加载失败,这个失败原因被吃掉了,和 ruby 对应不起来,当用户执行 ruby 脚本时,会报不支持 ruby,而不是真正失败的原因。
  3. 增加了对扩展点 IoC 和 AOP 的支持,一个扩展点可以直接 setter 注入其它扩展点。
    • Dubbo 自己实现了一套 SPI 机制,而不是使用 Java 标准的 SPI 。
    • 第一点问题,Dubbo 有很多的拓展点,例如 Protocol、Filter 等等。并且每个拓展点有多种的实现,例如 Protocol 有 DubboProtocol、InjvmProtocol、RestProtocol 等等。那么使用 JDK SPI 机制,会初始化无用的拓展点及其实现,造成不必要的耗时与资源浪费。
    • 如果无法理解的胖友,跟着 《Java SPI(Service Provider Interface)简介》写多个拓展实现 文章, ,就很容易理解了。 这就是概念呀。 - 第二点问题,【TODO 8009】ScriptEngine 没看明白,不影响本文理解。 - 第三点问题,严格来说,这不算问题,而是增加了功能特性 ,在下文我们会看到。

3. 代码结构

Dubbo SPI 在 dubbo-common 的 extension 包实现,如下图所示:

代码结构

代码结构

4. ExtensionLoader

com.alibaba.dubbo.common.extension.ExtensionLoader ,拓展加载器。这是 Dubbo SPI 的核心

4.1 属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
private static final String SERVICES_DIRECTORY = "META-INF/services/";

private static final String DUBBO_DIRECTORY = "META-INF/dubbo/";

private static final String DUBBO_INTERNAL_DIRECTORY = DUBBO_DIRECTORY + "internal/";

private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*");

// ============================== 静态属性 ==============================

/**
 * 拓展加载器集合
 *
 * key:拓展接口
 */
private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap<Class<?>, ExtensionLoader<?>>();
/**
 * 拓展实现类集合
 *
 * key:拓展实现类
 * value:拓展对象。
 *
 * 例如,key 为 Class<AccessLogFilter>
 *  value 为 AccessLogFilter 对象
 */
private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<Class<?>, Object>();

// ============================== 对象属性 ==============================

/**
 * 拓展接口。
 * 例如,Protocol
 */
private final Class<?> type;
/**
 * 对象工厂
 *
 * 用于调用 {@link #injectExtension(Object)} 方法,向拓展对象注入依赖属性。
 *
 * 例如,StubProxyFactoryWrapper 中有 `Protocol protocol` 属性。
 */
private final ExtensionFactory objectFactory;
/**
 * 缓存的拓展名与拓展类的映射。
 *
 * 和 {@link #cachedClasses} 的 KV 对调。
 *
 * 通过 {@link #loadExtensionClasses} 加载
 */
private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap<Class<?>, String>();
/**
 * 缓存的拓展实现类集合。
 *
 * 不包含如下两种类型:
 *  1. 自适应拓展实现类。例如 AdaptiveExtensionFactory
 *  2. 带唯一参数为拓展接口的构造方法的实现类,或者说拓展 Wrapper 实现类。例如,ProtocolFilterWrapper 。
 *   拓展 Wrapper 实现类,会添加到 {@link #cachedWrapperClasses} 中
 *
 * 通过 {@link #loadExtensionClasses} 加载
 */
private final Holder<Map<String, Class<?>>> cachedClasses = new Holder<Map<String, Class<?>>>();

/**
 * 拓展名与 @Activate 的映射
 *
 * 例如,AccessLogFilter。
 *
 * 用于 {@link #getActivateExtension(URL, String)}
 */
private final Map<String, Activate> cachedActivates = new ConcurrentHashMap<String, Activate>();
/**
 * 缓存的拓展对象集合
 *
 * key:拓展名
 * value:拓展对象
 *
 * 例如,Protocol 拓展
 *      key:dubbo value:DubboProtocol
 *      key:injvm value:InjvmProtocol
 *
 * 通过 {@link #loadExtensionClasses} 加载
 */
private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<String, Holder<Object>>();
/**
 * 缓存的自适应( Adaptive )拓展对象
 */
private final Holder<Object> cachedAdaptiveInstance = new Holder<Object>();
/**
 * 缓存的自适应拓展对象的类
 *
 * {@link #getAdaptiveExtensionClass()}
 */
private volatile Class<?> cachedAdaptiveClass = null;
/**
 * 缓存的默认拓展名
 *
 * 通过 {@link SPI} 注解获得
 */
private String cachedDefaultName;
/**
 * 创建 {@link #cachedAdaptiveInstance} 时发生的异常。
 *
 * 发生异常后,不再创建,参见 {@link #createAdaptiveExtension()}
 */
private volatile Throwable createAdaptiveInstanceError;

/**
 * 拓展 Wrapper 实现类集合
 *
 * 带唯一参数为拓展接口的构造方法的实现类
 *
 * 通过 {@link #loadExtensionClasses} 加载
 */
private Set<Class<?>> cachedWrapperClasses;

/**
 * 拓展名 与 加载对应拓展类发生的异常 的 映射
 *
 * key:拓展名
 * value:异常
 *
 * 在 {@link #loadFile(Map, String)} 时,记录
 */
private Map<String, IllegalStateException> exceptions = new ConcurrentHashMap<String, IllegalStateException>();
  • 第 1 至 5 行:在 接口全限定名每行拓展名=拓展实现类全限定名 META-INF/dubbo/internal/ 和 META-INF/dubbo/ 目录下,放置 配置文件, 内容为: 。
    • META-INF/dubbo/internal/内部 目录下,从名字上可以看出,用于 Dubbo 提供的拓展实现。下图是一个例子: META-INF/dubbo/internal/ 例子 META-INF/dubbo/internal/ 例子
    • META-INF/dubbo/自定义 目录下,用于用户 的拓展实现。
    • META-INF/service/「4.2 加载拓展配置」兼容 目录下,Java SPI 的配置目录。在 中,我们会看到 Dubbo SPI 对 Java SPI 做了 。
  • 第 7 行:逗号 NAME_SEPARATOR ,拓展名分隔符,使用 。
  • 第 9 至 124 行 ,我们将属性分成了两类:1)静态属性;2)对象属性。这是为啥呢?
    • 【静态属性】一方面,ExtensionLoader 是 ExtensionLoader 的管理容器分别 。一个拓展( 拓展接口 )对应一个 ExtensionLoader 对象。例如,Protocol 和 Filter 对应一个 ExtensionLoader 对象。
    • 【对象属性】另一方面,一个拓展通过其 ExtensionLoader 对象,加载它的拓展实现们cached缓存真正缓存 。我们会发现多个属性都是 “ “ 开头。ExtensionLoader 考虑到性能和资源的优化,读取拓展配置后,会首先进行 。等到 Dubbo 代码 用到对应的拓展实现时,进行拓展实现的对象的初始化。并且,初始化完成后,也会进行 。也就是说:
      • 缓存加载的拓展配置
      • 缓存创建的拓展实现对象
  • 胖友先看下属性的代码注释,有一个整体的印象。下面我们在读实现代码时,会进一步解析说明。

考虑到胖友能更好的理解下面的代码实现,推荐先阅读下 《Dubbo 开发指南 —— 扩展点加载》 文档,建立下对 ExtensionLoader 特点的初步理解:

4.2 获得拓展配置

4.2.1 getExtensionClasses

#getExtensionClasses() 方法,获得拓展实现类数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private final Holder<Map<String, Class<?>>> cachedClasses = new Holder<Map<String, Class<?>>>();

private volatile Class<?> cachedAdaptiveClass = null;

private Set<Class<?>> cachedWrapperClasses;

/**
 * 获得拓展实现类数组
 *
 * @return 拓展实现类数组
 */
private Map<String, Class<?>> getExtensionClasses() {
    // 从缓存中,获得拓展实现类数组
    Map<String, Class<?>> classes = cachedClasses.get();
    if (classes == null) {
        synchronized (cachedClasses) {
            classes = cachedClasses.get();
            if (classes == null) {
                // 从配置文件中,加载拓展实现类数组
                classes = loadExtensionClasses();
                // 设置到缓存中
                cachedClasses.set(classes);
            }
        }
    }
    return classes;
}
  • cachedClasses 属性,缓存的拓展实现类集合。它不包含如下两种类型的拓展实现:
    • 自适应 拓展实现类。例如 AdaptiveExtensionFactory 。
      • 拓展 Adaptive 实现类,会添加到 cachedAdaptiveClass 属性中。
    • 唯一参数为拓展接口 的构造方法的实现类,或者说拓展 Wrapper 实现类。例如,ProtocolFilterWrapper 。
      • 拓展 Wrapper 实现类,会添加到 cachedWrapperClasses 属性中。
    • 总结来说,完整 cachedClasses + cachedAdaptiveClass + cachedWrapperClasses 才是 缓存的拓展实现类的配置。
  • 第 7 至 11 行:从缓存中,获得拓展实现类数组。
  • 第 12 至 14 行:当缓存不存在时,调用 #loadExtensionClasses() 方法,从配置文件中,加载拓展实现类数组。
  • 第 16 行:设置加载的实现类数组,到缓存中。

4.2.2 loadExtensionClasses

#loadExtensionClasses() 方法,从多个配置文件中,加载拓展实现类数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 加载拓展实现类数组
*
* 无需声明 synchronized ,因为唯一调用该方法的
{
    @link #getExtensionClasses();
}
已经声明。
* // synchronized in getExtensionClasses
*
* @return 拓展实现类数组
*/
private Map<String, Class<?>> loadExtensionClasses() {
    // 通过 @SPI 注解,获得默认的拓展实现类名
    final SPI defaultAnnotation = type.getAnnotation(SPI.class);
    if (defaultAnnotation != null) {
        String value = defaultAnnotation.value();
        if ((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;
}
  • 第 10 至 22 行:通过 默认的「5. @SPI」 @SPI 注解,获得拓展接口对应的 拓展实现类名。在 详细解析。
  • 第 25 至 29 行:调用 注意 #loadFile(extensionClasses, dir) 方法,从配置文件中,加载拓展实现类数组。 ,此处配置文件的加载顺序。

4.2.3 loadFile

#loadFile(extensionClasses, dir) 方法,从一个配置文件中,加载拓展实现类数组。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 缓存的自适应拓展对象的类
*
* {
    @link #getAdaptiveExtensionClass();
}
*/
private volatile Class<?> cachedAdaptiveClass = null;

/**
* 拓展 Wrapper 实现类集合
*
* 带唯一参数为拓展接口的构造方法的实现类
*
* 通过 {@link #loadExtensionClasses} 加载
*/
private Set<Class<?>> cachedWrapperClasses;

/**
* 拓展名与 @Activate 的映射
*
* 例如,AccessLogFilter。
*
* 用于 {
    @link #getActivateExtension(URL, String);
}
*/
private final Map<String, Activate> cachedActivates = new ConcurrentHashMap<String, Activate>();

/**
* 缓存的拓展名与拓展类的映射。
*
* 和 {@link #cachedClasses} 的 KV 对调。
*
* 通过 {@link #loadExtensionClasses} 加载
*/
private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap<Class<?>, String>();

/**
* 拓展名 与 加载对应拓展类发生的异常 的 映射
*
* key:拓展名
* value:异常
*
* 在 {@link #loadFile(Map, String)} 时,记录
*/
private Map<String, IllegalStateException> exceptions = new ConcurrentHashMap<String, IllegalStateException>();

/**

  • 从一个配置文件中,加载拓展实现类数组。 *
  • @param extensionClasses 拓展类名数组
  • @param dir 文件名 */ private void loadFile(Map<String, Class<?» extensionClasses, String dir) { // 完整的文件名 String fileName = dir + type.getName(); try { Enumeration 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; while ((line = reader.readLine()) != null) { // 跳过当前被注释掉的情况,例如 #spring=xxxxxxxxx final int ci = line.indexOf('#'); if (ci >= 0) { line = line.substring(0, ci); } line = line.trim(); if (line.length() > 0) { try { // 拆分,key=value 的配置格式 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."); } // 缓存自适应拓展对象的类到 `cachedAdaptiveClass` 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 { // 缓存拓展 Wrapper 实现类到 `cachedWrapperClasses` try { clazz.getConstructor(type); Set<Class<?>> wrappers = cachedWrapperClasses; if (wrappers == null) { cachedWrapperClasses = new ConcurrentHashSet<Class<?>>(); wrappers = cachedWrapperClasses; } wrappers.add(clazz); // 缓存拓展实现类到 `extensionClasses` } catch (NoSuchMethodException e) { clazz.getConstructor(); // 未配置拓展名,自动生成。例如,DemoFilter 为 demo 。主要用于兼容 Java SPI 的配置。 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 到 `cachedActivates` 。 Activate activate = clazz.getAnnotation(Activate.class); if (activate != null) { cachedActivates.put(names[0], activate); } for (String n : names) { // 缓存到 `cachedNames` if (!cachedNames.containsKey(clazz)) { cachedNames.put(clazz, n); } // 缓存拓展实现类到 `extensionClasses` 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); } } ```

  • 第 9 行:获得完整的文件名( 相对路径 )。例如: “META-INF/dubbo/internal/com.alibaba.dubbo.common.extension.ExtensionFactory” 。
  • 第 12 至 18 行:获得文件名对应的所有文件 URL 数组。例如: ExtensionFactory的配置文件 ExtensionFactory 的配置文件
  • 第 21 至 24 行:逐个文件 URL 遍历。
  • 第 27 行:逐 遍历。
  • 第 29 至 32 行:跳过当前被 “#” 注释掉的情况,例如 #spring=xxxxxxxxx 。
  • 第 34 至 40 行:按照 注意 key=value 的配置拆分。其中 name 为拓展名, line 为拓展实现类名。 ,上文我们提到过 Dubbo SPI 会兼容 Java SPI 的配置格式,那么按照此处的解析方式, name 会为空。这种情况下,拓展名会自动生成,详细见第 71 至 82 行的代码。
  • 第 42 至 48 行:判断拓展实现类,需要实现拓展接口。
  • 第 50 至 57 行:缓存自适应拓展对象的类到 「6. @Adaptive」 cachedAdaptiveClass 属性。在 详细解析。
  • 第 59 至 67 行:缓存拓展 Wrapper 实现类到 cachedWrapperClasses 属性。
    • 第 61 行:调用 反射拓展 Wrapper 实现类ProtocolFilterWrapper(Protocol protocol) Class#getConstructor(Class<?>… parameterTypes) 方法,通过 的方式,参数为拓展接口,判断当前配置的拓展实现类为 。若成功(未抛出异常),则代表符合条件。例如, 这个构造方法。
  • 第 69 至 105 行:若获得构造方法失败,则代表是普通的拓展实现类,缓存到 变量 extensionClasses 中。
    • 第 70 行:调用 Class#getConstructor(Class<?>… parameterTypes) 方法,获得参数为空的构造方法。
    • 第 72 至 82 行:未配置拓展名,自动生成。适用于 Java SPI 的配置方式 。例如,xxx.yyy.DemoFilter 生成的拓展名为 demo 。
      • 第 73 行:通过 废弃 @Extension 注解的方式设置拓展名的方式已经 ,胖友可以无视该方法。
  • 第 84 行:获得拓展名。使用逗号进行分割,即多个拓展名可以对应同一个拓展实现类。
  • 第 86 至 90 行:缓存 「7. @Activate」 @Activate 到 cachedActivates 。在 详细解析。
  • 第 93 至 95 行:缓存到 cachedNames 属性。
  • 第 96 至 102 行:缓存拓展实现类到 注意 extensionClasses 变量。 ,相同拓展名,不能对应多个不同的拓展实现。
  • 第 108 至 112 行:若发生异常,记录到异常集合 exceptions 属性。

4.2.4 其他方法

如下方法,和该流程无关,胖友可自行查看。

4.3 获得拓展加载器

在 Dubbo 的代码里,常常能看到如下的代码:

1
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(name)

4.3.1 getExtensionLoader

#getExtensionLoader(type)静态方法,根据拓展点的接口,获得拓展加载器。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 拓展加载器集合
*
* key:拓展接口
*/
// 【静态属性】
private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS =
new ConcurrentHashMap<Class<?>, ExtensionLoader<?>>();

/**
* 根据拓展点的接口,获得拓展加载器
*
* @param type 接口
* @param <T> 泛型
* @return 加载器
*/
@SuppressWarnings("unchecked")
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!");
    }
    // 必须包含 @SPI 注解
    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);
    }
}

  • 第 12 至 15 行:必须是接口。
  • 第 16 至 20 行:调用 #withExtensionAnnotation() 方法,校验必须使用 @SPI 注解标记。
  • 第 22 至 27 行:从 静态 EXTENSION_LOADERS 中获取拓展接口对应的 ExtensionLoader 对象。若不存在,则创建 ExtensionLoader 对象,并添加到 EXTENSION_LOADERS 。

4.3.2 构造方法

构造方法,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 拓展接口。
* 例如,Protocol
*/
private final Class<?> type;

/**
* 对象工厂
*
* 用于调用
{
    @link #injectExtension(Object);
}
方法,向拓展对象注入依赖属性。
*
* 例如,StubProxyFactoryWrapper 中有 `Protocol protocol` 属性。
*/
private final ExtensionFactory objectFactory;

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

  • objectFactory功能上和 Spring IOC 一致 属性,对象工厂, 。
    • 用于调用 #injectExtension(instance)CacheFilter.cacheFactory 方法时,向创建的拓展注入其依赖的属性。例如, 属性。
    • 第 3 行:当拓展接口非 ExtensionFactory 时( 如果不加这个判断,会是一个死循环 ),调用 自适应为什么呢「8. ExtensionFactory」 ExtensionLoader#getAdaptiveExtension() 方法,获得 ExtensionFactory 拓展接口的 拓展实现对象。 ?在 详细解析。

4.4 获得指定拓展对象

在 Dubbo 的代码里,常常能看到如下的代码:

1
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(name)

4.4.1 getExtension

#getExtension() 方法,返回指定名字的扩展对象。如果指定名字的扩展不存在,则抛异常 IllegalStateException 。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* 缓存的拓展对象集合
*
* key:拓展名
* value:拓展对象
*
* 例如,Protocol 拓展
*      key:dubbo value:DubboProtocol
*      key:injvm value:InjvmProtocol
*
* 通过 {@link #loadExtensionClasses} 加载
*/
private final ConcurrentMap<String, Holder<Object>> cachedInstances =
new ConcurrentHashMap<String, Holder<Object>>();

/**
* Find the extension with the given name. If the specified name is not found,
* then
{
    @link IllegalStateException;
}
will be thrown.
*/
/**
* 返回指定名字的扩展对象。如果指定名字的扩展不存在,则抛异常
{
    @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;
}

  • 第 15 至 18 行:调用 默认的 #getDefaultExtension() 方法,查询 拓展对象。在该方法的实现代码中,简化代码为 getExtension(cachedDefaultName); 。
  • 第 19 至 28 行:从缓存中,获得拓展对象。
  • 第 29 至 31 行:当缓存不存在时,调用 #createExtension(name) 方法,创建拓展对象。
  • 第 33 行:添加创建的拓展对象,到缓存中。

4.4.2 createExtension

#createExtension(name) 方法,创建拓展名的拓展对象,并缓存。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 拓展实现类集合
*
* key:拓展实现类
* value:拓展对象。
*
* 例如,key 为 Class<AccessLogFilter>
*      value 为 AccessLogFilter 对象
*/
private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES =
new ConcurrentHashMap<Class<?>, Object>();

/**
* 创建拓展名的拓展对象,并缓存。
*
* @param name 拓展名
* @return 拓展对象
*/
@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, clazz.newInstance());
            instance = (T) EXTENSION_INSTANCES.get(clazz);
        }
        // 注入依赖的属性
        injectExtension(instance);
        // 创建 Wrapper 拓展对象
        Set<Class<?>> wrapperClasses = cachedWrapperClasses;
        if (wrapperClasses != null && !wrapperClasses.isEmpty()) {
            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);
    }
}

  • 第 9 至 13 行:获得拓展名对应的拓展实现类。若不存在,调用 #findException(name) 方法,抛出异常。
  • 第 16 行:从缓存 静态 EXTENSION_INSTANCES 属性中,获得拓展对象。
  • 第 17 至 21 行:当缓存不存在时,创建拓展对象,并添加到 EXTENSION_INSTANCES 中。因为 #getExtension(name) 方法中已经加 synchronized 修饰,所以此处不用同步。
  • 第 23 行:调用 #injectExtension(instance) 方法,向创建的拓展注入其依赖的属性。
  • 第 24 至 30 行:创建 Wrapper 拓展对象,将 包装在其中《Dubbo 开发指南 —— 扩展点加载》 instance 。在 文章中,如此介绍 Wrapper 类: Wrapper 类同样实现了扩展点接口,但是 Wrapper 不是扩展点的真正实现。它的用途主要是用于从 ExtensionLoader 返回扩展点时,包装在真正的扩展点实现外。即从 ExtensionLoader 中返回的实际上是 Wrapper 类的实例,Wrapper 持有了实际的扩展点实现类。 扩展点的 Wrapper 类可以有多个,也可以根据需要新增。 通过 Wrapper 类可以把所有扩展点公共逻辑移至 Wrapper 中。新加的 Wrapper 在所有的扩展点上添加了逻辑,有些类似 AOP,即 Wrapper 代理了扩展点。

4.4.3 injectExtension

#injectExtension(instance) 方法,注入依赖的属性。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 注入依赖的属性
*
* @param instance 拓展对象
* @return 拓展对象
*/
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())) { // setting && public 方法
                // 获得属性的类型
                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;
}

  • 第 9 行:必须有 objectFactory 属性,即 ExtensionFactory 的拓展对象,不需要注入依赖的属性。
  • 第 10 至 13 行:反射获得所有的方法,仅仅处理 public setting 方法。
  • 第 15 行:获得属性的类型。
  • 第 18 行:获得属性名。
  • 第 20 行:获得属性值注意「8. ExtensionFactory」 。 ,此处虽然调用的是 ExtensionFactory#getExtension(type, name) 方法,实际获取的不仅仅是拓展对象,也可以是 Spring Bean 对象。答案在 揭晓。
  • 第 21 至 24 行:设置属性值。

4.4.4 其他方法

如下方法,和该流程无关,胖友可自行查看。

4.5 获得自适应的拓展对象

在 Dubbo 的代码里,常常能看到如下的代码:

1
ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension()

友情提示,胖友先看下 「6. Adaptive」 的内容,在回到此处。

4.5.1 getAdaptiveExtension

#getAdaptiveExtension() 方法,获得自适应拓展对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 缓存的自适应( Adaptive )拓展对象
*/
private final Holder<Object> cachedAdaptiveInstance = new Holder<Object>();

/**
* 创建 {@link #cachedAdaptiveInstance} 时发生的异常。
*
* 发生异常后,不再创建,参见 {
    @link #createAdaptiveExtension();
}
*/
private volatile Throwable createAdaptiveInstanceError;

/**
* 获得自适应拓展对象
*
* @return 拓展对象
*/
@SuppressWarnings("unchecked")
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);
                    }
                }
            }
            // 若之前创建报错,则抛出异常 IllegalStateException
        } else {
            throw new IllegalStateException("fail to create adaptive instance: "
            + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
        }
    }
    return (T) instance;
}

  • 第 9 行:从缓存 cachedAdaptiveInstance 属性中,获得自适应拓展对象。
  • 第 28 至 30 行:若之前创建报错,则抛出异常 IllegalStateException 。
  • 第 14 至 20 行:当缓存不存在时,调用 #createAdaptiveExtension() 方法,创建自适应拓展对象,并添加到 cachedAdaptiveInstance 中。
  • 第 22 至 24 行:若创建发生异常,记录异常到 createAdaptiveInstanceError ,并抛出异常 IllegalStateException 。

4.5.2 createAdaptiveExtension

#createAdaptiveExtension() 方法,创建自适应拓展对象。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 创建自适应拓展对象
*
* @return 拓展对象
*/
@SuppressWarnings("unchecked")
private T createAdaptiveExtension() {
    try {
        return injectExtension((T) getAdaptiveExtensionClass().newInstance());
    } catch (Exception e) {
        throw new IllegalStateException("Can not create adaptive extension "
        + type + ", cause: " + e.getMessage(), e);
    }
}

  • 调用 #getAdaptiveExtensionClass() 方法,获得自适应拓展类。
  • 调用 Class#newInstance() 方法,创建自适应拓展对象。
  • 调用 #injectExtension(instance) 方法,向创建的自适应拓展对象,注入依赖的属性。

4.5.3 getAdaptiveExtensionClass

#getAdaptiveExtensionClass() 方法,获得自适应拓展类。代码如下:

1
2
3
4
5
6
7
8
9
10
/**
* @return 自适应拓展类
*/
private Class<?> getAdaptiveExtensionClass() {
    getExtensionClasses();
    if (cachedAdaptiveClass != null) {
        return cachedAdaptiveClass;
    }
    return cachedAdaptiveClass = createAdaptiveExtensionClass();
}

  • 【 @Adaptive 的第一种】第 6 至 8 行:若 cachedAdaptiveClass 已存在,直接返回。的第一种情况。
  • 自动生成编译 @Adaptive 的第二种】第 9 行:调用 #createAdaptiveExtensionClass() 方法, 自适应拓展的代码实现,并 后返回该类。

4.5.4 createAdaptiveExtensionClassCode

#createAdaptiveExtensionClassCode() 方法,自动生成自适应拓展的代码实现,并编译后返回该类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 自动生成自适应拓展的代码实现,并编译后返回该类。
*
* @return 类
*/
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);
}

  • 第 8 行:调用 #createAdaptiveExtensionClassCode 方法,自动生成自适应拓展的代码实现的字符串。
    • 代码比较简单,已经添加详细注释,胖友点击查看。
    • 如下是 ProxyFactory 的自适应拓展的代码实现的字符串生成例子 自适应拓展的代码实现的字符串生成例子 自适应拓展的代码实现的字符串生成例子
  • 第 9 至 12 行:使用 Dubbo SPI 加载 Compier 拓展接口对应的拓展实现对象,后调用 Compiler#compile(code, classLoader) 方法,进行编译。 因为不是本文的重点,后续另开文章分享。

4.6 获得激活的拓展对象数组

在 Dubbo 的代码里,看到使用代码如下:

1
List<Filter> filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);

4.6.1 getExtensionLoader

#getExtensionLoader(url, key, group) 方法,获得符合自动激活条件的拓展对象数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* This is equivalent to
{
    @code getActivateExtension(url, url.getParameter(key).split(","), null);
}
*
* 获得符合自动激活条件的拓展对象数组
*
* @param url   url
* @param key   url parameter key which used to get extension point names
*              Dubbo URL 参数名
* @param group group
*              过滤分组名
* @return extension list which are activated.
* @see #getActivateExtension(com.alibaba.dubbo.common.URL, String[], String)
*/
public List<T> getActivateExtension(URL url, String key, String group) {
    // 从 Dubbo URL 获得参数值
    String value = url.getParameter(key);
    // 获得符合自动激活条件的拓展对象数组
    return getActivateExtension(url, value == null || value.length() == 0
    ? null : Constants.COMMA_SPLIT_PATTERN.split(value), group);
}

/**
* Get activate extensions.
*
* 获得符合自动激活条件的拓展对象数组
*
* @param url    url
* @param values extension point names
* @param group  group
* @return extension list which are activated
* @see com.alibaba.dubbo.common.extension.Activate
*/
public List<T> getActivateExtension(URL url, String[] values, String group) {
    List<T> exts = new ArrayList<T>();
    List<String> names = values == null ? new ArrayList<String>(0) : Arrays.asList(values);
    // 处理自动激活的拓展对象们
    // 判断不存在配置 `"-name"` 。例如,<dubbo:service filter="-default" /> ,代表移除所有默认过滤器。
    if (!names.contains(Constants.REMOVE_VALUE_PREFIX + Constants.DEFAULT_KEY)) {
        // 获得拓展实现类数组
        getExtensionClasses();
        // 循环
        for (Map.Entry<String, Activate> entry : cachedActivates.entrySet()) {
            String name = entry.getKey();
            Activate activate = entry.getValue();
            if (isMatchGroup(group, activate.group())) { // 匹配分组
            // 获得拓展对象
            T ext = getExtension(name);
            if (!names.contains(name) // 不包含在自定义配置里。如果包含,会在下面的代码处理。
            && !names.contains(Constants.REMOVE_VALUE_PREFIX + name) // 判断是否配置移除。例如 <dubbo:service filter="-monitor" />,则 MonitorFilter 会被移除
            && isActive(activate, url)) { // 判断是否激活
            exts.add(ext);
        }
    }
}
// 排序
Collections.sort(exts, ActivateComparator.COMPARATOR);
}
// 处理自定义配置的拓展对象们。例如在 <dubbo:service filter="demo" /> ,代表需要加入 DemoFilter (这个是笔者自定义的)。
List<T> usrs = new ArrayList<T>();
for (int i = 0; i < names.size(); i++) {
    String name = names.get(i);
    if (!name.startsWith(Constants.REMOVE_VALUE_PREFIX)
    && !names.contains(Constants.REMOVE_VALUE_PREFIX + name)) { // 判断非移除的
    // 将配置的自定义在自动激活的拓展对象们前面。例如,<dubbo:service filter="demo,default,demo2" /> ,则 DemoFilter 就会放在默认的过滤器前面。
    if (Constants.DEFAULT_KEY.equals(name)) {
        if (!usrs.isEmpty()) {
            exts.addAll(0, usrs);
            usrs.clear();
        }
    } else {
        // 获得拓展对象
        T ext = getExtension(name);
        usrs.add(ext);
    }
}
}
// 添加到结果集
if (!usrs.isEmpty()) {
    exts.addAll(usrs);
}
return exts;
}

  • 第 16 行:从 Dubbo URL 获得参数值。例如说,若 XML 配置 Service 逗号 ,并且在获得 Filter 自动激活拓展时,此处就能解析到 value=demo,demo2 。另外, value 可以根据 拆分。
  • 第 18 行:调用 #getActivateExtension(url, values, group) 方法,获得符合自动激活条件的拓展对象数组。
  • 第 35 至 56 行:处理自动激活的拓展对象们。
  • 第 57 至 74 行:处理自定义配置的拓展对象们。
  • 第 75 至 78 行:将 尾部 usrs 合并到 exts 。
  • 代码比较简单,胖友直接看注释。

4.6.2 ActivateComparator

com.alibaba.dubbo.common.extension.support.ActivateComparator ,自动激活拓展对象排序器。

  • 代码比较简单,胖友直接看注释。

5. @SPI

com.alibaba.dubbo.common.extension.@SPI ,扩展点接口的标识。代码如下:

1
2
3
4
5
6
7
8
9
10
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface SPI {
    /**
    * default extension name
    */
    String value() default "";
}


  • value ,默认拓展实现类的名字。例如,Protocol 拓展接口,代码如下:
1
2
3
4
@SPI("dubbo")
public interface Protocol {
    // ... 省略代码
}

其中 “dubbo” 指的是 DubboProtocol ,Protocol 默认的拓展实现类。

6. @Adaptive

com.alibaba.dubbo.common.extension.@Adaptive ,自适应拓展信息的标记。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target( {
    ElementType.TYPE, ElementType.METHOD;
}
)
public @interface Adaptive {
    /**
    * Decide which target extension to be injected. The name of the target extension is decided
    * by the parameter passed in the URL, and the parameter names are given by this method.
    * <p>
    * If the specified parameters are not found from
    {
        @link URL;
    }
    , then the default extension
    * will be used for dependency injection (specified in its interface's
    {
        @link SPI;
    }
    ).
    * <p>
    * For examples, given <code>String[]
    {
        "key1", "key2";
    }
    </code>:
    * <ol>
    * <li>find parameter 'key1' in URL, use its value as the extension's name</li>
    * <li>try 'key2' for extension's name if 'key1' is not found (or its value is empty) in URL</li>
    * <li>use default extension if 'key2' doesn't appear either</li>
    * <li>otherwise, throw
    {
        @link IllegalStateException;
    }
    </li>
    * </ol>
    * If default extension's name is not give on interface's
    {
        @link SPI;
    }
    , then a name is generated
    * from interface's class name with the rule: divide classname from capital char into several parts,
    * and separate the parts with dot '.', for example: for
    {
        @code com.alibaba.dubbo.xxx.YyyInvokerWrapper;
    }
    ,
    * its default name is <code>String[]
    {
        "yyy.invoker.wrapper";
    }
    </code>. This name will be used to
    * search for parameter from URL.
    *
    * @return parameter key names in URL
    */
    /**
    * 从
    {
        @link URL;
    }
    的 Key 名,对应的 Value 作为要 Adapt 成的 Extension 名。
    * <p>
    * 如果
    {
        @link URL} 这些 Key 都没有 Value ,使用 缺省的扩展(在接口的
        {
            @link SP;
        }
    }
    中设定的值)。
    * 比如,<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接口类名的点分隔小写字串。
    * 即对于Extension接口
    {
        @code com.alibaba.dubbo.xxx.YyyInvokerWrapper;
    }
    的缺省值为
    * <code>String[] {"yyy.invoker.wrapper"}</code>
    *
    * @see SPI#value()
    */
    String[] value() default {}
}

@Adaptive 注解,可添加方法上,分别代表了两种不同的使用方式。

友情提示:一个拓展接口,有且仅有一个 Adaptive 拓展实现类。

  • 第一种,标记在类手动实现「8.1 AdaptiveExtensionFactory」 上,代表 它是一个拓展接口的 Adaptive 拓展实现类。目前 Dubbo 项目里,只有 ExtensionFactory 拓展的实现类 AdaptiveExtensionFactory 有这么用。详细解析见 。
  • 第二种,标记在拓展接口的方法自动生成代码实现 上,代表 该接口的 Adaptive 拓展实现类。
    • value真正的 ,从 Dubbo URL 获取参数中,使用键名( Key ),获取键值。该值为 拓展名。
      • 自适应拓展实现类,会获取拓展名对应的真正 的拓展对象。通过该对象,执行真正的逻辑。
      • 可以设置多个有值默认拓展名 键名( Key ),顺序获取直到 。若最终获取不到,使用 。
    • 「4.5.4 createAdaptiveExtensionClassCode」 详细解析。

7. @Activate

com.alibaba.dubbo.common.extension.@Activate ,自动激活条件的标记。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target( {
    ElementType.TYPE, ElementType.METHOD;
}
)
public @interface Activate {
    /**
    * Activate the current extension when one of the groups matches. The group passed into
    *
    {
        @link ExtensionLoader#getActivateExtension(URL, String, String);
    }
    will be used for matching.
    *
    * @return group names to match
    * @see ExtensionLoader#getActivateExtension(URL, String, String)
    */
    /**
    * Group过滤条件。
    * <br />
    * 包含
    {
        @link ExtensionLoader#getActivateExtension;
    }
    的group参数给的值,则返回扩展。
    * <br />
    * 如没有Group设置,则不过滤。
    */
    String[] group() default {}

    /**
    * Activate the current extension when the specified keys appear in the URL's parameters.
    * <p>
    * For example, given <code>@Activate("cache, validation")</code>, the current extension will
    * be return only when there's either <code>cache</code> or <code>validation</code> key appeared
    * in the URL's parameters.
    * </p>
    *
    * @return URL parameter keys
    * @see ExtensionLoader#getActivateExtension(URL, String)
    * @see ExtensionLoader#getActivateExtension(URL, String, String)
    */
    /**
    * Key过滤条件。包含
    {
        @link ExtensionLoader#getActivateExtension;
    }
    的URL的参数Key中有,则返回扩展。
    * <p/>
    * 示例:
    * 注解的值 <code>@Activate("cache,validatioin")</code>,
    * 则
    {
        @link ExtensionLoader#getActivateExtension;
    }
    的URL的参数有<code>cache</code>Key,或是<code>validatioin</code>则返回扩展。
    * <br/>
    * 如没有设置,则不过滤。
    */
    String[] value() default {}

    /**
    * Relative ordering info, optional
    *
    * @return extension list which should be put before the current one
    */
    /**
    * 排序信息,可以不提供。
    */
    String[] before() default {}

    /**
    * Relative ordering info, optional
    *
    * @return extension list which should be put after the current one
    */
    /**
    * 排序信息,可以不提供。
    */
    String[] after() default {}

    /**
    * Absolute ordering info, optional
    *
    * @return absolute ordering info
    */
    /**
    * 排序信息,可以不提供。
    */
    int order() default 0;
}

8. ExtensionFactory

com.alibaba.dubbo.common.extension.ExtensionFactory ,拓展工厂接口。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* ExtensionFactory
*
* 拓展工厂接口
*/
@SPI
public interface ExtensionFactory {
    /**
    * Get extension.
    *
    * 获得拓展对象
    *
    * @param type object type. 拓展接口
    * @param name object name. 拓展名
    * @return object instance. 拓展对象
    */
    <T> T getExtension(Class<T> type, String name);
}


  • ExtensionFactory 自身也是拓展接口,基于 Dubbo SPI 加载具体拓展实现类。
  • #getExtension(type, name)「4.4.3 injectExtension」注入依赖属性 方法,在 中,获得拓展对象,向创建的拓展对象 。在实际代码中,我们可以看到不仅仅获得的是拓展对象,也可以是 Spring 中的 Bean 对象。
  • ExtensionFactory 子类类图如下: ExtensionFactory类图 ExtensionFactory 类图

8.1 AdaptiveExtensionFactory

com.alibaba.dubbo.common.extension.factory.AdaptiveExtensionFactory ,自适应 ExtensionFactory 拓展实现类。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Adaptive
public class AdaptiveExtensionFactory implements ExtensionFactory {
    /**
    * ExtensionFactory 拓展对象集合
    */
    private final List<ExtensionFactory> factories;

    public AdaptiveExtensionFactory() {
        // 使用 ExtensionLoader 加载拓展对象实现类。
        ExtensionLoader<ExtensionFactory> loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
        List<ExtensionFactory> list = new ArrayList<ExtensionFactory>();
        for (String name : loader.getSupportedExtensions()) {
            list.add(loader.getExtension(name));
        }
        factories = Collections.unmodifiableList(list);
    }

    public <T> T getExtension(Class<T> type, String name) {
        // 遍历工厂数组,直到获得到属性
        for (ExtensionFactory factory : factories) {
            T extension = factory.getExtension(type, name);
            if (extension != null) {
                return extension;
            }
        }
        return null;
    }
}

  • @Adaptive自适应 注解,为 ExtensionFactory 的 拓展实现类。
  • 构造 方法,使用 ExtensionLoader 加载 ExtensionFactory 拓展对象的实现类。若胖友没自己实现 ExtensionFactory 的情况下, factories 为 SpiExtensionFactory 和 SpringExtensionFactory 。
  • #getExtension(type, name) 方法,遍历 factories ,调用其 #getExtension(type, name) 方法,直到获得到属性值。

8.2 SpiExtensionFactory

com.alibaba.dubbo.common.extension.factory.SpiExtensionFactory ,SPI ExtensionFactory 拓展实现类。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class SpiExtensionFactory implements ExtensionFactory {
    /**
    * 获得拓展对象
    *
    * @param type object type. 拓展接口
    * @param name object name. 拓展名
    * @param <T> 泛型
    * @return 拓展对象
    */
    public <T> T getExtension(Class<T> type, String name) {
        if (type.isInterface() && type.isAnnotationPresent(SPI.class)) { // 校验是 @SPI
        // 加载拓展接口对应的 ExtensionLoader 对象
        ExtensionLoader<T> loader = ExtensionLoader.getExtensionLoader(type);
        // 加载拓展对象
        if (!loader.getSupportedExtensions().isEmpty()) {
            return loader.getAdaptiveExtension();
        }
    }
    return null;
}
}

8.3 SpringExtensionFactory

com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory ,Spring ExtensionFactory 拓展实现类。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class SpringExtensionFactory implements ExtensionFactory {
    /**
    * Spring Context 集合
    */
    private static final Set<ApplicationContext> contexts = new ConcurrentHashSet<ApplicationContext>();

    public static void addApplicationContext(ApplicationContext context) {
        contexts.add(context);
    }

    public static void removeApplicationContext(ApplicationContext context) {
        contexts.remove(context);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T getExtension(Class<T> type, String name) {
        for (ApplicationContext context : contexts) {
            if (context.containsBean(name)) {
                // 获得属性
                Object bean = context.getBean(name);
                // 判断类型
                if (type.isInstance(bean)) {
                    return (T) bean;
                }
            }
        }
        return null;
    }
}

  • #getExtension(type, name) 方法,遍历 contexts ,调用其 ApplicationContext#getBean(name) 方法,获得 Bean 对象,直到成功并且值类型正确。

8.3.1 例子

DemoFilter 是笔者实现的 Filter 拓展实现类,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class DemoFilter implements Filter {
    private DemoDAO demoDAO;

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        return invoker.invoke(invocation);
    }

    public DemoFilter setDemoDAO(DemoDAO demoDAO) {
        this.demoDAO = demoDAO;
        return this;
    }
}

  • DemoDAO ,笔者在 Spring 中声明对应的 Bean 对象。
1
<bean id="demoDAO" class="com.alibaba.dubbo.demo.provider.DemoDAO" />

666. 彩蛋

比想象中的长的多的多。初始理解会比较辛苦,梳理干净后实际很简单。

能够耐心到此处的胖友,为你点赞。

本文由作者按照 CC BY 4.0 进行授权