- Dubbo学习之自动配置
- Dubbo学习之PostProcessor
- Dubbo学习之负载均衡策略
本文基于Spring Boot 2.6.6,dubbo-spring-boot-starter 3.0.6环境。
SPI全称为Service Provider Interface,是一种服务发现机制。SPI的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加载实现类。这样可以在运行时,动态为接口替换实现类。正因此特性,我们可以很容易的通过SPI机制为我们的程序提供拓展功能。
SPI机制在第三方框架中也有所应用,比如Dubbo就是通过SPI机制加载所有的组件。不过,Dubbo并未使用Java原生的SPI机制,而是对其进行了增强,使其能够更好的满足需求。
本文将从以下角度介绍SPI机制;
- Java SPI;
- Dubbo SPI;
- Dubbo SPI自适应拓展;
首先定义一个接口demo.spi.Person,代码如下:
public interface Person {
void saySomething();
}
然后定义两个实现类,demo.spi.Student、demo.spi.Teacher,代码如下:
public class Student implements Person {
@Override
public void saySomething() {
System.out.println("I'm a student");
}
}
public class Teacher implements Person {
@Override
public void saySomething() {
System.out.println("I'm a teacher");
}
}
接下来,在项目META-INF/services/目录下创建一个文件,文件名为接口Person的全限定名demo.spi.Person,文件内容为实现类的全限定名,具体如下:
demo.spi.Student demo.spi.Teacher
准备好以上内容后,便可以编写代码进行测试Java SPI机制,测试代码如下:
public class SpiTest {
public static void main(String[] args) {
ServiceLoader serviceLoader = ServiceLoader.load(Person.class);
serviceLoader.forEach(Person::saySomething);
}
}
测试结果如下:
I'm a student I'm a teacherDubbo SPI 简介
Dubbo并未使用Java SPI,而是重新实现了一套功能更强的SPI机制。Dubbo SPI的相关逻辑被封装在了ExtensionLoader类中,通过ExtensionLoader,我们可以加载指定的实现类。与Java SPI实现类配置不同,Dubbo SPI是通过键值对的方式进行配置,这样我们可以按需加载指定的实现类,而且Dubbo SPI中,支持文件路径如下:
- META-INF/dubbo/internal/;
- META-INF/dubbo/;
- META-INF/services/;
优先级越来越高,即后者定义的内容可以覆盖前者的内容;
示例首先定义一个接口demo.dubbo.spi.Person,需要标注@SPI,代码如下:
@SPI
public interface Person {
void saySomething();
}
然后定义两个实现类,demo.dubbo.spi.Student、demo.dubbo.spi.Teacher,代码如下:
public class Student implements Person {
@Override
public void saySomething() {
System.out.println("I'm a student");
}
}
public class Teacher implements Person {
@Override
public void saySomething() {
System.out.println("I'm a teacher");
}
}
接下来,在项目META-INF/dubbo/目录下创建一个文件,文件名为接口Person的全限定名demo.dubbo.spi.Person,文件内容为实现类的全限定名,具体如下:
student=demo.dubbo.spi.Student teacher=demo.dubbo.spi.Teacher
准备好以上内容后,便可以编写代码进行测试Java SPI机制,测试代码如下:
public class SpiTest {
public static void main(String[] args) {
ExtensionLoader extensionLoader =
ApplicationModel.defaultModel().getDefaultModule().getExtensionLoader(Person.class);
Person student = extensionLoader.getExtension("student");
student.saySomething();
Person teacher = extensionLoader.getExtension("teacher");
teacher.saySomething();
}
}
测试结果如下:
I'm a student I'm a teacher简析
首先分析ExtensionLoader的创建,代码如下:
// ExtensionAccessor.java defaultExtensionLoader getExtensionLoader(Class type) { return this.getExtensionDirector().getExtensionLoader(type); } // ExtensionDirector.java public ExtensionLoader getExtensionLoader(Class type) { checkDestroyed(); if (type == null) { throw new IllegalArgumentException("Extension type == null"); } if (!type.isInterface()) { throw new IllegalArgumentException("Extension type (" + type + ") is not an interface!"); } if (!withExtensionAnnotation(type)) { // 如果type上没有SPI注解,则抛出异常 throw new IllegalArgumentException("Extension type (" + type + ") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!"); } // 此处实现了缓存 // 先从本地缓存中查找 ExtensionLoader loader = (ExtensionLoader ) extensionLoadersMap.get(type); ExtensionScope scope = extensionScopeMap.get(type); if (scope == null) { // 缓存中不存在,则需要根据type解析获取 SPI annotation = type.getAnnotation(SPI.class); scope = annotation.scope(); // 缓存,方便下次直接获取 extensionScopeMap.put(type, scope); } if (loader == null && scope == ExtensionScope.SELF) { // 创建实例 loader = createExtensionLoader0(type); } // 不存在,则尝试从父类中查找,若存在父类的话 if (loader == null) { if (this.parent != null) { loader = this.parent.getExtensionLoader(type); } } // 还不存在,则创建实例 if (loader == null) { loader = createExtensionLoader(type); } return loader; } private static boolean withExtensionAnnotation(Class> type) { return type.isAnnotationPresent(SPI.class); } private ExtensionLoader createExtensionLoader(Class type) { ExtensionLoader loader = null; // scope是否匹配 if (isScopeMatched(type)) { // 匹配,则创建ExtensionLoader loader = createExtensionLoader0(type); } else { // 不匹配,则返回null } return loader; } private ExtensionLoader createExtensionLoader0(Class type) { checkDestroyed(); ExtensionLoader loader; // 创建ExtensionLoader实例并放入缓存 extensionLoadersMap.putIfAbsent(type, new ExtensionLoader (type, this, scopeModel)); loader = (ExtensionLoader ) extensionLoadersMap.get(type); return loader; }
继续分析ExtensionLoader的初始化过程,代码如下:
ExtensionLoader(Class> type, ExtensionDirector extensionDirector, ScopeModel scopeModel) {
this.type = type;
this.extensionDirector = extensionDirector;
this.extensionPostProcessors = extensionDirector.getExtensionPostProcessors();
initInstantiationStrategy();
// 用于查找依赖,以便注入到创建的实例
// 此处也使用了Dubbo SPI机制
this.injector = (type == ExtensionInjector.class ? null : extensionDirector.getExtensionLoader(ExtensionInjector.class)
.getAdaptiveExtension());
this.activateComparator = new ActivateComparator(extensionDirector);
this.scopeModel = scopeModel;
}
ExtensionLoader创建好后,便可以根据指定的名称获取对应的实现类了,代码如下:
public T getExtension(String name) {
T extension = getExtension(name, true);
if (extension == null) {
throw new IllegalArgumentException("Not find extension: " + name);
}
return extension;
}
public T getExtension(String name, boolean wrap) {
checkDestroyed();
if (StringUtils.isEmpty(name)) {
throw new IllegalArgumentException("Extension name == null");
}
if ("true".equals(name)) {
// 使用默认实例
return getDefaultExtension();
}
String cacheKey = name;
if (!wrap) {
cacheKey += "_origin";
}
final Holder
createExtension方法主要逻辑如下:
- 通过getExtensionClasses获取所有的拓展类;
- 创建拓展对象;
- 向拓展对象中注入依赖;
- 将拓展对象包裹在相应的Wrapper对象中;
先分析getExtensionClasses方法,代码如下:
private Map> getExtensionClasses() { Map > classes = cachedClasses.get(); if (classes == null) { synchronized (cachedClasses) { classes = cachedClasses.get(); // DCL if (classes == null) { // 家在拓展实现类 classes = loadExtensionClasses(); cachedClasses.set(classes); } } } return classes; } private Map > loadExtensionClasses() { checkDestroyed(); // 缓存默认拓展实现类 cacheDefaultExtensionName(); Map > extensionClasses = new HashMap<>(); // 遍历需要加载的目录 for (LoadingStrategy strategy : strategies) { // 加载该目录下本type的拓展实现类 loadDirectory(extensionClasses, strategy, type.getName()); if (this.type == ExtensionInjector.class) { // 兼容ExtensionFactory loadDirectory(extensionClasses, strategy, ExtensionFactory.class.getName()); } } return extensionClasses; }
分析loadDirectory实现之前,先简单分析下strategies的内容,其赋值语句如下:
private static volatile LoadingStrategy[] strategies = loadLoadingStrategies();
private static LoadingStrategy[] loadLoadingStrategies() {
return stream(load(LoadingStrategy.class).spliterator(), false)
.sorted()
.toArray(LoadingStrategy[]::new);
}
// ServiceLoader.java
public static ServiceLoader load(Class service) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return ServiceLoader.load(service, cl);
}
这里借助Java SPI机制实现多LoadingStrategy,在dubbo-3.0.6.jar的META-INF/services/目录下存在文件org.apache.dubbo.common.extension.LoadingStrategy,内容如下:
org.apache.dubbo.common.extension.DubboInternalLoadingStrategy org.apache.dubbo.common.extension.DubboLoadingStrategy org.apache.dubbo.common.extension.ServicesLoadingStrategy
这三个LoadingStrategy的核心代码如下:
public class DubboInternalLoadingStrategy implements LoadingStrategy {
@Override
public String directory() {
return "META-INF/dubbo/internal/";
}
@Override
public int getPriority() {
return MAX_PRIORITY;
}
}
public class DubboLoadingStrategy implements LoadingStrategy {
@Override
public String directory() {
return "META-INF/dubbo/";
}
@Override
public boolean overridden() {
// 支持覆盖
return true;
}
@Override
public int getPriority() {
return NORMAL_PRIORITY;
}
}
public class ServicesLoadingStrategy implements LoadingStrategy {
@Override
public String directory() {
return "META-INF/services/";
}
@Override
public boolean overridden() {
// 支持覆盖
return true;
}
@Override
public int getPriority() {
return MIN_PRIORITY;
}
}
至此,可以知道strategies的内容如下:
- DubboInternalLoadingStrategy;
- DubboLoadingStrategy;
- ServicesLoadingStrategy;
且遍历顺序由上而下,而DubboLoadingStrategy和ServicesLoadingStrategy支持覆盖,所以,若三者都配置了某个Type的拓展实现类,则优先使用ServicesLoadingStrategy,其次DubboLoadingStrategy,最后才是DubboInternalLoadingStrategy;
知道了strategies的内容后,继续分析loadDirectory的实现,代码如下:
private void loadDirectory(Map> extensionClasses, LoadingStrategy strategy, String type) { loadDirectory(extensionClasses, strategy.directory(), type, strategy.preferExtensionClassLoader(), strategy.overridden(), strategy.includedPackages(), strategy.excludedPackages(), strategy.onlyExtensionClassLoaderPackages()); // 兼容以前的版本 String oldType = type.replace("org.apache", "com.alibaba"); loadDirectory(extensionClasses, strategy.directory(), oldType, strategy.preferExtensionClassLoader(), strategy.overridden(), strategy.includedPackagesInCompatibleType(), strategy.excludedPackages(), strategy.onlyExtensionClassLoaderPackages()); } private void loadDirectory(Map > extensionClasses, String dir, String type, boolean extensionLoaderClassLoaderFirst, boolean overridden, String[] includedPackages, String[] excludedPackages, String[] onlyExtensionClassLoaderPackages) { // 生成文件路径 String fileName = dir + type; try { List classLoadersToLoad = new LinkedList<>(); if (extensionLoaderClassLoaderFirst) { ClassLoader extensionLoaderClassLoader = ExtensionLoader.class.getClassLoader(); if (ClassLoader.getSystemClassLoader() != extensionLoaderClassLoader) { classLoadersToLoad.add(extensionLoaderClassLoader); } } Set classLoaders = scopeModel.getClassLoaders(); if (CollectionUtils.isEmpty(classLoaders)) { Enumeration resources = ClassLoader.getSystemResources(fileName); if (resources != null) { while (resources.hasMoreElements()) { loadResource(extensionClasses, null, resources.nextElement(), overridden, includedPackages, excludedPackages, onlyExtensionClassLoaderPackages); } } } else { classLoadersToLoad.addAll(classLoaders); } Map > resources = ClassLoaderResourceLoader.loadResources(fileName, classLoadersToLoad); // 获取到Resource URL后,逐个加载 resources.forEach(((classLoader, urls) -> { loadFromClass(extensionClasses, overridden, urls, classLoader, includedPackages, excludedPackages, onlyExtensionClassLoaderPackages); })); } catch (Throwable t) { logger.error("Exception occurred when loading extension class (interface: " + type + ", description file: " + fileName + ").", t); } } private void loadFromClass(Map > extensionClasses, boolean overridden, Set urls, ClassLoader classLoader, String[] includedPackages, String[] excludedPackages, String[] onlyExtensionClassLoaderPackages) { if (CollectionUtils.isNotEmpty(urls)) { for (java.net.URL url : urls) { // 加载Resource loadResource(extensionClasses, classLoader, url, overridden, includedPackages, excludedPackages, onlyExtensionClassLoaderPackages); } } } private void loadResource(Map > extensionClasses, ClassLoader classLoader, java.net.URL resourceURL, boolean overridden, String[] includedPackages, String[] excludedPackages, String[] onlyExtensionClassLoaderPackages) { try { try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), StandardCharsets.UTF_8))) { String line; String clazz; // 逐行遍历 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(); clazz = line.substring(i + 1).trim(); } else { // Java SPI形式 clazz = line; } if (StringUtils.isNotEmpty(clazz) && !isExcluded(clazz, excludedPackages) && isIncluded(clazz, includedPackages) && !isExcludedByClassLoader(clazz, classLoader, onlyExtensionClassLoaderPackages)) { // 加载Class loadClass(extensionClasses, resourceURL, Class.forName(clazz, true, classLoader), name, overridden); } } catch (Throwable t) { IllegalStateException e = new IllegalStateException("Failed to load extension class (interface: " + type + ", class line: " + line + ") in " + resourceURL + ", cause: " + t.getMessage(), t); exceptions.put(line, e); } } } } } catch (Throwable t) { logger.error("Exception occurred when loading extension class (interface: " + type + ", class file: " + resourceURL + ") in " + resourceURL, t); } } private void loadClass(Map > extensionClasses, java.net.URL resourceURL, Class> clazz, String name, boolean overridden) throws NoSuchMethodException { if (!type.isAssignableFrom(clazz)) { throw new IllegalStateException("Error occurred when loading extension class (interface: " + type + ", class line: " + clazz.getName() + "), class " + clazz.getName() + " is not subtype of interface."); } // 检测目标类上是否有 Adaptive 注解 if (clazz.isAnnotationPresent(Adaptive.class)) { // 缓存Adaptive类 cacheAdaptiveClass(clazz, overridden); } else if (isWrapperClass(clazz)) { // 检测 clazz 是否是 Wrapper 类型 // 缓存Wrapper类 cacheWrapperClass(clazz); } else { // 普通的拓展类 if (StringUtils.isEmpty(name)) { // 优先使用注解Extension.value()作为名称若存在的话 // 若本Class名以Type名结尾,则取去掉Type的前半部分 // 否则直接去本Class名 name = findAnnotationName(clazz); if (name.length() == 0) { throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + resourceURL); } } String[] names = NAME_SEPARATOR.split(name); if (ArrayUtils.isNotEmpty(names)) { // 缓存Activate类,若本类标注了Activate注解的话 cacheActivateClass(clazz, names[0]); for (String n : names) { // 缓存本拓展类和名称的映射关系 cacheName(clazz, n); // 缓存本拓展类 saveInExtensionClass(extensionClasses, clazz, n, overridden); } } } }
从配置文件中加载所有的拓展类后,通过指定的名称便可以得到指定的拓展实现类,接下来便是实例化该类,代码如下:
private Object createExtensionInstance(Class> type) throws ReflectiveOperationException {
// 默认为InstantiationStrategy
// 查找合适的构造方法,然后查找对应的构造参数进行创建实例
return instantiationStrategy.instantiate(type);
}
拓展实现类实例创建后,便需要注入其依赖,代码如下:
private T injectExtension(T instance) {
if (injector == null) {
return instance;
}
try {
for (Method method : instance.getClass().getMethods()) {
if (!isSetter(method)) {
// 跳过非Setter方法
continue;
}
if (method.isAnnotationPresent(DisableInject.class)) {
// 跳过@DisableInject标注的方法
continue;
}
Class> pt = method.getParameterTypes()[0];
if (ReflectUtils.isPrimitives(pt)) {
// 跳过原始类型参数的Setter方法
continue;
}
try {
// 获取参数名称
String property = getSetterProperty(method);
// 从容器中查找对应的依赖
Object object = injector.getInstance(pt, property);
if (object != null) {
// 注入依赖
method.invoke(instance, object);
}
} catch (Exception e) {
logger.error("Failed to inject via method " + method.getName()
+ " of interface " + type.getName() + ": " + e.getMessage(), e);
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return instance;
}
injector的赋值是在构造方法中,代码如下:
this.injector = (type == ExtensionInjector.class ? null : extensionDirector.getExtensionLoader(ExtensionInjector.class)
.getAdaptiveExtension());
ExtensionInjector的拓展实现类的加载基于Dubbo SPI机制,其配置文件为META-INF/dubbo/internal/org.apache.dubbo.common.extension.ExtensionInjector,内容如下:
spring=org.apache.dubbo.config.spring.extension.SpringExtensionInjector adaptive=org.apache.dubbo.common.extension.inject.AdaptiveExtensionInjector spi=org.apache.dubbo.common.extension.inject.SpiExtensionInjector scopeBean=org.apache.dubbo.common.beans.ScopeBeanExtensionInjector
getAdaptiveExtension()方法是基于后文介绍的Dubbo SPI 自适应拓展,这里暂不做分析,getAdaptiveExtension()方法最终获取到的是标注了@Adaptive的AdaptiveExtensionInjector,AdaptiveExtensionInjector中的injectors存放了其它的ExtensionInjector的拓展实现类,其getInstance方法代码如下:
publicT getInstance(Class type, String name) { // 遍历内部的ExtensionInjector拓展实现类 for (ExtensionInjector injector : injectors) { T extension = injector.getInstance(type, name); if (extension != null) { // 找到则立即返回 return extension; } } return null; }
拓展实例创建且注入相应的依赖后,最后一步便是包装;
如果需要包装,且存在包装类的话,那么就依次进行包装,核心代码如下:
// 将当前 instance 作为参数传给 Wrapper 的构造方法,并通过反射创建 Wrapper 实例并注入依赖 instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance)); // 进行初始化后处理 instance = postProcessAfterInitialization(instance, name);Dubbo SPI 自适应拓展 简介
在Dubbo中,很多拓展都是通过SPI机制进行加载的,比如Protocol、Cluster、LoadBalance等。有时,有些拓展并不想在框架启动阶段被加载,而是希望在拓展方法被调用时,根据运行时参数进行加载。这听起来有些矛盾,拓展未被加载,那么拓展方法就无法被调用(静态方法除外);拓展方法未被调用,拓展就无法被加载。对于这个矛盾的问题,Dubbo通过自适应拓展机制很好地解决了。
自适应拓展机制的实现逻辑比较复杂,如果存在具有代理功能的拓展实现类(标注了@Adaptive)则Dubbo直接使用,否则Dubbo会为该拓展接口生成具有代理功能的代码,然后通过javassist或jdk编译这段代码,得到代理Class类;



