title: Dubbo SPI機(jī)制分析【二】
tags: Dubbo,SPI,源碼
grammar_cjkRuby: true
上回Dubbo SPI機(jī)制分析【一】分析了ExtensionLoader.getExtensionLoader.getAdaptiveExtension
的過(guò)程,接下來(lái)繼續(xù)分析ExtensionLoader.getExtension
:
源碼分析
getExtension
public T getExtension(String name) {
if (name == null || name.length() == 0)
throw new IllegalArgumentException("Extension name == null");
if ("true".equals(name)) {
return getDefaultExtension();
}
Holder<Object> holder = cachedInstances.get(name);
if (holder == null) {
cachedInstances.putIfAbsent(name, new Holder<Object>());
holder = cachedInstances.get(name);
}
Object instance = holder.get();
if (instance == null) {
synchronized (holder) {
instance = holder.get();
if (instance == null) {
//實(shí)例化拓展點(diǎn)實(shí)現(xiàn)類
instance = createExtension(name);
holder.set(instance);
}
}
}
return (T) instance;
}
判斷當(dāng)前名字對(duì)應(yīng)的拓展點(diǎn)實(shí)現(xiàn)類的實(shí)例是否存在,如果不存在,進(jìn)行注冊(cè)式單例構(gòu)造實(shí)例createExtension
createExtension
private T createExtension(String name) {
//加載所有拓展點(diǎn)實(shí)現(xiàn)類妒峦,然后根據(jù)name獲取對(duì)應(yīng)的拓展點(diǎn)實(shí)現(xiàn)類
Class<?> clazz = getExtensionClasses().get(name);
if (clazz == null) {
throw findException(name);
}
try {
T instance = (T) EXTENSION_INSTANCES.get(clazz);
if (instance == null) {
//以注冊(cè)式單例 構(gòu)造實(shí)例
EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance());
instance = (T) EXTENSION_INSTANCES.get(clazz);
}
//對(duì)實(shí)例進(jìn)行注入操作
injectExtension(instance);
//進(jìn)行包裝類封裝
Set<Class<?>> wrapperClasses = cachedWrapperClasses;
//遍歷當(dāng)前拓展點(diǎn)的所有包裝類,進(jìn)行包裝
if (wrapperClasses != null && !wrapperClasses.isEmpty()) {
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);
}
}
這段代碼一共做了這么幾件事:
加載拓展點(diǎn)實(shí)現(xiàn)類
實(shí)例化拓展點(diǎn)實(shí)現(xiàn)類麻车,并且進(jìn)行注入
-
遍歷當(dāng)前拓展點(diǎn)的包裝類伯铣,并且將當(dāng)前
name
對(duì)應(yīng)的拓展點(diǎn)實(shí)例作為參數(shù)傳入該包裝類實(shí)例的構(gòu)造函數(shù)崎坊,將該包裝類實(shí)例化,如圖
最后返回實(shí)例
總結(jié)
至此壁公,getExtension
分析完畢感论,主要需要注意的一點(diǎn)就是,通過(guò)此方法返回的實(shí)例最后可能會(huì)通過(guò)包裝類進(jìn)行包裝紊册,做一些特殊處理比肄,形成一個(gè)鏈?zhǔn)秸{(diào)用,最后才會(huì)真正調(diào)用到特定實(shí)例的具體方法囊陡。
那么還有個(gè)問(wèn)題芳绩,對(duì)于
ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension()
這段代碼,最后返回的Protocol類型撞反,具體是怎樣的呢妥色?
-首先通過(guò)getAdaptiveExtension
獲取到的是Protocol$Adaptive
:
package com.wl.dubbo;
/**
* @Author: liumenglong
* @Date: 2018/9/29 22:48
* @Description:
*/
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class Protocol$Adaptive implements com.alibaba.dubbo.rpc.Protocol {
public void destroy() {
throw new UnsupportedOperationException("method public abstract void com.alibaba.dubbo.rpc.Protocol.destroy() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
}
public int getDefaultPort() {
throw new UnsupportedOperationException("method public abstract int com.alibaba.dubbo.rpc.Protocol.getDefaultPort() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
}
public com.alibaba.dubbo.rpc.Invoker refer(java.lang.Class arg0, com.alibaba.dubbo.common.URL arg1) throws com.alibaba.dubbo.rpc.RpcException {
if (arg1 == null) throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg1;
//根據(jù)URL獲取到對(duì)應(yīng)的拓展名,如果沒(méi)有指定遏片,則默認(rèn)取“dubbo”
String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");
//根據(jù)拓展名獲取對(duì)應(yīng)的拓展點(diǎn)實(shí)現(xiàn)類
com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);
//調(diào)用實(shí)際拓展點(diǎn)的refer方法嘹害,如DubboProtocol
return extension.refer(arg0, arg1);
}
public com.alibaba.dubbo.rpc.Exporter export(com.alibaba.dubbo.rpc.Invoker arg0) throws com.alibaba.dubbo.rpc.RpcException {
if (arg0 == null) throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null)
throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");
com.alibaba.dubbo.common.URL url = arg0.getUrl();
//根據(jù)URL獲取到對(duì)應(yīng)的拓展名,如果沒(méi)有指定丁稀,則默認(rèn)取“dubbo”
String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url(" + url.toString() + ") use keys([protocol])");
//根據(jù)拓展名獲取對(duì)應(yīng)的拓展點(diǎn)實(shí)現(xiàn)類
com.alibaba.dubbo.rpc.Protocol extension =
(com.alibaba.dubbo.rpc.Protocol) ExtensionLoader.
getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);
//調(diào)用實(shí)際拓展點(diǎn)的export方法吼拥,如DubboProtocol
return extension.export(arg0);
}
}
查看方法export
,最后的調(diào)用還是需要先獲得具體的Protocol:
//根據(jù)拓展名獲取對(duì)應(yīng)的拓展點(diǎn)實(shí)現(xiàn)類
com.alibaba.dubbo.rpc.Protocol extension =
(com.alibaba.dubbo.rpc.Protocol) ExtensionLoader.
getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);
那么也就是說(shuō)最后需要通過(guò)getExtension
來(lái)獲取特定的Protocol
,而此時(shí)extName
為dubbo
倚聚,那么應(yīng)該返回的就是DubboProtocol
,然而在上面分析我們可以知道线衫,DubboProtcol
在實(shí)例化之后,需要經(jīng)過(guò)包裝類的封裝惑折,因此通過(guò)此方法最后返回的Protocol
是ProtocolListenerWrapper