1.ScopeModel
抽象这三个能力是为了实现 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));
}
}
相关子类初始化时机:
- @EnableDubbo注解通过import方式引入类DubboConfigConfigurationRegistrar、DubboComponentScanRegistrar。
- 接口ImportBeanDefinitionRegistrar子类核心方法内部通过类DubboSpringInitializer触发ScopeModel相关子类。
- FrameworkModel构造器中初始化ApplicationModel,ApplicationModel构造器中初始化ModuleModel。
- FrameworkModel、ApplicationModel、ModuleModel三者之间并没有Java编程语法意义上的继承关系,但是是通过父类ScopeModel的parent属性建立父子关系。
类ExtensionDirector作用:子类加载扩展类就是利用该类得到类ExtensionLoader完成SPI功能的。
1.1.FrameworkModel
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<ScopeModelInitializer> initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
Set<ScopeModelInitializer> 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);
}
}
1.2.ApplicationModel
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<ApplicationInitListener> extensionLoader = this.getExtensionLoader(ApplicationInitListener.class);
Set<String> listenerNames = extensionLoader.getSupportedExtensions();
for (String listenerName : listenerNames) {
extensionLoader.getExtension(listenerName).init();
}
initApplicationExts();
// 加载 ScopeModelInitializer类型的 扩展类
ExtensionLoader<ScopeModelInitializer> initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
Set<ScopeModelInitializer> initializers = initializerExtensionLoader.getSupportedExtensionInstances();
for (ScopeModelInitializer initializer : initializers) {
// 马上执行扩展类的核心方法
initializer.initializeApplicationModel(this);
}
}
}
1.3.ModuleModel
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<ScopeModelInitializer> initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
Set<ScopeModelInitializer> initializers = initializerExtensionLoader.getSupportedExtensionInstances();
for (ScopeModelInitializer initializer : initializers) {
// 马上执行扩展类的核心方法
initializer.initializeModuleModel(this);
}
}
}
2.ExtensionAccessor
利用Spi机制获取扩展类都是通过当前类先获取ExtensionLoader,后续通过ExtensionLoader加载对应的扩展类。
获取ExtensionLoader存在两种方式:
- 通过ExtensionDirector直接获取。
- 通过ExtensionAccessor间接获取。这种方式其实是通过ScopeModel间接获取到ExtensionDirector,其目的是利用ScopeModel初始化ExtensionDirector的parent属性。
如果是通过 ScopeModel 的子类获取扩展类,则必然是通过第二种方式获取ExtensionLoader。
public interface ExtensionAccessor {
// 由 ExtensionDirector 以及 ScopeModel 类实现该方法
ExtensionDirector getExtensionDirector();
default <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
return this.getExtensionDirector().getExtensionLoader(type);
}
//支持获取特定name对应的扩展类
default <T> T getExtension(Class<T> type, String name) {
ExtensionLoader<T> extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getExtension(name) : null;
}
default <T> T getAdaptiveExtension(Class<T> type) {
ExtensionLoader<T> extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getAdaptiveExtension() : null;
}
default <T> T getDefaultExtension(Class<T> type) {
ExtensionLoader<T> extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getDefaultExtension() : null;
}
}
内部方法参数type类型必须是被@SPI注解的类。
FrameworkModel、ApplicationModel、ModuleModel三者抽象父类ScopeModel实现了方法:
public interface ExtensionAccessor {
ExtensionDirector getExtensionDirector();
}
2.1.ExtensionDirector
ExtensionDirector的作用其实就是获取扩展类的加载器ExtensionLoader。其实任何方式得到的扩展类的加载器都是ExtensionLoader,唯一的区别就是parent属性赋值与否。
ExtensionScope的取值涉及:FRAMEWORK、APPLICATION、MODULE、SELF。
public class ExtensionDirector implements ExtensionAccessor {
public <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
...
// 1. find in local cache
ExtensionLoader<T> loader = (ExtensionLoader<T>) 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;
}
}
2.2.ExtensionLoader
2.3.LoadingStrategy
DubboInternalLoadingStrategy:META-INF/dubbo/internal/。
DubboLoadingStrategy:META-INF/dubbo/。
ServicesLoadingStrategy:META-INF/services/。