Java提供了SPI机制(ServiceLoader)来进行服务发现,而Dubbo中的扩展点同样使用了SPI机制进行扩展点的发现。
@SPI
@SPI注解的作用主要是表示这个接口是一个扩展点,并且可以在value中标明默认实现。Dubbo中的所有扩展点都标注了这个注解。
ExtensionLoader基础
类似于JDK中的ServiceLoader,进行加载SPI的工作;每一个ExtensionLoader的实例都和一个SPI接口相关联,他们存在ExtensionLoader的类变量中
private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap<Class<?>, ExtensionLoader<?>>();
- 加载路径
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/";
- 文件名仍然是接口的全限定名,文件内容是name=实现类的全限定名
dubbo=com.alibaba.dubbo.registry.dubbo.DubboRegistryFactory
- 加载方法ExtensionLoader#getExtensionClasses
这个方法用于加载所有的SPI实现类,是最基础的方法,ExtensionLoader后面的很多方法都会最终调这个方法,分为以下几个函数与步骤。
- 加载Extension,加锁保证不会重复加载
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;
}
- type是需要读取的服务接口的Class对象,从接口的@SPI注解上读到默认的接口实现,然后去三个目录分别加载服务的实现类
private Map<String, Class<?>> loadExtensionClasses() {
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<?>>();
loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY);
loadDirectory(extensionClasses, DUBBO_DIRECTORY);
loadDirectory(extensionClasses, SERVICES_DIRECTORY);
return extensionClasses;
}
- 默认使用ExtensionLoader的classLoader来加载资源
private void loadDirectory(Map<String, Class<?>> extensionClasses, String dir) {
String fileName = dir + type.getName();
try {
Enumeration<java.net.URL> 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 resourceURL = urls.nextElement();
loadResource(extensionClasses, classLoader, resourceURL);
}
}
} catch (Throwable t) {
logger.error("Exception when load extension class(interface: " +
type + ", description file: " + fileName + ").", t);
}
}
- 逐行读取文件内容(UTF-8编码),去除注释,将实现的名字name与实现的全限定名line分别获取,然后加载
private void loadResource(Map<String, Class<?>> extensionClasses, ClassLoader classLoader, java.net.URL resourceURL) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "utf-8"));
try {
String line;
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();
line = line.substring(i + 1).trim();
}
if (line.length() > 0) {
loadClass(extensionClasses, resourceURL, Class.forName(line, true, classLoader), name);
}
} 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);
}
}
}
} finally {
reader.close();
}
} catch (Throwable t) {
logger.error("Exception when load extension class(interface: " +
type + ", class file: " + resourceURL + ") in " + resourceURL, t);
}
}
- 主要是将name与实现类关联起来,放到一个map
(1) 检查clazz实现类是否实现了type接口
(2) 实现类是否有@Adaptive注解,如果有那么缓存这个实现类到cachedAdaptiveClass,每个SPI接口只能有一个适配器类。Dubbo源码中只有AdaptiveCompiler
和AdaptiveExtensionFactory
两个实现类标注了Adaptive注解,标注了Adaptive注解的实现类会被优先使用。
(3) 实现类是否是一个包装类,如果是的话那么缓存这个实现类到cachedWrapperClasses
(4) 如果都不满足,那么就是一个具体的实现类,这个时候将name到clazz的映射存到一个map中。如果clazz标注了Activate注解,那么存到cachedActivates中。
private void loadClass(Map<String, Class<?>> extensionClasses, java.net.URL resourceURL, Class<?> clazz, String name) throws NoSuchMethodException {
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.");
}
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 if (isWrapperClass(clazz)) {
Set<Class<?>> wrappers = cachedWrapperClasses;
if (wrappers == null) {
cachedWrapperClasses = new ConcurrentHashSet<Class<?>>();
wrappers = cachedWrapperClasses;
}
wrappers.add(clazz);
} else {
clazz.getConstructor();
if (name == null || name.length() == 0) {
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 (names != null && names.length > 0) {
Activate activate = clazz.getAnnotation(Activate.class);
if (activate != null) {
cachedActivates.put(names[0], activate);
}
for (String n : names) {
if (!cachedNames.containsKey(clazz)) {
cachedNames.put(clazz, n);
}
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());
}
}
}
}
}
ExtensionLoader加载扩展点
加载扩展点的方法有如下的三种类型
- 加载指定名称的扩展点或默认的扩展点
Protocol refprotocol = ExtensionLoader.getExtensionLoader(Protocol.class).getDefaultExtension();
Protocol refprotocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("dubbo");
- 加载适配器扩展点
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
getExtension(String) 获取指定的扩展点
关键点1:生成ExtensionLoader
getExtensionLoader(Protocal.class)会创建一个Protocal类型的ExtensionLoader,然后放到类变量EXTENSION_LOADERS里,其中也会创建一个ExtensionFactory,用于依赖注入,因为所有的接口都是扩展点,所以ExtensionFactory也是通过ExtensionLoader加载的,而且是适配器类型的,其实也就是AdaptiveExtensionFactory,这里判断type是否是ExtensionFactory是为了避免死循环,如下:
private ExtensionLoader(Class<?> type) {
this.type = type;
objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
}
关键点2:创建并获取Extension
getDefaultExtension(),内部调用getExtension实现,加锁保证每个Extension只会create一次
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;
}
先加载ExtensionClasses,然后获取这个类型的实例,然后注入依赖injectExtension,然后用包装类型,依次包装这个实例(AOP)
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);
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);
}
}
关键点3:依赖注入
使用injectExtension方法完成IOC,遍历Extension所有公有set方法,根据参数类型和变量名从ExtensionFactory(也就是AdaptiveExtensionFactory)中加载依赖的Extension
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())) {
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;
}
关键点4:objectFactory
从objectFactory获取Extension时,用的是
private ExtensionLoader(Class<?> type) {
this.type = type;
objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
}
也就是Adaptive类型的ExtensionFactory,在这里也即是AdaptiveExtensionFactory,原因后面再说。
使用AdaptiveExtensionFactory获取Extension其实也就是遍历所有的ExtensionFactory来获取。
@Adaptive
public class AdaptiveExtensionFactory implements ExtensionFactory {
private final List<ExtensionFactory> factories;
public AdaptiveExtensionFactory() {
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);
}
@Override
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;
}
}
dubbo源码里除了AdaptiveExtensionFactory外就只有两个ExtensionFactory,一个是SpiExtensionFactory还有一个是SpringExtensionFactory,如名,SpiExtensionFactory用于加载dubbo中的SPI接口的依赖,SpringExtensionFactory用于加载Spring容器管理的依赖。
值得强调的是,通过SpiExtensionFactory加载SPI接口时,name参数是没用的,只用了类型参数,而且获取的是这个SPI接口Adaptive类型的实现。
getAdaptiveExtension() 获取适配器类型的扩展点
关键点1:加锁
加锁保证AdaptiveExtension只创建一次,类似于普通Extension的创建过程
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);
}
}
}
} else {
throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
}
}
return (T) instance;
}
关键点2:依赖注入
load所有的Extension;如果有标注了@Adaptive注解的Extension实现,那么使用这个,如果没有Adaptive类型的Extension那么创造一个出来;然后进行依赖注入。(值得一提的是之前的ExtensionFactory接口,AdaptiveExtensionFactory就标注了@Adaptive,所以创建ObjectFactory的时候使用的就是这个实现)
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);
}
}
private Class<?> getAdaptiveExtensionClass() {
getExtensionClasses();
if (cachedAdaptiveClass != null) {
return cachedAdaptiveClass;
}
return cachedAdaptiveClass = createAdaptiveExtensionClass();
}
关键点3:createAdaptiveExtensionClass
(1) 构造源码;
(2) 获取Compiler的Adaptive实现,也就是AdaptiveCompiler;AdaptiveCompiler默认会使用JavassistCompiler来进行编译;
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);
}
关键点4:构造源码
private String createAdaptiveExtensionClassCode() {
StringBuilder codeBuilder = new StringBuilder();
Method[] methods = type.getMethods();
// 检查SPI接口是否有Adaptive方法,如果没有就不需要生成。
// no need to generate adaptive class since there's no adaptive method found.
if (!hasAdaptiveAnnotation)
throw new IllegalStateException("No adaptive method on extension " + type.getName() + ", refuse to create the adaptive class!");
// 为每个接口方法生成实现
for (Method method : methods) {
Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class);
// 如果不是Adaptive方法,那么方法体中抛出异常
if (adaptiveAnnotation == null) {
code.append("throw new UnsupportedOperationException(\"method ")
.append(method.toString()).append(" of interface ")
.append(type.getName()).append(" is not adaptive method!\");");
} else {
// 看方法参数中是否有URL类型的参数
// found parameter in URL type
if (urlTypeIndex != -1) {
// Null Point check
String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"url == null\");",
urlTypeIndex);
code.append(s);
}
// did not find parameter in URL type
else {
String attribMethod = null;
// find URL getter method
// 检查参数中是否有获取URL的方法,如果没有,那么无法生成
if (attribMethod == null) {
throw new IllegalStateException("fail to create adaptive class for interface " + type.getName()
+ ": not found url parameter or url attribute in parameters of method " + method.getName());
}
// Null point check
String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"%s argument == null\");",
urlTypeIndex, pts[urlTypeIndex].getName());
code.append(s);
s = String.format("\nif (arg%d.%s() == null) throw new IllegalArgumentException(\"%s argument %s() == null\");",
urlTypeIndex, attribMethod, pts[urlTypeIndex].getName(), attribMethod);
code.append(s);
}
// 到这里为止,方法体做的事情就是获取一个URL的参数,校验它不为空
String[] value = adaptiveAnnotation.value();
// value is not set, use the value generated from class name as the key
// 这个value也就是URL里面参数的名称,如果Adaptive的注解里没有指定的话,就从SPI接口的名称中生成,默认是接口名的小写,遇到大写单词,中间添加'.';后面动态选择实现的时候,从URL对象中getParameter也就是具体实现的名字
/* @Adaptive({Constants.PROXY_KEY})==========="proxy"
* <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) throws RpcException;
*
* 如果URL中是dubbo:xxxx?proxy=jdk,而SPI中的值是javassist,那么就是
*
* String extName = url.getParameter("proxy", "javassist"); //结果是jdk
*/
// 判断是否有Invocation类型的参数,如果有,那么从URL中根据invocation中的method和key获取到参数;如果没有Invocation,直接从URL中获取key的值
//
String defaultExtName = cachedDefaultName;
String getNameCode = null;
for (int i = value.length - 1; i >= 0; --i) {
if (i == value.length - 1) {
if (null != defaultExtName) {
if (!"protocol".equals(value[i]))
if (hasInvocation)
getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName);
else
getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", value[i], defaultExtName);
else
getNameCode = String.format("( url.getProtocol() == null ? \"%s\" : url.getProtocol() )", defaultExtName);
} else {
if (!"protocol".equals(value[i]))
if (hasInvocation)
getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName);
else
getNameCode = String.format("url.getParameter(\"%s\")", value[i]);
else
getNameCode = "url.getProtocol()";
}
} else {
if (!"protocol".equals(value[i]))
if (hasInvocation)
getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName);
else
getNameCode = String.format("url.getParameter(\"%s\", %s)", value[i], getNameCode);
else
getNameCode = String.format("url.getProtocol() == null ? (%s) : url.getProtocol()", getNameCode);
}
}
code.append("\nString extName = ").append(getNameCode).append(";");
// check extName == null?
String s = String.format("\nif(extName == null) " +
"throw new IllegalStateException(\"Fail to get extension(%s) name from url(\" + url.toString() + \") use keys(%s)\");",
type.getName(), Arrays.toString(value));
code.append(s);
s = String.format("\n%s extension = (%<s)%s.getExtensionLoader(%s.class).getExtension(extName);",
type.getName(), ExtensionLoader.class.getSimpleName(), type.getName());
code.append(s);
// return statement
if (!rt.equals(void.class)) {
code.append("\nreturn ");
}
s = String.format("extension.%s(", method.getName());
code.append(s);
for (int i = 0; i < pts.length; i++) {
if (i != 0)
code.append(", ");
code.append("arg").append(i);
}
code.append(");");
}
// 生成方法体
return codeBuilder.toString();
}
@Adaptive
Adaptive可以用在方法与类上,用在类上表明这是一个Adaptive实现;用在接口方法上,表示这个接口需要生成一个适配器类,然后根据URL动态的调用不同的实现。
为什么要创建设配类,一个接口多种实现,SPI机制也是如此,这是策略模式,但是我们在代码执行过程中选择哪种具体的策略呢。Dubbo采用统一数据模式com.alibaba.dubbo.common.URL(它是dubbo定义的数据模型不是jdk的类),它会穿插于系统的整个执行过程,URL中定义的协议类型字段protocol,会根据具体业务设置不同的协议。url.getProtocol()值可以是dubbo也是可以webservice, 可以是zookeeper也可以是redis。
设配类的作用是根据url.getProtocol()的值extName,去ExtensionLoader. getExtension( extName)选取具体的扩展点实现。
优先级: @Adaptive实现 -> url 参数指定的实现 -> 默认实现
网友评论