鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽
Posted Java杩欑偣浜?/a> 鏁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽相关的知识,希望对你有一定的参考价值。
姣忔櫄10鐐逛笌浣犲垎浜獼ava鎶€鏈€両T璧勮
Dubbo SPI 鏋舵瀯鍥?/span>
ExtensionLoader<Protocol> extensionLoader = ExtensionLoader.getExtensionLoader(Protocol.class);Protocol http = extensionLoader.getExtension("dubbo");System.out.println(http);
Class<?> type锛?/strong>琛ㄧず褰撳墠ExtensionLoader瀹炰緥鏄摢涓帴鍙g殑鎵╁睍鐐瑰姞杞藉櫒
ExtensionFactory objectFactory锛?/strong>鎵╁睍鐐瑰伐鍘傦紙瀵硅薄宸ュ巶锛夛紝鍙互鑾峰緱鏌愪釜瀵硅薄
ExtensionLoader鏈€缁堟墍寰楀埌鐨勫璞℃槸Dubbo SPI鏈哄埗浜х敓鐨?/span>
ExtensionFactory鏈€缁堟墍寰楀埌鐨勫璞″彲鑳芥槸Dubbo SPI鏈哄埗鎵€浜х敓鐨勶紝涔熷彲鑳芥槸浠嶴pring瀹瑰櫒涓墍鑾峰緱鐨勫璞?/span>
getExtension("dubbo")锛?/strong>琛ㄧず鑾峰彇鍚嶅瓧涓篸ubbo鐨勬墿灞曠偣瀹炰緥
getAdaptiveExtension()锛?/strong>琛ㄧず鑾峰彇涓€涓嚜閫傚簲鐨勬墿灞曠偣瀹炰緥
getActivateExtension(URL url, String[] values, String group)锛?/strong>琛ㄧず涓€涓彲浠ヨurl婵€娲荤殑鎵╁睍鐐瑰疄渚嬶紝鍚庢枃璇︾粏瑙i噴
getExtension(String name)鏂规硶
createExtension(String name)鏂规硶
鏍规嵁name鎵惧埌瀵瑰簲鐨勬墿灞曠偣瀹炵幇绫?/span>
鏍规嵁瀹炵幇绫荤敓鎴愪竴涓疄渚嬶紝鎶?span>瀹炵幇绫诲拰瀵瑰簲鐢熸垚鐨勫疄渚?/strong>杩涜缂撳瓨
瀵圭敓鎴愬嚭鏉ョ殑瀹炰緥杩涜渚濊禆娉ㄥ叆锛堢粰瀹炰緥鐨勫睘鎬ц繘琛岃祴鍊硷級
瀵逛緷璧栨敞鍏ュ悗鐨勫疄渚嬭繘琛孉OP锛圵rapper锛?鎶婂綋鍓嶆帴鍙g被鐨勬墍鏈夌殑Wrapper鍏ㄩ儴涓€灞備竴灞傚寘瑁瑰湪瀹炰緥瀵硅薄涓婏紝娌″寘瑁逛釜Wrapper鍚庯紝涔熶細瀵筗rapper瀵硅薄杩涜渚濊禆娉ㄥ叆
杩斿洖鏈€缁堢殑Wrapper瀵硅薄
getExtensionClasses
鏍规嵁鎺ュ彛鐨勫叏闄愬畾鍚嶅幓META-INF/dubbo/internal/鐩綍涓嬪鎵惧搴旂殑鏂囦欢锛岃皟鐢╨oadResource鏂规硶杩涜鍔犺浇
鏍规嵁鎺ュ彛鐨勫叏闄愬畾鍚嶅幓META-INF/dubbo/鐩綍涓嬪鎵惧搴旂殑鏂囦欢锛岃皟鐢╨oadResource鏂规硶杩涜鍔犺浇
鏍规嵁鎺ュ彛鐨勫叏闄愬畾鍚嶅幓META-INF/services/鐩綍涓嬪鎵惧搴旂殑鏂囦欢锛岃皟鐢╨oadResource鏂规硶杩涜鍔犺浇
loadResource鏂规硶
loadClass鏂规硶
褰撳墠鎵╁睍鐐瑰疄鐜扮被涓婃槸鍚﹀瓨鍦ˊAdaptive娉ㄨВ锛屽鏋滃瓨鍦ㄥ垯鎶婅绫昏涓烘槸褰撳墠鎺ュ彛鐨勯粯璁よ嚜閫傚簲绫伙紙鎺ュ彛浠g悊绫伙級锛屽苟鎶婅绫诲瓨鍒癱achedAdaptiveClass灞炴€т笂銆?/span>
褰撳墠鎵╁睍鐐瑰疄鐜版槸鍚︽槸涓€涓綋鍓嶆帴鍙g殑涓€涓猈rapper绫伙紝濡傛灉鍒ゆ柇鐨勶紵灏辨槸鐪嬪綋鍓嶇被涓槸鍚﹀瓨鍦ㄤ竴涓瀯閫犳柟娉曪紝璇ユ瀯閫犳柟娉曞彧鏈変竴涓弬鏁帮紝鍙傛暟绫诲瀷涓烘帴鍙g被鍨嬶紝濡傛灉瀛樺湪杩欎竴鐨勬瀯閫犳柟娉曪紝閭d箞杩欎釜绫诲氨鏄鎺ュ彛鐨刉rapper绫伙紝濡傛灉鏄紝鍒欐妸璇ョ被娣诲姞鍒癱achedWrapperClasses涓幓锛?cachedWrapperClasses鏄竴涓猻et銆?/span>
濡傛灉涓嶆槸鑷€傚簲绫伙紝鎴栬€呬篃涓嶆槸Wrapper绫伙紝鍒欏垽鏂槸鏈夊瓨鍦╪ame锛屽鏋滄病鏈塶ame锛屽垯鎶ラ敊銆?/span>
濡傛灉鏈夊涓猲ame锛屽垯鍒ゆ柇涓€涓嬪綋鍓嶆墿灞曠偣瀹炵幇绫讳笂鏄惁瀛樺湪@Activate娉ㄨВ锛?/span>濡傛灉瀛樺湪锛屽垯鎶婅绫绘坊鍔犲埌cachedActivates涓紝cachedWrapperClasses鏄竴涓猰ap銆?/strong>
鏈€鍚庯紝閬嶅巻澶氫釜name锛屾妸姣忎釜name鍜屽搴旂殑瀹炵幇绫诲瓨鍒癳xtensionClasses涓幓锛宔xtensionClasses灏辨槸涓婃枃鎵€鎻愬埌鐨刴ap銆?/span>
Dubbo涓殑IOC
鏍规嵁褰撳墠瀹炰緥鐨勭被锛屾壘鍒拌繖涓被涓殑setter鏂规硶锛岃繘琛屼緷璧栨敞鍏?/span>
鍏堝垎鏋愬嚭setter鏂规硶鐨勫弬鏁扮被鍨媝t
鍦ㄦ埅鍙栧嚭setter鏂规硶鎵€瀵瑰簲鐨勫睘鎬у悕property
璋冪敤objectFactory.getExtension(pt, property)寰楀埌涓€涓璞★紝杩欓噷灏变細浠嶴pring瀹瑰櫒鎴栭€氳繃DubboSpi鏈哄埗寰楀埌涓€涓璞★紝姣旇緝鐗规畩鐨勬槸锛屽鏋滄槸閫氳繃DubboSpi鏈哄埗寰楀埌鐨勫璞★紝鏄痯t杩欎釜绫诲瀷鐨勪竴涓嚜閫傚簲瀵硅薄(浠g悊瀵硅薄)銆?/span>
鍐嶅弽灏勮皟鐢╯etter鏂规硶杩涜娉ㄥ叆
Dubbo涓殑AOP
createAdaptiveExtensionClass鏂规硶
type灏辨槸鎺ュ彛
cacheDefaultName灏辨槸璇ユ帴鍙i粯璁ょ殑鎵╁睍鐐瑰疄鐜扮殑鍚嶅瓧
package org.apache.dubbo.rpc;import org.apache.dubbo.common.extension.ExtensionLoader;public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol { 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!"); } public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) 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.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); }}
package org.apache.dubbo.rpc;import org.apache.dubbo.common.extension.ExtensionLoader;public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol { 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!"); } public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) 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.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); }}
璇ユ柟娉曞鏋滄槸鏃犲弬鐨勶紝閭d箞鍒欎細鎶ラ敊
璇ユ柟娉曟湁鍙傛暟锛屽彲浠ユ湁澶氫釜锛屽苟涓斿叾涓煇涓弬鏁扮被鍨嬫槸URL锛岄偅涔堝垯鍙互杩涜浠g悊
璇ユ柟娉曟湁鍙傛暟锛屽彲浠ユ湁澶氫釜锛屼絾鏄病鏈塙RL绫诲瀷鐨勫弬鏁帮紝閭d箞鍒欎笉鑳借繘琛屼唬鐞?/span>
璇ユ柟娉曟湁鍙傛暟锛屽彲浠ユ湁澶氫釜锛屾病鏈塙RL绫诲瀷鐨勫弬鏁帮紝浣嗘槸濡傛灉杩欎簺鍙傛暟绫诲瀷锛屽搴旂殑绫讳腑瀛樺湪getUrl鏂规硶锛堣繑鍥炲€肩被鍨嬩负URL锛夛紝閭d箞涔熷彲浠ヨ繘琛屼唬鐞?/span>
demo
ExtensionLoader<Filter> extensionLoader = ExtensionLoader.getExtensionLoader(Filter.class);URL url = new URL("http://", "localhost", 8080);url = url.addParameter("cache", "test");List<Filter> activateExtensions = extensionLoader.getActivateExtension(url,
new String[]{"validation"}, CommonConstants.CONSUMER);for (Filter activateExtension : activateExtensions) { System.out.println(activateExtension);}
String[] group()锛氳〃绀鸿繖涓墿灞曠偣鏄睘浜庡摢缁勭殑锛岃繖閲岀粍閫氬父鍒嗕负PROVIDER鍜孋ONSUMER锛岃〃绀鸿鎵╁睍鐐硅兘鍦ㄦ湇鍔℃彁渚涜€呯锛屾垨鑰呮秷璐圭浣跨敤
String[] value()锛氳〃绀虹殑鏄疷RL涓殑鏌愪釜鍙傛暟key锛屽綋鍒╃敤getActivateExtension鏂规硶鏉ュ鎵炬墿灞曠偣鏃讹紝濡傛灉浼犲叆鐨剈rl涓寘鍚殑鍙傛暟鐨勬墍鏈塳ey涓紝鍖呮嫭浜嗗綋鍓嶆墿灞曠偣涓殑value鍊硷紝閭d箞鍒欒〃绀哄綋鍓島rl鍙互浣跨敤璇ユ墿灞曠偣銆?/span>
鈼?/p>
鈼?/p>
鈼?/p>
鈼?/p>
以上是关于鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章
鏄撻敊鏄撴贩娣嗙煡璇嗙偣璁茶В绯诲垪锛堜節锛夆€斿缓绔嬪姩鎬佽鍒掓ā鍨嬬殑鍩烘湰姝ラ