1.SPI介绍
参考此篇文章
https://www.cnblogs.com/huzi007/p/6679215.html
2.dubbo扩展点
参考此篇文章
http://blog.csdn.net/xiaoxufox/article/details/75117992
可以理解ExtensionLoader相比JDK SPI是扩展版本
- ServiceLoader只能遍历加载所有实现,且非单实例
- ExtensionLoader可以加载特定类,且是单实例
我们重点关注Adaptive的使用
3.Adaptive注解
3.1原理
可以先参考此篇文章
http://gaofeihang.cn/archives/278
其适配器有2种形式定义
- 静态代码适配
- 动态代码生成适配
3.2CacheFactory示例
我们来看内置的CacheFactory
代码示例1
@SPI("lru")
public interface CacheFactory {
@Adaptive("cache")
Cache getCache(URL url);
}
其动态生成的适配器代码如下:
代码示例2
package com.alibaba.dubbo.cache;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class CacheFactory$Adpative implements com.alibaba.dubbo.cache.CacheFactory {
public com.alibaba.dubbo.cache.Cache getCache(com.alibaba.dubbo.common.URL arg0) {
if (arg0 == null) throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getParameter("cache", "lru");
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.cache.CacheFactory) name from url("
+ url.toString() + ") use keys([cache])");
com.alibaba.dubbo.cache.CacheFactory extension = (com.alibaba.dubbo.cache.CacheFactory) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.cache.CacheFactory.class).getExtension(extName);
return extension.getCache(arg0);
}
}
上面代码第8行是重点,URL的作用有2点
- 通过参数来选择适配对象
- 通过URL来传递参数,该功能作为可选项,如果只是当做适配器使用,URL参数就是显得比较多余
3.3自定义示例
通过自定义的例子再了来看一下
代码示例3
@SPI("dirFirst")
public interface DirExtension {
@Adaptive({SpiConstants.DIR_DEFAULT})
String sayHello(URL url, String name);
}
public class DirExtensionFirstImpl implements DirExtension {
@Override
public String sayHello(URL url, String name) {
return name + "-first";
}
}
public class DirExtensionDefaultImpl implements DirExtension {
@Override
public String sayHello(URL url, String name) {
return name + "-defualt";
}
}
public class DirExtensionTest {
public static void main(String[] args){
ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(DirExtension.class);
DirExtension dirExtension = (DirExtension)extensionLoader.getAdaptiveExtension();
Map parameters = new HashMap();
//parameters.put("dirDefault", "dirFirst");
parameters.put("dirDefault", "dirDefault");
URL url = new URL("dubbo", "127.0.0.1", 8089, parameters);
System.out.println(dirExtension.sayHello(url, "qbb"));
}
}
上面的dirDefault参数作为参数key进行传值进行选择实例,以下是代码适配器自动生成的代码
代码示例4
package demo.dubbo;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class DirExtension$Adpative implements demo.dubbo.DirExtension {
public java.lang.String sayHello(com.alibaba.dubbo.common.URL arg0, java.lang.String arg1) {
if (arg0 == null) throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getParameter("dirDefault", "dirFirst");
if(extName == null) throw new IllegalStateException("Fail to get extension(demo.dubbo.DirExtension) name from url("
+ url.toString() + ") use keys([dirDefault])");
demo.dubbo.DirExtension extension = (demo.dubbo.DirExtension)ExtensionLoader.getExtensionLoader(demo.dubbo.DirExtension.class).getExtension(extName);
return extension.sayHello(arg0, arg1);
}
}
3.4自定义适配器
也可以不通过URL的方式进行传参数,可以来自定义参数来实现适配器
代码示例5
@SPI("dir2Default")
public interface Dir2Extension {
String sayHello(String name, String type);
}
@Adaptive
public class Dir2ExtensionAdaptive implements Dir2Extension{
@Override
public String sayHello(String name, String type) {
ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(Dir2Extension.class);
Dir2Extension dir2Extension = (Dir2Extension)extensionLoader.getDefaultExtension();
if (type != null && SpiConstants.DIR2_FIRST.equalsIgnoreCase(type.trim())) {
dir2Extension = (Dir2Extension)extensionLoader.getExtension(SpiConstants.DIR2_FIRST);
}
return dir2Extension.sayHello(name, type);
}
}
public class Dir2ExtensionTest {
public static void main(String[] args){
ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(Dir2Extension.class);
Dir2Extension dirExtension = (Dir2Extension)extensionLoader.getAdaptiveExtension();
String type = SpiConstants.DIR2_FIRST;
//type = SpiConstants.DIR2_DEFAULT;
//type = SpiConstants.DIR2_FIRST;
System.out.println(dirExtension.sayHello("qbb2", type));
}
}
实质还是没有变化,只不过把URL参数改成其他类型了而已
3.4对Adaptive的理解
如果没有引入该概念的时候,如何使用对象实例呢?如下代码
代码示例6
ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(DirExtension.class);
DirExtension dirExtension = (DirExtension)extensionLoader.getExtension(SpiConstants.DIR_DEFAULT);
System.out.println(dirExtension.sayHello(null, "qbb"));
dirExtension = (DirExtension)extensionLoader.getExtension(SpiConstants.DIR_FIRST);
System.out.println(dirExtension.sayHello(null, "qbb"));
上面先获取对象,然后再调用方法,此处没有Adaptive,参数URL是多余的
假设一个接口有很多方法,又需要对不同实现的实例进行调用,上面的方式就显得有些并不方便了,如果能够拿到一个适配对象,然后传入产生进行选择实例调用,将会大大减少代码量的编写,而且适配器的代码基本是比较类似的,为了抽象公共的适配器代码,所以使用了URL进行参数传值,并且采用动态代码生成适配器的方式来实现。
我们可以通过下面的代码再跟上面的比较一下,实质效果是相同的,主要是思维方式发生了变化
代码示例7
dirExtension = (DirExtension)extensionLoader.getAdaptiveExtension();
Map parameters = new HashMap();
parameters.put("dirDefault", SpiConstants.DIR_FIRST);
URL url = new URL("dubbo", "127.0.0.1", 8089, parameters);
System.out.println(dirExtension.sayHello(url, "qbb"));
代码示例
https://github.com/sevenXin/javaDemo/tree/master/demo-dubbo