上一篇文章已经介绍了jdk SPI机制的源码,Dubbo也采用SPI机制进行接口服务的扩展ExtensionLoader,不过采用了不同的实现方式,相比于jdk提供的ServiceLoader复杂的多,丰富了以下几个功能。
1.自动注入依赖的扩展类
2.自动包装扩展类
3.增加注解SPI,提供默认实现类
4.提供注解Adaptive,采用javassist动态生成代码,默认实现为Adaptive instance
获取ExtensionFactory扩展工厂实例
ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()
获取Protocol协议实例,得到它的默认端口
int defaultPort = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(name).getDefaultPort();
静态方法创建ExtensionLoader
public static ExtensionLoader getExtensionLoader(Class type) {
// 接口非空判断
if (type == null) {
throw new IllegalArgumentException("Extension type == null");
}
// 一定是接口
if (!type.isInterface()) {
throw new IllegalArgumentException("Extension type (" + type + ") is not an interface!");
}
// class类上有@SPI注解
if (!withExtensionAnnotation(type)) {
throw new IllegalArgumentException("Extension type (" + type +
") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!");
}
// 获取静态map缓存中的ExtensionLoader实例对象
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;
}
构造方法为私有方法,通过静态方法创建.此构造方法内部设置了私有属性ExtensionFactory,并且这个ExtensionFactory也是通过ExtensionLoader获取的,此ExtensionFactory的作用给通过ExtensionLoader创建的service provider自动注入相关属性,具体代码逻辑稍后分析。
private ExtensionLoader(Class> type) {
this.type = type;
// 设置私有属性objectFactory
objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
}
ExtensionFactory接口上标有@SPI注解,有如下3个实现类,其中AdaptiveExtensionFactory类上标有@Adaptive注解,是默认的Adaptive实现类 。
@SPI
public interface ExtensionFactory {
T getExtension(Class type, String name);
}
SpiExtensionFactory是获取type类型的AdaptiveExtension实现类
public class SpiExtensionFactory implements ExtensionFactory {
@Override
public T getExtension(Class type, String name) {
if (type.isInterface() && type.isAnnotationPresent(SPI.class)) {
ExtensionLoader loader = ExtensionLoader.getExtensionLoader(type);
if (!loader.getSupportedExtensions().isEmpty()) {
return loader.getAdaptiveExtension();
}
}
return null;
}
}
SpiExtensionFactory是获取type类型的AdaptiveExtension实现类,主要目的是通过springContext中获取bean的实例,
静态方法addApplicationContext在spring启动时ServiceBean,ReferenceBean,ConfigCenterBean的初始化,实现ApplicationContextAware接口调用的。
public class SpringExtensionFactory implements ExtensionFactory {
private static final Logger logger = LoggerFactory.getLogger(SpringExtensionFactory.class);
private static final Set CONTEXTS = new ConcurrentHashSet();
private static final ApplicationListener SHUTDOWN_HOOK_LISTENER = new ShutdownHookListener();
// 静态方法添加context
public static void addApplicationContext(ApplicationContext context) {
CONTEXTS.add(context);
if (context instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) context).registerShutdownHook();
DubboShutdownHook.getDubboShutdownHook().unregister();
}
// 注册shutdown_hook_listener
BeanFactoryUtils.addApplicationListener(context, SHUTDOWN_HOOK_LISTENER);
}
@Override
public T getExtension(Class type, String name) {
//SPI should be get from SpiExtensionFactory
if (type.isInterface() && type.isAnnotationPresent(SPI.class)) {
return null;
}
// 遍历springContext通过name获取spring中bean的实例
for (ApplicationContext context : CONTEXTS) {
if (context.containsBean(name)) {
Object bean = context.getBean(name);
if (type.isInstance(bean)) {
return (T) bean;
}
}
}
logger.warn("No spring extension (bean) named:" + name + ", try to find an extension (bean) of type " + type.getName());
if (Object.class == type) {
return null;
}
// 通过name没有获取到,就通过接口类型获取bean
for (ApplicationContext context : CONTEXTS) {
try {
return context.getBean(type);
} catch (NoUniqueBeanDefinitionException multiBeanExe) {
logger.warn("Find more than 1 spring extensions (beans) of type " + type.getName() + ", will stop auto injection. Please make sure you have specified the concrete parameter type and there's only one extension of that type.");
} catch (NoSuchBeanDefinitionException noBeanExe) {
if (logger.isDebugEnabled()) {
logger.debug("Error when get spring extension(bean) for type:" + type.getName(), noBeanExe);
}
}
}
logger.warn("No spring extension (bean) named:" + name + ", type:" + type.getName() + " found, stop get bean.");
return null;
}
// 注册spring ApplicationEvent事件监听器,当发生 ContextClosed事件时,销毁所有注册,销毁所有协议
private static class ShutdownHookListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextClosedEvent) {
DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
shutdownHook.doDestroy();
}
}
}
}
AdaptiveExtensionFactory类上标有@Adaptive注解,是ExtensionFactory的默认provider,它是一个组合类,获取所有ExtensionFactory的provider,getExtension方法是遍历所有的ExtensionFactory,获取bean。
@Adaptive
public class AdaptiveExtensionFactory implements ExtensionFactory {
private final List factories;
public AdaptiveExtensionFactory() {
ExtensionLoader loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
List list = new ArrayList();
for (String name : loader.getSupportedExtensions()) {
list.add(loader.getExtension(name));
}
factories = Collections.unmodifiableList(list);
}
@Override
public T getExtension(Class type, String name) {
for (ExtensionFactory factory : factories) {
T extension = factory.getExtension(type, name);
if (extension != null) {
return extension;
}
}
return null;
}
}
分析完了ExtensionFactory,让我们回到ExtensionLoader中的getAdaptiveExtension方法,此方法先从缓存中获取adaptiveInstance实例,获取不到在双层判断同步代码块,线程安全方式创建adaptiveExtension,并设置进入缓存。
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("Failed to create adaptive instance: " + t.toString(), t);
}
}
}
} else {
throw new IllegalStateException("Failed to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
}
}
return (T) instance;
}
createAdaptiveExtension方法先获取adaptiveExtensionClass并实例化,然后再调用injectExtension注入extension属性
private T createAdaptiveExtension() {
try {
return injectExtension((T) getAdaptiveExtensionClass().newInstance());
} catch (Exception e) {
throw new IllegalStateException("Can't create adaptive extension " + type + ", cause: " + e.getMessage(), e);
}
}
我们首先来看injectExtension方法,获取的instance,通过java的反射机制遍历class的Method,如果是setter方法,
并且没有标注@DisableInject注解,则通过我们上面分析的ExtensionFactory注入属性,可以注入springContext中的bean和通过ExtensionLoader加载的adaptiveInstance。
private T injectExtension(T instance) {
try {
if (objectFactory != null) {
for (Method method : instance.getClass().getMethods()) {
if (isSetter(method)) {
/**
* Check {@link DisableInject} to see if we need auto injection for this property
*/
if (method.getAnnotation(DisableInject.class) != null) {
continue;
}
Class> pt = method.getParameterTypes()[0];
if (ReflectUtils.isPrimitives(pt)) {
continue;
}
try {
String property = getSetterProperty(method);
Object object = objectFactory.getExtension(pt, property);
if (object != null) {
method.invoke(instance, object);
}
} catch (Exception e) {
logger.error("Failed to inject via method " + method.getName()
+ " of interface " + type.getName() + ": " + e.getMessage(), e);
}
}
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return instance;
}
然后我们来看getAdaptiveExtensionClass方法,此方法返回最适配的class,首先调用getExtensionClasses方法,此方法
和jdk提供的ServiceLoader相似,都是从配置文件中找到对应的class类并加载至jvm。不过加载的过程中如果发现有的provider
上标有@Adaptive注解,则缓存至cachedAdaptiveClass属性中。此方法首先获取标有@Adaptive的class,如果没有则通过javasist动态生成代码。
private Class> getAdaptiveExtensionClass() {
getExtensionClasses();
if (cachedAdaptiveClass != null) {
return cachedAdaptiveClass;
}
return cachedAdaptiveClass = createAdaptiveExtensionClass();
}
我们先来看getExtensionClasses方法,此方法即是对class的缓存设置,获取加载class逻辑在loadExtensionClasses中。
private Map> getExtensionClasses() {
Map> classes = cachedClasses.get();
if (classes == null) {
synchronized (cachedClasses) {
classes = cachedClasses.get();
if (classes == null) {
classes = loadExtensionClasses();
cachedClasses.set(classes);
}
}
}
return classes;
}
loadExtensionClasses方法即是从META-INF/dubbo/internal,META-INF/dubbo,META-INF/services/等多处目录中加载class全类名。
private Map> loadExtensionClasses() {
// 缓存默认的name
cacheDefaultExtensionName();
Map> extensionClasses = new HashMap<>();
loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName());
loadDirectory(extensionClasses, DUBBO_INTERNAL_DIRECTORY, type.getName().replace("org.apache", "com.alibaba"));
loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName());
loadDirectory(extensionClasses, DUBBO_DIRECTORY, type.getName().replace("org.apache", "com.alibaba"));
loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName());
loadDirectory(extensionClasses, SERVICES_DIRECTORY, type.getName().replace("org.apache", "com.alibaba"));
return extensionClasses;
}
下面是loadDirectory,loadResource和loadClass方法,和JDK的ServiceLoader获取资源并解析一样,这里不做一一分析。不过需要特别注意的是cacheAdaptiveClass(clazz)和cacheWrapperClass(clazz)方法。
cacheAdaptiveClass是缓存provider类上有@Adaptive注解的class,和cacheWrapperClass作用是缓存构造函数的参数是当前type的包装类class。
private void loadDirectory(Map> extensionClasses, String dir, String type) {
String fileName = dir + type;
try {
Enumeration 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 occurred when loading extension class (interface: " +
type + ", description file: " + fileName + ").", t);
}
}
private void loadResource(Map> extensionClasses, ClassLoader classLoader, java.net.URL resourceURL) {
try {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), StandardCharsets.UTF_8))) {
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);
}
}
}
}
} catch (Throwable t) {
logger.error("Exception occurred when loading extension class (interface: " +
type + ", class file: " + resourceURL + ") in " + resourceURL, t);
}
}
private void loadClass(Map> extensionClasses, java.net.URL resourceURL, Class> clazz, String name) throws NoSuchMethodException {
if (!type.isAssignableFrom(clazz)) {
throw new IllegalStateException("Error occurred when loading extension class (interface: " +
type + ", class line: " + clazz.getName() + "), class "
+ clazz.getName() + " is not subtype of interface.");
}
if (clazz.isAnnotationPresent(Adaptive.class)) {
cacheAdaptiveClass(clazz);
} else if (isWrapperClass(clazz)) {
cacheWrapperClass(clazz);
} else {
clazz.getConstructor();
if (StringUtils.isEmpty(name)) {
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 (ArrayUtils.isNotEmpty(names)) {
cacheActivateClass(clazz, names[0]);
for (String n : names) {
cacheName(clazz, n);
saveInExtensionClass(extensionClasses, clazz, n);
}
}
}
}
下面我们回到getAdaptiveExtensionClass的createAdaptiveExtensionClass方法上,如果没有默认的标有@Adaptive注解的provider类,则通过createAdaptiveExtensionClass动态生成java代码,并通过Compiler(此为JavassistCompiler)进行动态编译,代码实例下一节中描述,感兴趣的童鞋欢迎点开下一节内容。
private Class> createAdaptiveExtensionClass() {
String code = new AdaptiveClassCodeGenerator(type, cachedDefaultName).generate();
ClassLoader classLoader = findClassLoader();
org.apache.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(org.apache.dubbo.common.compiler.Compiler.class).getAdaptiveExtension();
return compiler.compile(code, classLoader);
}