本文通過(guò)分析 Dubbo 服務(wù)暴露過(guò)程中使用到的3個(gè) SPI 加載的類 ExtensionFactory
、ProxyFactory
最冰、Protocol
來(lái)理解 SPI 的靈活加載邏輯洁灵。
以最簡(jiǎn)單的暴露服務(wù)到 jvm 為例,下面是位于 org.apache.dubbo.config.ServiceConfig
的關(guān)鍵源代碼
// PROTOCOL 的動(dòng)態(tài)類
private static final Protocol PROTOCOL = (Protocol)ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
// PROXY_FACTORY 的動(dòng)態(tài)類
private static final ProxyFactory PROXY_FACTORY = (ProxyFactory)ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
// 暴露服務(wù)到本地 jvm 中
private void exportLocal(URL url) {
URL local = URLBuilder.from(url).setProtocol("injvm").setHost("127.0.0.1").setPort(0).build();
Exporter<?> exporter = PROTOCOL.export(PROXY_FACTORY.getInvoker(this.ref, this.interfaceClass, local));
// ...
}
獲取 AdaptiveClass
類中兩個(gè)靜態(tài)變量 PROXY_FACTORY
或 PROTOCOL
使用了相同的方法初始化柱锹。
- 獲取指定類的 ExtensionLoader 實(shí)例
- 通過(guò) ExtensionLoader 實(shí)例調(diào)用 getAdaptiveExtension() 方法獲取指定類的可擴(kuò)展類闽巩。
獲取指定 type 的 ExtensionLoader 方法如下:
public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
if (type == null) {
throw new IllegalArgumentException("Extension type == null");
} else if (!type.isInterface()) {
throw new IllegalArgumentException("Extension type (" + type + ") is not an interface!");
} else if (!withExtensionAnnotation(type)) {
throw new IllegalArgumentException("Extension type (" + type + ") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!");
} else {
// 先查看是否已經(jīng)生成過(guò)
ExtensionLoader<T> loader = (ExtensionLoader)EXTENSION_LOADERS.get(type);
if (loader == null) {
// 當(dāng) type=ProxyFactory 時(shí),首次進(jìn)入這里會(huì)初始化 ProxyFactory 類的 ExtensionLoader 實(shí)例
// 并把實(shí)例都會(huì)緩存到靜態(tài)變量中
EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));
loader = (ExtensionLoader)EXTENSION_LOADERS.get(type);
}
return loader;
}
}
關(guān)鍵是使用 ExtensionLoader
類的構(gòu)造函數(shù)進(jìn)行實(shí)例化的流椒。
private ExtensionLoader(Class<?> type) {
this.type = type;
// 當(dāng)前 type=ProxyFactory署惯, 它的 objectFactory 為 ExtensionFactory 類的動(dòng)態(tài)類
// 當(dāng) type=ExtensionFactory 時(shí),它的 objectFactory 為 null
this.objectFactory = type == ExtensionFactory.class ? null : (ExtensionFactory)getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension();
}
objectFactory
是用來(lái)在注入依賴的時(shí)候镣隶,獲取依賴屬性的對(duì)象工廠。它是 ExtensionFactory
類的 AdaptiveClass诡右。
獲取 ExtensionLoader的 AdaptiveClass安岂,它的流程比較長(zhǎng),如下圖:
最終是在項(xiàng)目的 META-INF 指定文件夾中查找到 type 配置的所有實(shí)現(xiàn)類帆吻,獲取到的 AdaptiveClass 有兩種情況:
- Type 接口的實(shí)現(xiàn)類上有
@Adaptive
注解域那,則直接返回有此注解的類,如 type=ExtensionFactory 的實(shí)現(xiàn)類AdaptiveExtensionFactory
猜煮。 - Type 接口的實(shí)現(xiàn)類上均無(wú)
@Adaptive
注解次员,但是實(shí)現(xiàn)類的方法上有此注解,則會(huì)在有注解的方法上動(dòng)態(tài)生成實(shí)現(xiàn)類王带,如 type=ProxyFactory 接口均有@Adaptive
方法淑蔚,又@SPI("javassist")
代表默認(rèn)獲取到的是 JavassistProxyFactory 類。
(ProxyFactory)ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension()
獲得的類如下:
package org.apache.dubbo.rpc;
import org.apache.dubbo.common.extension.ExtensionLoader;
public class ProxyFactory$Adaptive implements org.apache.dubbo.rpc.ProxyFactory {
public java.lang.Object getProxy(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
// 檢查是否參數(shù)異常
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
org.apache.dubbo.common.URL url = arg0.getUrl();
// 首先根據(jù) url 中指定的 proxy 名稱愕撰,為空的話默認(rèn)使用 javassist 類名對(duì)應(yīng)的 ProxyFactory 實(shí)現(xiàn)類
String extName = url.getParameter("proxy", "javassist");
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getProxy(arg0);
}
public java.lang.Object getProxy(org.apache.dubbo.rpc.Invoker arg0, boolean arg1) throws org.apache.dubbo.rpc.RpcException {
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
org.apache.dubbo.common.URL url = arg0.getUrl();
String extName = url.getParameter("proxy", "javassist");
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getProxy(arg0, arg1);
}
public org.apache.dubbo.rpc.Invoker getInvoker(java.lang.Object arg0, java.lang.Class arg1, org.apache.dubbo.common.URL arg2) throws org.apache.dubbo.rpc.RpcException {
if (arg2 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg2;
String extName = url.getParameter("proxy", "javassist");
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getInvoker(arg0, arg1, arg2);
}
}
(Protocol)ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension()
獲得的類如下:
package org.apache.dubbo.rpc;
import org.apache.dubbo.common.extension.ExtensionLoader;
public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol {
// 沒(méi)有 @Adaptive 注解的方法會(huì)直接拋出異常
public void destroy() {
throw new UnsupportedOperationException("The method public abstract void org.apache.dubbo.rpc.Protocol.destroy() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
}
public int getDefaultPort() {
throw new UnsupportedOperationException("The method public abstract int org.apache.dubbo.rpc.Protocol.getDefaultPort() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
}
// 有 Adaptive 注解的方法都會(huì)生成動(dòng)態(tài)方法
public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
// 參數(shù)異常檢查
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
// 這里使用的是 url 中的 protocol 作為動(dòng)態(tài)獲取的類名刹衫,如果 url 中沒(méi)有,則使用默認(rèn)的類名
org.apache.dubbo.common.URL url = arg0.getUrl();
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.export(arg0);
}
public org.apache.dubbo.rpc.Invoker refer(java.lang.Class arg0, org.apache.dubbo.common.URL arg1) throws org.apache.dubbo.rpc.RpcException {
if (arg1 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg1;
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.refer(arg0, arg1);
}
public java.util.List getServers() {
throw new UnsupportedOperationException("The method public default java.util.List org.apache.dubbo.rpc.Protocol.getServers() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
}
}
通過(guò) SPI 獲取到了 Type 接口的 AdaptiveClass搞挣,接下來(lái)看在使用的地方如何動(dòng)態(tài)獲取指定的類带迟。
暴露服務(wù)到 jvm 本地
代碼很短小,實(shí)際運(yùn)行的內(nèi)容卻不簡(jiǎn)單
private void exportLocal(URL url) {
// 設(shè)置 local URL 屬性
URL local = URLBuilder.from(url).setProtocol("injvm").setHost("127.0.0.1").setPort(0).build();
Exporter<?> exporter = PROTOCOL.export(PROXY_FACTORY.getInvoker(this.ref, this.interfaceClass, local));
// ...
}
通過(guò)前面已經(jīng)知道 PROXY_FACTORY
是動(dòng)態(tài)生成ProxyFactory$Adaptive
類的實(shí)例囱桨,
PROXY_FACTORY.getInvoker(this.ref, this.interfaceClass, local)
會(huì)調(diào)用ProxyFactory$Adaptive
類的下面幾行代碼
org.apache.dubbo.common.URL url = arg2;
// 為 local 的 URL 中 proxy 參數(shù)為空仓犬,會(huì)去獲取名為 javassist 的默認(rèn)實(shí)現(xiàn)類
String extName = url.getParameter("proxy", "javassist");
// 這里根據(jù)運(yùn)行時(shí)得到的名稱加載指定類實(shí)例
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
// 調(diào)用類實(shí)例的 getInvoker() 方法
return extension.getInvoker(arg0, arg1, arg2);
getExtension(extName) 方法調(diào)用邏輯如下
PROXY_FACTORY.getInvoker(this.ref, this.interfaceClass, local)
實(shí)際調(diào)用棧如下
接著是 PROTOCOL.export(invoker)
分析過(guò)程與 PROXY_FACTORY.getInvoker(...)
一致,它的調(diào)用棧邏輯如下