一.参考
dubbo启动,使用spi动态扩展的地方参考之前写的
二.架构
1.每个接口或者ExtensionFactory都对应一个ExtensionLoader。对接口主要有四步操作:
(1).获取接口的ExtensionLoader.
(2).从扩展文件中读取对应接口的所有实现类
(3).创建优先级最高,真正使用的类的实例
(4).注入这个类的属性,通过setxxx方法.
2.从优先级看,@Adaptive类 > url 路径 > @Spi的值.
3.@Adaptive用在方法上时,key是url的参数名,通过这个参数名的取值判断用哪个实现类.
@Activate用于配置不同实现类的分组,组内的优先级.
三.源码细节
(一).Protocol接口加载
测试代码是ServiceConfig的成员protocl,初始化代码为
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
加载dubbo的Protocol接口的ExtensionLoader.
1.ExtensionLoader#getExtensionLoader()方法,入参为Protocol接口。代码如下:
public static ExtensionLoader getExtensionLoader(Class type) {
//接口不能为空
if (type == null)
throw new IllegalArgumentException("Extension type == null");
//传入type必须是接口
if (!type.isInterface()) {
throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
}
//接口必须带有@spi注解
if (!withExtensionAnnotation(type)) {
throw new IllegalArgumentException("Extension type(" + type +
") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
}
//在缓存ExtensionLoader#EXTENSION_LOADERS是否已经有loader了,没有再创建
ExtensionLoader loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);
if (loader == null) {
//创建ExtensionLoader,操作都在构造方法里面.构造方法在下面代码.
EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));
loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);
}
return loader;
}
private ExtensionLoader(Class> type) {
this.type = type;
//第一次使用ExtensionLoader,先创建ExtensionFactory接口工厂的ExtensionLoader.
//然后调用ExtensionLoader#getAdaptiveExtension()获取ExtensionFactory的实例.
objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
}
2.获取扩展接口的实例
入口ExtensionLoader#getAdaptiveExtension().
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;
}
创建扩展接口的实例
入口 ExtensionLoader#createAdaptiveExtension
private T createAdaptiveExtension() {
try {
/* 先调用getAdaptiveExtensionClass()获取接口实际使用的实现类,代码如下.
再调用newInstance()创建这个实现类的对象
最后调用injectExtension()对这个对象的属性进行注入.
对于Protocol接口,这里创建动态生成的Protocol$Adaptive类的实例 */
return injectExtension((T) getAdaptiveExtensionClass().newInstance());
} catch (Exception e) {
throw new IllegalStateException("Can not create adaptive extension " + type + ", cause: " + e.getMessage(), e);
}
}
获取接口的扩展类
入口ExtensionLoader#getAdaptiveExtensionClass()
private Class> getAdaptiveExtensionClass() {
//获取扩展类
getExtensionClasses();
if (cachedAdaptiveClass != null) {
//如果有带Adaptive注解的,优先返回有带Adaptive注解的实现类
return cachedAdaptiveClass;
}
//Protocol接口的实现类没有@Adaptive注解的,进到这里,在后面6处分析
return cachedAdaptiveClass = createAdaptiveExtensionClass();
}
方法ExtensionLoader#getExtensionClasses()获取接口的所有实现类
private Map> getExtensionClasses() {
Map> classes = cachedClasses.get();
if (classes == null) {
synchronized (cachedClasses) {
classes = cachedClasses.get();
if (classes == null) {
//读取扩展接口文件,加载,代码在下面
classes = loadExtensionClasses();
//对于ExtensionFactory,这里有两个,SpiExtensionFactory和SpringExtensionFactory.
cachedClasses.set(classes);
}
}
}
return classes;
}
3.读取spi文件的接口的所有的实现类
如果是protocol协议,这里能获取到{RegistryProtocol,InjvmProtocol,ThriftProtocol,DubboProtocol,MockProtocol,HttpProtocol,RedisProtocol,RmiProtocol}八个实现类,默认使用DubboProtocol.
入口是ExtensionLoader#loadExtensionClasses()
private Map> loadExtensionClasses() {
//获取接口的spi注解,比如ExtensionFactory,Protocol
final SPI defaultAnnotation = type.getAnnotation(SPI.class);
if (defaultAnnotation != null) {
//获取扩展默认加载哪个实现类.
String value = defaultAnnotation.value();
if (value != null && (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> extensionClasses = new HashMap>();
//加载META-INF/dubbo/internal/目录下的扩展文件
loadFile(extensionClasses, DUBBO_INTERNAL_DIRECTORY);
//加载META-INF/dubbo/目录下的扩展文件
loadFile(extensionClasses, DUBBO_DIRECTORY);
//加载META-INF/services/目录下的扩展文件
loadFile(extensionClasses, SERVICES_DIRECTORY);
return extensionClasses;
}
读取文件,入口是ExtensionLoader#loadFile().加载到的实现类都放到extensionClasses.key是别名,value是实现类.
private void loadFile(Map> extensionClasses, String dir) {
//格式是META-INF/dubbo/internal/com.alibaba.dubbo.common.extension.ExtensionFactory
//目录名+接口路径文件名
String fileName = dir + type.getName();
try {
Enumeration urls;
//调用ExtensionLoader.class.getClassLoader()获取ClassLoader
ClassLoader classLoader = findClassLoader();
if (classLoader != null) {
//获取文件路径的url格式
urls = classLoader.getResources(fileName);
} else {
urls = ClassLoader.getSystemResources(fileName);
}
if (urls != null) {
//遍历所有文件路径的url
while (urls.hasMoreElements()) {
/* 接口文件的本地磁盘路径file:/xxx/dubbo-config/xxx/internal/xxx.ExtensionFactory格式 */
java.net.URL url = urls.nextElement();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
try {
/* 一行行遍历接口文件,对于ExtensionFactory接口,在dubbo-config只有下面一行 spring=com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory
在dubbo-common下面有adaptive=com.alibaba.dubbo.common.extension.factory.AdaptiveExtensionFactory
spi=com.alibaba.dubbo.common.extension.factory.SpiExtensionFactory两行 */
String line = null;
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别名部分
name = line.substring(0, i).trim();
//接口实现类赋值给line,这里是SpringExtensionFactory类全路径
line = line.substring(i + 1).trim();
}
if (line.length() > 0) {
//创建实现类的Class对象
Class> clazz = Class.forName(line, true, classLoader);
//判断实现类是否是该接口的
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.");
}
//实现类上是否有Adaptive注解
if (clazz.isAnnotationPresent(Adaptive.class)) {
if (cachedAdaptiveClass == null) {
//有Adaptive注解的放到成员cachedAdaptiveClass里面,这个优先级最高
cachedAdaptiveClass = clazz;
} else if (!cachedAdaptiveClass.equals(clazz)) {
throw new IllegalStateException("More than 1 adaptive class found: "
+ cachedAdaptiveClass.getClass().getName()
+ ", " + clazz.getClass().getName());
}
} else {
try {
//实现类是否有参数为本接口类型的构造方法,这样后面可以把多个实现类链接起来
clazz.getConstructor(type);
Set> wrappers = cachedWrapperClasses;
if (wrappers == null) {
cachedWrapperClasses = new ConcurrentHashSet>();
wrappers = cachedWrapperClasses;
}
wrappers.add(clazz);
} catch (NoSuchMethodException e) {
//实现类是否有无参构造方法
clazz.getConstructor();
if (name == null || name.length() == 0) {
name = findAnnotationName(clazz);
if (name == null || name.length() == 0) {
if (clazz.getSimpleName().length() > type.getSimpleName().length()
&& clazz.getSimpleName().endsWith(type.getSimpleName())) {
name = clazz.getSimpleName().substring(0, clazz.getSimpleName().length() - type.getSimpleName().length()).toLowerCase();
} else {
throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + url);
}
}
}
//做正则表达式匹配name,对于SpringExtensionFactory,这里是spring
String[] names = NAME_SEPARATOR.split(name);
if (names != null && names.length > 0) {
Activate activate = clazz.getAnnotation(Activate.class);
if (activate != null) {
//如果实现类有Activate注解,加入cachedActivates缓存.
cachedActivates.put(names[0], activate);
}
for (String n : names) {
if (!cachedNames.containsKey(clazz)) {
//加入已经解析的名称缓存cachedNames
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());
}
}
}
}
}
}
} catch (Throwable t) {
...
}
}
} // end of while read lines
} finally {
//关闭读文件句柄
reader.close();
}
} catch (Throwable t) {
...
}
} // end of while urls
}
} catch (Throwable t) {
...
}
}
4.创建接口的实现类的对象,以ExtensionFactory为例,实现类是AdaptiveExtensionFactory,它的优先级最高,带有@Adaptive注解.代码如下:
public AdaptiveExtensionFactory() {
//进到这里,可以从缓存直接拿到了,因为是调用ExtensionLoader.getExtensionLoader(ExtensionFactory.class)才到这里
ExtensionLoader loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
List list = new ArrayList();
//遍历所有ExtensionFactory接口的实现类,这里与两个,spi和spring
for (String name : loader.getSupportedExtensions()) {
/* 创建所有ExtensionFactory接口实现类的对象,这里先去创建SpiExtensionFactory类的对象,然后调用ExtensionLoader#injectExtension()方法注入对象 */
list.add(loader.getExtension(name));
}
factories = Collections.unmodifiableList(list);
}
ExtensionLoader#getExtension()调用ExtensionLoader#createExtension()完成操作.代码如下:
比如传入的name是"spi",
private T createExtension(String name) {
//获取"spi"的实现类SpiExtensionFactory
Class> clazz = getExtensionClasses().get(name);
if (clazz == null) {
throw findException(name);
}
try {
T instance = (T) EXTENSION_INSTANCES.get(clazz);
//第一次获取为空
if (instance == null) {
//创建实现类的对象,比如创建SpiExtensionFactory类的对象
EXTENSION_INSTANCES.putIfAbsent(clazz, (T) clazz.newInstance());
instance = (T) EXTENSION_INSTANCES.get(clazz);
}
//对对象进行注入,比如对SpiExtensionFactory对象进行注入,主要是调用它的setxxx方法.
injectExtension(instance);
Set> wrapperClasses = cachedWrapperClasses;
if (wrapperClasses != null && wrapperClasses.size() > 0) {
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);
}
}
5.ioc,注入对象属性
private T injectExtension(T instance) {
try {
if (objectFactory != null) {
for (Method method : instance.getClass().getMethods()) {
//只处理setxxx()方法,方法参数只有一个
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) : "";
/* 调用ExtensionFactory#getExtension()获取要注入的参数的对象实例,property是参数名,主要是去容器中获取bean的实例对象,比如spring的singleObjects*/
Object object = objectFactory.getExtension(pt, property);
if (object != null) {
//调用setxxx()方法注入参数对象
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;
}
6.创建自适应的扩展类,在接口的多个实现类中选一个.
从前面的2调用过来,比如Protocol接口
private Class> createAdaptiveExtensionClass() {
//代码在下面分析,Protocol这里返回Protocol$Adaptive类的实现代码字串
String code = createAdaptiveExtensionClassCode();
//获取当前类的ClassLoader
ClassLoader classLoader = findClassLoader();
//获取Compiler接口的实现类JavassistCompiler
com.alibaba.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.compiler.Compiler.class).getAdaptiveExtension();
//调用Compiler接口的实现类JavassistCompiler把上面的字符串代码编译成Class
return compiler.compile(code, classLoader);
}
创建扩展类的字符串代码最后生成的字符串如下图,
代码如下:
private String createAdaptiveExtensionClassCode() {
StringBuilder codeBuidler = new StringBuilder();
//获取接口内部的类,比如Protocol接口的类
Method[] methods = type.getMethods();
boolean hasAdaptiveAnnotation = false;
//遍历所有方法,判断是否有带@Adaptive注解的方法
for (Method m : methods) {
if (m.isAnnotationPresent(Adaptive.class)) {
hasAdaptiveAnnotation = true;
break;
}
}
// no need to generate adaptive class since there's no adaptive method found.
//所有类都没有adaptive方法,则抛出异常
if (!hasAdaptiveAnnotation)
throw new IllegalStateException("No adaptive method on extension " + type.getName() + ", refuse to create the adaptive class!");
codeBuidler.append("package " + type.getPackage().getName() + ";");
codeBuidler.append("\nimport " + ExtensionLoader.class.getName() + ";");
codeBuidler.append("\npublic class " + type.getSimpleName() + "$Adaptive" + " implements " + type.getCanonicalName() + " {");
for (Method method : methods) {
//返回值类型
Class> rt = method.getReturnType();
//参数类型
Class>[] pts = method.getParameterTypes();
//方法抛出的异常类型
Class>[] ets = method.getExceptionTypes();
//获取方法上的Adaptive注解
Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class);
StringBuilder code = new StringBuilder(512);
if (adaptiveAnnotation == null) {
//没有Adaptive注解
code.append("throw new UnsupportedOperationException(\"method ")
.append(method.toString()).append(" of interface ")
.append(type.getName()).append(" is not adaptive method!\");");
} else {
//有Adaptive注解的
int urlTypeIndex = -1;
//取出参数类型是url的参数位置索引
for (int i = 0; i < pts.length; ++i) {
if (pts[i].equals(URL.class)) {
urlTypeIndex = i;
break;
}
}
// found parameter in URL type
if (urlTypeIndex != -1) {
//参数中有url类型的
// Null Point check
String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"url == null\");",
urlTypeIndex);
code.append(s);
s = String.format("\n%s url = arg%d;", URL.class.getName(), urlTypeIndex);
code.append(s);
}
// did not find parameter in URL type
else {
//参数中没有url类型的,比如Protocol#export(Invoker invoker).没有url参数,带有@Adaptive注解,返回Exporter
String attribMethod = null;
// find URL getter method
//遍历所有参数
LBL_PTS:
for (int i = 0; i < pts.length; ++i) {
Method[] ms = pts[i].getMethods();
//比如获取到Invoker类型的参数,遍历Invoker的所有成员方法
//找到一个getxxx方法,返回值是URL类型,记下方法名.
for (Method m : ms) {
String name = m.getName();
if ((name.startsWith("get") || name.length() > 3)
&& Modifier.isPublic(m.getModifiers())
&& !Modifier.isStatic(m.getModifiers())
&& m.getParameterTypes().length == 0
&& m.getReturnType() == URL.class) {
urlTypeIndex = i;
attribMethod = name;
break LBL_PTS;
}
}
}
...
}
//获取方法上@Adaptive注解的value值
String[] value = adaptiveAnnotation.value();
// value is not set, use the value generated from class name as the key
if (value.length == 0) {
//没有值的话,默认取dubbo接口的名称,比如protocol
char[] charArray = type.getSimpleName().toCharArray();
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < charArray.length; i++) {
if (Character.isUpperCase(charArray[i])) {
if (i != 0) {
sb.append(".");
}
sb.append(Character.toLowerCase(charArray[i]));
} else {
sb.append(charArray[i]);
}
}
value = new String[]{sb.toString()};
}
//获取方法的所有参数,看是否有Invocation类型.
boolean hasInvocation = false;
for (int i = 0; i < pts.length; ++i) {
if (pts[i].getName().equals("com.alibaba.dubbo.rpc.Invocation")) {
...
}
}
//获取接口的默认扩展的实现名
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);
}
}
...
}
...
}
codeBuidler.append("\n}");
return codeBuidler.toString();
}