Dubbo SPI
dubbo版本为2.7.7
在学习Dubbo SPI时,首先要搞清楚两个概念。
- 扩展点:通过SPI机制查找并加载实现的接口,都是扩展点。
- 扩展点实现:实现了扩展点对应接口的实现类
Dubbo的配置文件主要分三类
1.兼容JDK SPI
2.用户自定义SPI
3.Dubbo内置使用的SPI
SPI格式为KV格式,例如:
dubbo=org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol
@SPI注解
只要是接口上被标注了@SPI注解的,都是SPI接口
@SPI注解就一个value()方法,如果在通过Dubbo SPI加载Protocol接口的实现时,没有明确指定扩展名,则默认会将@SPI注解的value方法的值作为扩展名,即加载dubbo这个扩展名对应的扩展实现类
ExtensionLoader
1、ExtensionLoader用来加载哪些实现了@SPI注解的接口实现类,功能与JDK的ServiceLoader类似,不过提供了更全的功能
2、ExtensionLoader主要加载三个目录下的配置文件:
1)META-INF/services/ 该目录下的SPI配置文件用来兼容JDK的SPI,对应实现类为ServicesLoadingStrategy
2)META-INF/dubbo/ 该目录用于存放用户自定义的dubbo SPI配置文件,对应实现为DubboLoadingStrategy
3)META-INF/dubbo/internal/ 该目录用于存放dubbo内部使用的SPI配置,对应实现为DubboInternalLoadingStrategy
他们都实现了Prioritized优先级接口,优先级为:
DubboInternalLoadingStrategy > DubboLoadingStrategy > ServicesLoadingStrategy
ExtensionLoader常用方法
public static void main(String[] args) {
//通过ExtensionLoader实现Dubbo SPI核心逻辑
ExtensionLoader<Protocol> extensionLoader = ExtensionLoader.getExtensionLoader(Protocol.class);
//ExtensionLoader加载的Protocol接口根据扩展名dubbo找到对应的实现类
Protocol protocol = extensionLoader.getExtension("dubbo");
//加载默认的实现类
Protocol defaultProtocol = extensionLoader.getDefaultExtension();
//加载适配器
Protocol adaptiveProtocol = extensionLoader.getAdaptiveExtension();
//获取扩展名为dubbo的实现类,如果获取不到则获取默认的
Protocol defaultOrProtocol = extensionLoader.getOrDefaultExtension("dubbo");
//获取到ExtensionLoader加载的Protocol所有实现类
List<Protocol> protocols = extensionLoader.getLoadedExtensionInstances();
}
ExtensionLoader字段
//ExtensionLoader核心字段,LoadingStrategy接口有三个实现,分别为:
//DubboInternalLoadingStrategy、DubboLoadingStrategy、ServicesLoadingStrateg
//用于加载不同配置文件下的SPI配置
private static volatile LoadingStrategy[] strategies = loadLoadingStrategies();
//用于存放全部的ExtensionLoader实例,每一个扩展接口对应一个ExtensionLoader实例,其中key为扩展的接口,value为加载其扩展实现的ExtensionLoader实例
private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap<>(64);
//该集合缓存了扩展实现类与其实例的映射关系,key为class,value为实现类,如key为Protocol,value为DubboProtocol对象
private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<>(64);
//表示当前ExtensionLoader实例负责加载扩展接口
private Class<?> type;
//记录了type扩展接口SPI注解的value值
private String cachedDefaultName;
//缓存了ExtensionLoader加载的扩展实现类与扩展名之间的映射关系
private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap<>();
//缓存了ExtensionLoader加载的扩展名与实现类之间的映射关系,与上面的缓存关系相反
private final Holder<Map<String, Class<?>>> cachedClasses = new Holder<>();
//缓存了ExtensionLoader加载的扩展名与扩展实现对象之间的关系
private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<>();
ExtensionLoader通过getExtensionLoader()方法获取EXTENSION_LOADERS缓存中对应的ExtensionLoader实例
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 an interface!");
}
if (!withExtensionAnnotation(type)) {
throw new IllegalArgumentException("Extension type (" + type +
") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!");
}
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);
}
return loader;
}
得到接口对应的ExtensionLoader对象之后会调用getExtension()方法,根据传入的扩展名称从cachedInstances缓存中查找扩展实现的实例,最终将其实例化后返回
public T getExtension(String name, boolean wrap) {
if (StringUtils.isEmpty(name)) {
throw new IllegalArgumentException("Extension name == null");
}
if ("true".equals(name)) {
return getDefaultExtension();
}
//getOrCreateHolder方法中封装了查找cachedInstances缓存的逻辑
final Holder<Object> holder = getOrCreateHolder(name);
Object instance = holder.get();
//双重检索防止并发问题
if (instance == null) {
synchronized (holder) {
instance = holder.get();
if (instance == null) {
//根据扩展名从SPI配置文件中查找对应的扩展实现类
instance = createExtension(name, wrap);
holder.set(instance);
}
}
}
return (T) instance;
}
//从cachedInstances中根据name获取扩展实例的实现对象
private Holder<Object> getOrCreateHolder(String name) {
Holder<Object> holder = cachedInstances.get(name);
if (holder == null) {
cachedInstances.putIfAbsent(name, new Holder<>());
holder = cachedInstances.get(name);
}
return holder;
}
//根据扩展名加载相应扩展实现类的实例化,还实现了自动装配以及自动Wrapper包等功能
private T createExtension(String name, boolean wrap) {
//根据扩展名从cachedClasses缓存中获取扩展实现类
Class<?> clazz = getExtensionClasses().get(name);
if (clazz == null || unacceptableExceptions.contains(name)) {
throw findException(name);
}
try {
//根据扩展实现类从EXTENSION_INSTANCES缓存中查找对应的实例
T instance = (T) EXTENSION_INSTANCES.get(clazz);
if (instance == null) {
//如果没有查到对应的实例,则通过反射创建扩展实现对象
EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.getDeclaredConstructor().newInstance());
//通过反射创建对应的实现类后存入缓存
instance = (T) EXTENSION_INSTANCES.get(clazz);
}
//自动装配
injectExtension(instance);
if (wrap) {
List<Class<?>> wrapperClassesList = new ArrayList<>();
if (cachedWrapperClasses != null) {
wrapperClassesList.addAll(cachedWrapperClasses);
wrapperClassesList.sort(WrapperComparator.COMPARATOR);
Collections.reverse(wrapperClassesList);
}
if (CollectionUtils.isNotEmpty(wrapperClassesList)) {
for (Class<?> wrapperClass : wrapperClassesList) {
Wrapper wrapper = wrapperClass.getAnnotation(Wrapper.class);
if (wrapper == null
|| (ArrayUtils.contains(wrapper.matches(), name) && !ArrayUtils.contains(wrapper.mismatches(), name))) {
instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));
}
}
}
}
//如果实现了Lifecycle接口,则调用初始化方法
initExtension(instance);
return instance;
} catch (Throwable t) {
throw new IllegalStateException("Extension instance (name: " + name + ", class: " +
type + ") couldn't be instantiated: " + t.getMessage(), t);
}
}
@Adaptive注解 适配器
Dubbo使用@Adaptive注解来实现适配器功能
如:ExtensionFactory接口,他有三个实现类分别为:SpiExtensionFactory、AdaptiveExtensionFactory、SpringExtensionFactory
其中AdaptiveExtensionFactory上就有@Adaptive注解,用于适配SpiExtensionFactory和SpringExtensionFactory这两种实现,根据运行时的一些状态来选择具体调用ExtensionFactory的哪个实现
@Adaptive注解还可以加到接口方法之上,Dubbo会动态生成适配器类
例如,Transporter接口就有两个被@Adaptive注解修饰的方法:
@SPI("netty")
public interface Transporter {
/**
* Bind a server.
*
* @param url server url
* @param handler
* @return server
* @throws RemotingException
* @see org.apache.dubbo.remoting.Transporters#bind(URL, ChannelHandler...)
*/
@Adaptive({Constants.SERVER_KEY, Constants.TRANSPORTER_KEY})
RemotingServer bind(URL url, ChannelHandler handler) throws RemotingException;
/**
* Connect to a server.
*
* @param url server url
* @param handler
* @return client
* @throws RemotingException
* @see org.apache.dubbo.remoting.Transporters#connect(URL, ChannelHandler...)
*/
@Adaptive({Constants.CLIENT_KEY, Constants.TRANSPORTER_KEY})
Client connect(URL url, ChannelHandler handler) throws RemotingException;
}
Dubbo会生成一个Transporter$Adaptive适配器类,该类继承Transporter接口:
public class Transporter$Adaptive implements Transporter {
public org.apache.dubbo.remoting.Client connect(URL arg0, ChannelHandler arg1) throws RemotingException {
// 必须传递URL参数
if (arg0 == null) throw new IllegalArgumentException("url == null");
URL url = arg0;
// 确定扩展名,优先从URL中的client参数获取,其次是transporter参数
// 这两个参数名称由@Adaptive注解指定,最后是@SPI注解中的默认值
String extName = url.getParameter("client",
url.getParameter("transporter", "netty"));
if (extName == null)
throw new IllegalStateException("...");
// 通过ExtensionLoader加载Transporter接口的指定扩展实现
Transporter extension = (Transporter) ExtensionLoader
.getExtensionLoader(Transporter.class)
.getExtension(extName);
return extension.connect(arg0, arg1);
}
... // 省略bind()方法
}