1:Dubbo spi 较JAVA spi 优点
1.1 spi 全称Service Provider Interface
优点:第三方可通过实现接口,扩展自己的实现,将功能的实现逻辑控制权交由第三方配置,从而实现插件的可拔插。
1.2 java spi 举例:
//接口
package com.alibaba.dubbo.common.spitest;
public interface Car {
void drive();
}
//benz 实现
package com.alibaba.dubbo.common.spitest.impl;
import com.alibaba.dubbo.common.spitest.Car;
public class BenzCar implements Car {
public BenzCar(){
System.out.println("BenzCar 构造函数执行");
}
static {
System.out.println("BenzCar 静态代码块执行");
}
@Override
public void drive() {
System.out.println("benz car drive");
}
}
//byd 实现
package com.alibaba.dubbo.common.spitest.impl;
import com.alibaba.dubbo.common.spitest.Car;
public class BYDCar implements Car {
public BYDCar(){
System.out.println("BYDCar 构造函数执行");
}
static {
System.out.println("BYDCar 静态代码块执行");
}
@Override
public void drive() {
System.out.println("byd car drive");
}
}
在resources/META-INF/services 下新增文件com.alibaba.dubbo.common.spitest.Car(改文件为接口包名+接口名称)
文件内容:
com.alibaba.dubbo.common.spitest.impl.BenzCar
com.alibaba.dubbo.common.spitest.impl.BYDCar
//测试类
public class JavaSpiTest {
public static void main(String[] args) {
ServiceLoader loader = ServiceLoader.load(Car.class);
Iterator<Car> itr = loader.iterator();
while (itr.hasNext()){
itr.next().drive();
}
}
}
输出结果:
BenzCar 静态代码块执行
BenzCar 构造函数执行
benz car drive
BYDCar 静态代码块执行
BYDCar 构造函数执行
byd car drive
1.3 Dubbo 较 java 原生spi 改进点:
JDK 标准的 SPI 会一次性实例化扩展点所有实现,如果有扩展实现初始化很耗时(如通过构造函数进行初始化数据),但如果没用上也加载,会很浪费资源,见1.2例子
- 而 Dubbo SPI 读取扩展点配置后,通过Class.forName进行类加载(会执行静态代码块,但是构造函数不执行)使用时在初始化
- 支持AOP和IOC功能
2:Dubbo如何实现SPI扩展点的加载(扩展点其实就是SPI接口的实现类)
2.1 理解其加载过程之前,我们首先需要理解几个注解
- SPI :注解在接口类上,标识该接口支持SPI
- Adative : 可注解在类上,也可注解在方法上
当注解在类上,则扩展点直接取该类实例(优先级>方法级别注解)
当注解在方法上时,需要动态生成一个代理类XXX$Adative,通过该代理内ExtensionLoader.getExtensionLoader(XXXX.class).getExtension(extName) 获取对应的扩展点见下为Protocol生成的动态代理类:
//该动态代理类为Protocol 生成的动态代理类,直接源码开启debug 即可输出,
//如果想debug 该动态代理类,可以直接将其拷贝到对应的包目录下即可
public class Protocol$Adaptive implements com.alibaba.dubbo.rpc.Protocol {
该接口无Adative注解,所以通过动态代理类调用类直接抛异常
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!");
}
//该接口无Adative注解,所以通过动态代理类调用类直接抛异常
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.Invoker refer(java.lang.Class arg0, com.alibaba.dubbo.common.URL arg1) throws com.alibaba.dubbo.rpc.RpcException {
if (arg1 == null) throw new IllegalArgumentException("url == null");//URL 比传
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);
}
//用于服务暴露
public com.alibaba.dubbo.rpc.Exporter export(com.alibaba.dubbo.rpc.Invoker arg0) throws com.alibaba.dubbo.rpc.RpcException {
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);
}
}
总结:
默认使用javassit对该类进行编译
由此可见,想获取对应的扩展点,需要从URL参数中提取;当方法中无url参数时,调用该方法会有异常
参数获取扩展点优先级: key1>key2>类转小写,原先大写字母位置新增点>spi 默认值;key1,key2 为Adative 的值
- Activate:可注解在类上,也可注解在方法上
2.2 ExtensionLoader是如何进行SPI 加载
ExtensionLoader 之 loadExtensionClasses
private Map<String, Class<?>> loadExtensionClasses() {
final SPI defaultAnnotation = type.getAnnotation(SPI.class);//接口是否存在注解SPI
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<?>>();
loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY);//META-INF/dubbo/internal 目录下加载扩展点
loadDirectory(extensionClasses, DUBBO_DIRECTORY);//META-INF/dubbo 目录下加载扩展点
loadDirectory(extensionClasses, SERVICES_DIRECTORY);//META-INF/services 目录下加载扩展点
return extensionClasses;
}
总结:
1:到指定目录下加载扩展点(META-INF/dubbo/internal;META-INF/dubbo;META-INF/services)
最后缓存到几个比较重要的属性中
cachedAdaptiveClass : Adative注解在类上的,只能有一个,不然后面的将解析不到
cachedWrapperClasses:是否是包装类(判断是否是存在构造函数的参数为该扩展点接口类型,存在则认为是该扩展点的包装类,如ProtocolFilterWrapper,ProtocolListenerWrapper 则为Protocol的包装类)
cachedActivates:注解Activte的类
extensionClasses:注解Adative 不在类上的
2:我们经常通过getAdaptiveExtension 方法和 getExtension(String name) 获取对应的扩展点
当使用getAdaptiveExtension 时会调用以下方法:
private Class<?> getAdaptiveExtensionClass() {
getExtensionClasses();//触发loadExtensionClasses 进行总结第一点的几个属性填充
if (cachedAdaptiveClass != null) {
return cachedAdaptiveClass; //判断是否存在注解Activte的类,存在则返回
}
return cachedAdaptiveClass = createAdaptiveExtensionClass();//否则通过动态代理技术生成动态类
}
通过2.1我们可以窥探出该动态代理类的形式,最终都会通过 getExtension(String name) 获取对应的扩展点;在获取对应扩展点时,会判断该接口是否存在对应的包装类,如果存在则返回该包装类,包装类包装了具体真实要用到的扩展点
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;
}
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);//为扩展点进行IOC处理
//会为实例注入对应的扩展点或者spring 容器bean,前提是该实例具有和扩展点一致,或者spring bean 名称一致的属性,
//同时存在对应的set方法
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);
}
}
后续会继续探讨 dubbo spi 其他细节问题;如如何进行IOC和AOP;举例Protocol 分析整个扩展点的获取过程;喜欢的加关注