您好,登錄后才能下訂單哦!
這篇文章主要講解了“Dubbo的SPI機制介紹以及Adaptive實例”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Dubbo的SPI機制介紹以及Adaptive實例”吧!
@SPI("dubbo") public interface AdaptiveExt { @Adaptive // 單元測試方法4的注解為@Adaptive({"t"}) String echo(String msg, URL url); }
public class DubboAdaptiveExt implements AdaptiveExt { @Override public String echo(String msg, URL url) { return "dubbo"; } } public class SpringCloudAdaptiveExt implements AdaptiveExt { @Override public String echo(String msg, URL url) { return "spring cloud"; } } // 單元測試3中加上@Adaptive注解,其余不加 @Adaptive public class ThriftAdaptiveExt implements AdaptiveExt { @Override public String echo(String msg, URL url) { return "thrift"; } }
同時應當在resources
目錄下新建META-INF/dubbo
文件夾,新建com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt
,即接口的全限定名
文件,文件內容為:
dubbo=com.alibaba.dubbo.demo.provider.adaptive.impl.DubboAdaptiveExt cloud=com.alibaba.dubbo.demo.provider.adaptive.impl.SpringCloudAdaptiveExt thrift=com.alibaba.dubbo.demo.provider.adaptive.impl.ThriftAdaptiveExt
下面是4個單元測試用例。觀察4個測試用例的輸出結果,我們可以得出以下結論:
從測試3可以看出,若實現類加了@Adaptive注解,則它優先級最高
,getAdaptiveExtension()
創建的就是該類的實例
從測試1看出,若SP注解上有值,且url參數中無值,并且沒有類標注@Adaptive
注解,則創建dubbo的key對應的類的實例
從測試4看出,若方法上有注解@Adpative({"t"})
,則URL中應當配上該參數t=cloud
,創建cloud對應的實例
從測試2看出,方法有注解@Adaptive,同時URL配置的是默認參數,該參數時通過AdaptiveExt
通過轉小寫
生成(adaptive.ext=cloud)
,則創建的就是cloud對應類的實例,可以看出,其實測試2和4類似,只要URL中有參數并且配置正確,則忽略@SPI注解上的值
所以可以得出優先級: @Adaptive標注的類 > URL參數 > @SPI注解中的值
/** * SPI上有注解,@SPI("dubbo"),url無參數,沒有類上添加@Adaptive注解,方法@Adaptive注解上無參數,輸出dubbo */ @Test public void test1(){ ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class); AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension(); URL url = URL.valueOf("test://localhost/test"); System.out.println(adaptiveExtension.echo("d", url)); }
/** * SPI上有注解,@SPI("dubbo"),URL中也有具體的值,輸出spring cloud,注意這里對方法標注有@Adaptive注解,但是該注解沒有值 */ @Test public void test2(){ ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class); AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension(); URL url = URL.valueOf("test://localhost/test?adaptive.ext=cloud"); System.out.println(adaptiveExtension.echo("d", url)); }
/** * SPI上有注解,@SPI("dubbo"),URL中也有具體的值,ThriftAdaptiveExt實現類上面有@Adaptive注解,輸出thrift */ @Test public void test3(){ ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class); AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension(); URL url = URL.valueOf("test://localhost/test?adaptive.ext=cloud"); System.out.println(adaptiveExtension.echo("d", url)); }
/** * SPI上有注解,@SPI("dubbo"),URL中也有具體的值,接口方法中加上注解@Adaptive({"t"}),各個實現類上面沒有 * @Adaptive注解,輸出spring cloud */ @Test public void test4(){ ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class); AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension(); URL url = URL.valueOf("test://localhost/test?t=cloud"); System.out.println(adaptiveExtension.echo("d", url)); }
首先先分析測試用例對應的源碼,其余幾種情況都差不多,1種情況分析透徹了,其余幾種自然就清楚了.
// SPI上有注解,@SPI("dubbo"),url無參數,沒有類上添加@Adaptive注解,方法@Adaptive注解上無參數,輸出dubbo @Test public void test1(){ ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class); AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension(); URL url = URL.valueOf("test://localhost/test"); System.out.println(adaptiveExtension.echo("d", url)); }
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) { // 拋異常 } } } } else { // 拋異常 } } return (T) instance; }
private T createAdaptiveExtension() { try { // 分為3步:1是創建自適應拓展代理類Class對象,2是通過反射創建對象,3是給創建的對象按需依賴注入 return injectExtension((T) getAdaptiveExtensionClass().newInstance()); } catch (Exception e) { // 拋異常 } } private Class<?> getAdaptiveExtensionClass() { // 從默認目錄中加載標注了@SPI注解的實現類 getExtensionClasses(); // 如果有標注了@Adaptive注解實現類,那么cachedAdaptiveClass不為空,直接返回 if (cachedAdaptiveClass != null) { return cachedAdaptiveClass; } // 創建自適應拓展代理類class文件 return cachedAdaptiveClass = createAdaptiveExtensionClass(); }
private Class<?> createAdaptiveExtensionClass() { // code就是保存了創建的class字符串數據 String code = createAdaptiveExtensionClassCode(); ClassLoader classLoader = findClassLoader(); Compiler compiler = ExtensionLoader.getExtensionLoader(Compiler.class).getAdaptiveExtension(); return compiler.compile(code, classLoader); }
private String createAdaptiveExtensionClassCode() { // 用來存放生成的代理類class文件 StringBuilder codeBuilder = new StringBuilder(); // 遍歷標注有@SPI注解的接口的所有方法,這里分析的是com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt Method[] methods = type.getMethods(); // 這些方法中應當致至少有一個方法被@Adaptive注解標注,否則不需要生成自適應代理類,直接拋出異常 boolean hasAdaptiveAnnotation = false; for (Method m : methods) { if (m.isAnnotationPresent(Adaptive.class)) { hasAdaptiveAnnotation = true; break; } } // no need to generate adaptive class since there's no adaptive method found. if (!hasAdaptiveAnnotation) // 拋異常 // 生成包信息,形如package com.alibaba.dubbo.demo.provider.adaptive; codeBuilder.append("package ").append(type.getPackage().getName()).append(";"); // 生成導包信息,形如import com.alibaba.dubbo.common.extension.ExtensionLoader; codeBuilder.append("\nimport ").append(ExtensionLoader.class.getName()).append(";"); // 生成類名,形如public class AdaptiveExt$Adaptive // implements com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt { codeBuilder.append("\npublic class ").append(type.getSimpleName()).append("$Adaptive"). append(" implements ").append(type.getCanonicalName()).append(" {"); // 遍歷所有方法,為SPI接口的所有方法生成代理方法 for (Method method : methods) { // 方法返回值、參數、拋出異常 Class<?> rt = method.getReturnType(); Class<?>[] pts = method.getParameterTypes(); Class<?>[] ets = method.getExceptionTypes(); // 獲取方法上的Adaptive注解,如果方法上沒有該注解,直接為該方法拋出異常 Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class); StringBuilder code = new StringBuilder(512); if (adaptiveAnnotation == null) { code.append("throw new UnsupportedOperationException(\"method ") .append(method.toString()).append(" of interface ") .append(type.getName()).append(" is not adaptive method!\");"); } else { // urlTypeIndex用來記錄URL這個參數在第幾個參數位置上,這里String echo(String msg, URL url); // 在位置1上 int urlTypeIndex = -1; for (int i = 0; i < pts.length; ++i) { if (pts[i].equals(URL.class)) { urlTypeIndex = i; break; } } // 找到了URL參數 if (urlTypeIndex != -1) { // 空指針檢查 // s形如:if (arg1 == null) throw new IllegalArgumentException("url == null"); String s = String.format("\nif (arg%d == null) throw new IllegalArgumentException(\"url == null\");",urlTypeIndex); code.append(s); // s形如:com.alibaba.dubbo.common.URL url = arg1; s = String.format("\n%s url = arg%d;", URL.class.getName(), urlTypeIndex); code.append(s); } // 沒找到,暫不分析,TODO // 獲取方法上的Adaptive注解的值,@Adaptive({"t"}),這里是t String[] value = adaptiveAnnotation.value(); // 如果@Adaptive注解沒有值,對應第二種測試情況,從接口名生成從url中獲取參數的key, // key為adaptive.ext,獲取參數為url.getParameter("adaptive.ext", "dubbo") // 因為第二種情況URL中傳遞了adaptive.ext這個參數, // 所以String extName = url.getParameter("t", "dubbo");中獲取的是cloud if (value.length == 0) { char[] charArray = type.getSimpleName().toCharArray(); StringBuilder sb = new StringBuilder(128); for (int i = 0; i < charArray.length; i++) { if (Character.isUpperCase(charArray[i])) { if (i != 0) { sb.append("."); } sb.append(Character.toLowerCase(charArray[i])); } else { sb.append(charArray[i]); } } value = new String[]{sb.toString()}; } // hasInvocation 暫不分析,TODO // defaultExtName是dubbo,cachedDefaultName = names[0],這個值是@SPI("dubbo")里的 String defaultExtName = cachedDefaultName; String getNameCode = null; for (int i = value.length - 1; i >= 0; --i) { if (i == value.length - 1) { if (null != defaultExtName) { if (!"protocol".equals(value[i])) if (hasInvocation) getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); else // 形如:url.getParameter("t", "dubbo"); // 理解就是看url中有沒有傳t參數,傳了就以url中為準,否則就取 // @SPI("dubbo")中的為默認值 getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", value[i], defaultExtName); else getNameCode = String.format("( url.getProtocol() == null ? \"%s\" : url.getProtocol() )", defaultExtName); } else { if (!"protocol".equals(value[i])) if (hasInvocation) getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); else getNameCode = String.format("url.getParameter(\"%s\")", value[i]); else getNameCode = "url.getProtocol()"; } } else { if (!"protocol".equals(value[i])) if (hasInvocation) getNameCode = String.format("url.getMethodParameter(methodName, \"%s\", \"%s\")", value[i], defaultExtName); else getNameCode = String.format("url.getParameter(\"%s\", %s)", value[i], getNameCode); else getNameCode = String.format("url.getProtocol() == null ? (%s) : url.getProtocol()", getNameCode); } } // 形如:String extName = url.getParameter("t", "dubbo"); // 這個extName就是要為@SPI標注的接口生成哪個代理類 code.append("\nString extName = ").append(getNameCode).append(";"); // check extName == null? // 形如:if (extName == null) throw new IllegalStateException("..."); String s = String.format("\nif(extName == null) " + "throw new IllegalStateException(\"Fail to get extension(%s) name from url(\" + url.toString() + \") use keys(%s)\");", type.getName(), Arrays.toString(value)); code.append(s); // AdaptiveExt extension = (AdaptiveExt) // ExtensionLoader.getExtensionLoader(AdaptiveExt.class).getExtension(extName); s = String.format("\n%s extension = (%<s)%s.getExtensionLoader(%s.class). getExtension(extName);",type.getName(), ExtensionLoader.class.getSimpleName(), type.getName()); code.append(s); // return statement if (!rt.equals(void.class)) { code.append("\nreturn "); } // 形如:return extension.echo(arg0, arg1); s = String.format("extension.%s(", method.getName()); code.append(s); for (int i = 0; i < pts.length; i++) { if (i != 0) code.append(", "); code.append("arg").append(i); } code.append(");"); } // 加上方法名,形如:public java.lang.String echo(java.lang.String arg0, com.alibaba.dubbo.common.URL arg1) { codeBuilder.append("\npublic ") .append(rt.getCanonicalName()).append(" ").append(method.getName()).append("("); for (int i = 0; i < pts.length; i++) { if (i > 0) { codeBuilder.append(", "); } codeBuilder.append(pts[i].getCanonicalName()); codeBuilder.append(" "); codeBuilder.append("arg").append(i); } codeBuilder.append(")"); // 異常 if (ets.length > 0) { codeBuilder.append(" throws "); for (int i = 0; i < ets.length; i++) { if (i > 0) { codeBuilder.append(", "); } codeBuilder.append(ets[i].getCanonicalName()); } } codeBuilder.append(" {"); codeBuilder.append(code.toString()); codeBuilder.append("\n}"); } codeBuilder.append("\n}"); if (logger.isDebugEnabled()) { logger.debug(codeBuilder.toString()); } return codeBuilder.toString(); }
通過這一系列代碼,Dubbo
就為AdaptiveExt
根據@SPI
的注解值dubbo
生成了一個自適應拓展代理類,類代碼如下:
package com.alibaba.dubbo.demo.provider.adaptive; import com.alibaba.dubbo.common.extension.ExtensionLoader; public class AdaptiveExt$Adaptive implements com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt { public java.lang.String echo(java.lang.String arg0, com.alibaba.dubbo.common.URL arg1) { if (arg1 == null) throw new IllegalArgumentException("url == null"); com.alibaba.dubbo.common.URL url = arg1; // 核心,通過上面的分析我們知道,并沒有配置t參數,所以URL取不到t參數,則以默認值dubbo代替,而dubbo就是 // @SPI注解的值,adaptiveExtension.echo("d", url),執行這句代碼時,adaptiveExtension實際上是 // AdaptiveExt$Adaptive的實例對象,因此會走到它的echo方法中 String extName = url.getParameter("t", "dubbo"); if (extName == null) throw new IllegalStateException("Fail to get extension(AdaptiveExt) name from url(" + url.toString() + ") use keys([t])"); // 為了排版布局,使用了簡寫AdaptiveExt.class,但是應當知道這里應當是全限定名 // 這里面根據extName去獲取Adaptive實例對象,獲取的是dubbo的key對應的DubboAdaptiveExt實例對象 com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt extension = (com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt) ExtensionLoader.getExtensionLoader(AdaptiveExt.class). getExtension(extName); // 所以會走DubboAdaptiveExt的echo方法,輸出dubbo return extension.echo(arg0, arg1); } }
分析完了測試用例1,再來分析2和4就簡單多了,看代碼.歸納起來就是,如果方法上配置了@Adaptive
,就將接口名轉小寫(adaptive.ext)
,去URL中取這個參數對應的值,即url.getParameter("adaptive.ext", "dubbo")
的值作為extName
,生成的也是extName對應的類.如果方法上配置了@Adaptive({"t"})
,則以url.getParameter("t", "dubbo")
這種方式去取值作為extName
.
// 獲取方法上的Adaptive注解的值,@Adaptive({"t"}),這里是t String[] value = adaptiveAnnotation.value(); // 如果@Adaptive注解沒有值,對應第二種測試情況,從接口名生成從url中獲取參數的key,key為adaptive.ext,獲取參數 // 為url.getParameter("adaptive.ext", "dubbo") // 因為第二種情況URL中傳遞了adaptive.ext這個參數,所以String extName = url.getParameter("t", "dubbo");中獲取的是cloud if (value.length == 0) { char[] charArray = type.getSimpleName().toCharArray(); StringBuilder sb = new StringBuilder(128); for (int i = 0; i < charArray.length; i++) { if (Character.isUpperCase(charArray[i])) { if (i != 0) { sb.append("."); } sb.append(Character.toLowerCase(charArray[i])); } else { sb.append(charArray[i]); } } value = new String[]{sb.toString()}; }
// defaultExtName是dubbo,cachedDefaultName = names[0],這個值是@SPI("dubbo")里的 String defaultExtName = cachedDefaultName; String getNameCode = null; for (int i = value.length - 1; i >= 0; --i) { if (i == value.length - 1) { if (null != defaultExtName) { if (!"protocol".equals(value[i])) if (hasInvocation) // 刪除部分代碼 else // 形如:url.getParameter("t", "dubbo"); // 理解就是看url中有沒有傳t參數,傳了就以url中為準,否則就取@SPI("dubbo")中的為默認值 getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", value[i], defaultExtName); else // 刪除部分代碼 } else { // 刪除部分代碼 } } } // 形如:String extName = url.getParameter("t", "dubbo"); // 這個extName就是要為@SPI標注的接口生成哪個代理類 code.append("\nString extName = ").append(getNameCode).append(";");
接下來分析測試用例3,即ThriftAdaptiveExt
類上面標注了@Adaptive
注解,前面也說過,它的優先級最高,下面看代碼.
private Class<?> getAdaptiveExtensionClass() { // 1.從默認目錄中加載標注了@SPI注解的實現類 getExtensionClasses(); // 2.如果有標注了@Adaptive注解實現類,那么cachedAdaptiveClass不為空,直接返回 if (cachedAdaptiveClass != null) { return cachedAdaptiveClass; } // 3.創建自適應拓展代理類class文件 return cachedAdaptiveClass = createAdaptiveExtensionClass(); }
前面我們分析沒有類上面標注@Adaptive
注解時,dubbo需要根據配置情況為接口生成自適應拓展代理類,也就是上述對應的步驟3代碼.但是當有類標注了@Adaptive
注解時,情況就不一樣了.看上面步驟1getExtensionClasses()
會走到下面loadClass
方法,當解析到ThriftAdaptiveExt
類時,發現它滿足clazz.isAnnotationPresent(Adaptive.class)
條件,因此cachedAdaptiveClass = clazz
被緩存起來,不會再走后面的邏輯.這樣當走步驟2時,直接返回cachedAdaptiveClass
.那么dubbo為AdaptiveExt接口生成的自適應拓展就是ThriftAdaptiveExt
.
private void loadClass(Map<String, Class<?>> extensionClasses, java.net.URL resourceURL, Class<?> clazz, String name) throws NoSuchMethodException { // 判斷clazz是否為標注了@Adaptive注解,后面分析 if (clazz.isAnnotationPresent(Adaptive.class)) { if (cachedAdaptiveClass == null) { cachedAdaptiveClass = clazz; } else if (!cachedAdaptiveClass.equals(clazz)) { throw new IllegalStateException("More than 1 adaptive class found: " + cachedAdaptiveClass.getClass().getName() + ", " + clazz.getClass().getName()); } } // 刪除無關代碼 }
感謝各位的閱讀,以上就是“Dubbo的SPI機制介紹以及Adaptive實例”的內容了,經過本文的學習后,相信大家對Dubbo的SPI機制介紹以及Adaptive實例這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。