抽象这三个能力是为了实现 Dubbo 的多实例支持,FrameworkModel 是实现类似 JVM 租户级别的隔离,ApplicationModel 是为了实现一个机器上发布多个应用(如 demo-application1 和 demo-application2 一起发布),ModuleModel 是为了实现服务生命周期的独立管理(如一个 demo-application 可以由多个 Spring 容器共同提供)。
public class ModuleModel extends ScopeModel {
private final ScopeModel parent;
private final ExtensionScope scope;
public ScopeModel(ScopeModel parent, ExtensionScope scope, boolean isInternal) {
this.parent = parent;
this.scope = scope;
this.internalScope = isInternal;
}
protected void initialize() {
this.extensionDirector = new ExtensionDirector(parent != null ? parent.getExtensionDirector() : null, scope, this);
this.extensionDirector.addExtensionPostProcessor(new ScopeModelAwareExtensionProcessor(this));
}
}
相关子类初始化时机:
类ExtensionDirector作用:子类加载扩展类就是利用该类得到类ExtensionLoader完成SPI功能的。
public class FrameworkModel extends ScopeModel {
public FrameworkModel() {
super(null, ExtensionScope.FRAMEWORK, false);
...
initialize();
}
@Override
protected void initialize() {
super.initialize();
TypeDefinitionBuilder.initBuilders(this);
serviceRepository = new FrameworkServiceRepository(this);
// 获取 ScopeModelInitializer 类型的扩展类
ExtensionLoader initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
Set initializers = initializerExtensionLoader.getSupportedExtensionInstances();
for (ScopeModelInitializer initializer : initializers) {
initializer.initializeFrameworkModel(this);
}
// 将当前类作为 ApplicationModel 的父类
internalApplicationModel = new ApplicationModel(this, true);
internalApplicationModel.getApplicationConfigManager().setApplication(new ApplicationConfig(internalApplicationModel, CommonConstants.DUBBO_INTERNAL_APPLICATION));
internalApplicationModel.setModelName(CommonConstants.DUBBO_INTERNAL_APPLICATION);
}
}
public class ApplicationModel extends ScopeModel {
public ApplicationModel(FrameworkModel frameworkModel, boolean isInternal) {
super(frameworkModel, ExtensionScope.APPLICATION, isInternal);
this.frameworkModel = frameworkModel;
frameworkModel.addApplication(this);
initialize();
}
@Override
protected void initialize() {
super.initialize();
// 将当前类作为 ModuleModel 的父类
internalModule = new ModuleModel(this, true);
this.serviceRepository = new ServiceRepository(this);
// 加载 ApplicationInitListener类型 的扩展类
ExtensionLoader extensionLoader = this.getExtensionLoader(ApplicationInitListener.class);
Set listenerNames = extensionLoader.getSupportedExtensions();
for (String listenerName : listenerNames) {
extensionLoader.getExtension(listenerName).init();
}
initApplicationExts();
// 加载 ScopeModelInitializer类型的 扩展类
ExtensionLoader initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
Set initializers = initializerExtensionLoader.getSupportedExtensionInstances();
for (ScopeModelInitializer initializer : initializers) {
// 马上执行扩展类的核心方法
initializer.initializeApplicationModel(this);
}
}
}
public class ModuleModel extends ScopeModel {
public ModuleModel(ApplicationModel applicationModel, boolean isInternal) {
super(applicationModel, ExtensionScope.MODULE, isInternal);
this.applicationModel = applicationModel;
applicationModel.addModule(this, isInternal);
initialize();
}
@Override
protected void initialize() {
super.initialize();
this.serviceRepository = new ModuleServiceRepository(this);
this.moduleConfigManager = new ModuleConfigManager(this);
this.moduleConfigManager.initialize();
initModuleExt();
// 加载 ScopeModelInitializer类型的扩展类
ExtensionLoader initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
Set initializers = initializerExtensionLoader.getSupportedExtensionInstances();
for (ScopeModelInitializer initializer : initializers) {
// 马上执行扩展类的核心方法
initializer.initializeModuleModel(this);
}
}
}
利用Spi机制获取扩展类都是通过当前类先获取ExtensionLoader,后续通过ExtensionLoader加载对应的扩展类。
获取ExtensionLoader存在两种方式:
如果是通过 ScopeModel 的子类获取扩展类,则必然是通过第二种方式获取ExtensionLoader。
public interface ExtensionAccessor {
// 由 ExtensionDirector 以及 ScopeModel 类实现该方法
ExtensionDirector getExtensionDirector();
default ExtensionLoader getExtensionLoader(Class type) {
return this.getExtensionDirector().getExtensionLoader(type);
}
//支持获取特定name对应的扩展类
default T getExtension(Class type, String name) {
ExtensionLoader extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getExtension(name) : null;
}
default T getAdaptiveExtension(Class type) {
ExtensionLoader extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getAdaptiveExtension() : null;
}
default T getDefaultExtension(Class type) {
ExtensionLoader extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getDefaultExtension() : null;
}
}
内部方法参数type类型必须是被@SPI注解的类。
FrameworkModel、ApplicationModel、ModuleModel三者抽象父类ScopeModel实现了方法:
public interface ExtensionAccessor {
ExtensionDirector getExtensionDirector();
}
ExtensionDirector的作用其实就是获取扩展类的加载器ExtensionLoader。其实任何方式得到的扩展类的加载器都是ExtensionLoader,唯一的区别就是parent属性赋值与否。
ExtensionScope的取值涉及:FRAMEWORK、APPLICATION、MODULE、SELF。
public class ExtensionDirector implements ExtensionAccessor {
public ExtensionLoader getExtensionLoader(Class type) {
...
// 1. find in local cache
ExtensionLoader loader = (ExtensionLoader) extensionLoadersMap.get(type);
ExtensionScope scope = extensionScopeMap.get(type);
if (scope == null) {
SPI annotation = type.getAnnotation(SPI.class);
scope = annotation.scope();
extensionScopeMap.put(type, scope);
}
// 如果 type 的scope范围是SELF,则直接返回 ExtensionLoader,并不需要考虑 属性parent的功能
if (loader == null && scope == ExtensionScope.SELF) {
// create an instance in self scope
loader = createExtensionLoader0(type);
}
// 2. find in parent
if (loader == null) {
// 条件成立的条件是:必须是通过ScopeModel子类触发的扩展类加载
if (this.parent != null) {
// 递归的方式
loader = this.parent.getExtensionLoader(type);
}
}
// 3. create it
if (loader == null) {
loader = createExtensionLoader(type);
}
return loader;
}
}
DubboInternalLoadingStrategy:META-INF/dubbo/internal/。
DubboLoadingStrategy:META-INF/dubbo/。
ServicesLoadingStrategy:META-INF/services/。