鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽

Posted Java杩欑偣浜?/a> 鏁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽相关的知识,希望对你有一定的参考价值。

姣忔櫄10鐐逛笌浣犲垎浜獼ava鎶€鏈€両T璧勮



鍦ㄤ笂涓€绡?nbsp; 鏂囩珷涓垜浠簡瑙e埌浜嗕负鍟ヨ鐪嬫簮鐮併€?/span>
杩欐婧愮爜鍒嗘瀽鐨勬枃绔犲氨鍏堟潵璁茶dubbo鎵╁睍鏈哄埗spi鐨勫師鐞嗭紝娴忚杩嘾ubbo瀹樻柟鏂囨。鐨勬湅鍙嬭偗瀹氱煡閬擄紝dubbo鏈夊ぇ閲忕殑spi鎵╁睍瀹炵幇锛屽寘鎷崗璁墿灞曘€佽皟鐢ㄦ嫤鎴墿灞曘€佽矾鐢辨墿灞曠瓑26涓墿灞曪紝骞朵笖spi鏈哄埗杩愮敤鍒颁簡鍚勪釜妯″潡璁捐涓€傛墍浠ユ垜鎵撶畻鍏堣瑙ubbo鐨勬墿灞曟満鍒秙pi銆?/span>
棣栧厛 鎴戜滑鏉ョ湅鐪嬸煈?/span>

Dubbo SPI 鏋舵瀯鍥?/span>

涓嬮潰杩欎釜Demo灏辨槸Dubbo甯歌鐨勫啓娉曪紝琛ㄧず鑾峰彇"dubbo"瀵瑰簲鐨凱rotocol鎵╁睍鐐广€侾rotocol鏄竴涓帴鍙c€?/span>
   
     
     
   
ExtensionLoader<Protocol> extensionLoader = ExtensionLoader.getExtensionLoader(Protocol.class);Protocol http = extensionLoader.getExtension("dubbo");System.out.println(http);
鍦‥xtensionLoader绫荤殑鍐呴儴鏈変竴涓猻tatic鐨凜oncurrentHashMap锛岀敤鏉ョ紦瀛?span class="mq-26">鏌愪釜鎺ュ彛绫诲瀷鎵€瀵瑰簲鐨?/strong>ExtensionLoader瀹炰緥

ExtensionLoader
ExtensionLoader琛ㄧず鏌愪釜鎺ュ彛鐨勬墿灞曠偣鍔犺浇鍣紝鍙互鐢ㄦ潵鍔犺浇鏌愪釜鎵╁睍鐐瑰疄渚嬨€?/span>
鍦‥xtensionLoader涓櫎寮€鏈変笂鏂囩殑static鐨凪ap澶栵紝杩樻湁涓や釜闈炲父閲嶈鐨勫睘鎬э細
  • Class<?> type锛?/strong>琛ㄧず褰撳墠ExtensionLoader瀹炰緥鏄摢涓帴鍙g殑鎵╁睍鐐瑰姞杞藉櫒

  • ExtensionFactory objectFactory锛?/strong>鎵╁睍鐐瑰伐鍘傦紙瀵硅薄宸ュ巶锛夛紝鍙互鑾峰緱鏌愪釜瀵硅薄

ExtensionLoader鍜? ExtensionFactory鐨勫尯鍒湪浜庯細
  1. ExtensionLoader鏈€缁堟墍寰楀埌鐨勫璞℃槸Dubbo SPI鏈哄埗浜х敓鐨?/span>

  2. ExtensionFactory鏈€缁堟墍寰楀埌鐨勫璞″彲鑳芥槸Dubbo SPI鏈哄埗鎵€浜х敓鐨勶紝涔熷彲鑳芥槸浠嶴pring瀹瑰櫒涓墍鑾峰緱鐨勫璞?/span>

鍦?span>ExtensionLoader涓湁涓変釜甯哥敤鐨勬柟娉曪細
  1. getExtension("dubbo")锛?/strong>琛ㄧず鑾峰彇鍚嶅瓧涓篸ubbo鐨勬墿灞曠偣瀹炰緥

  2. getAdaptiveExtension()锛?/strong>琛ㄧず鑾峰彇涓€涓嚜閫傚簲鐨勬墿灞曠偣瀹炰緥

  3. getActivateExtension(URL url, String[] values, String group)锛?/strong>琛ㄧず涓€涓彲浠ヨurl婵€娲荤殑鎵╁睍鐐瑰疄渚嬶紝鍚庢枃璇︾粏瑙i噴


鍏朵腑锛屼粈涔堟槸鑷€傚簲鎵╁睍鐐瑰疄渚?/strong>锛熷畠鍏跺疄灏辨槸褰撳墠杩欎釜鎺ュ彛鐨勪竴涓?span>浠g悊瀵硅薄銆?/strong>
ExtensionLoader<Protocol> extensionLoader = ExtensionLoader.getExtensionLoader(Protocol.class); Protocol protocol = extensionLoader.getExtension("dubbo");
褰撴垜浠皟鐢ㄤ笂杩颁唬鐮侊紝鎴戜滑浼氬皢寰楀埌涓€涓狣ubboProtocol鐨勫疄渚嬪璞★紝浣嗗湪getExtension()鏂规硶涓紝Dubbo浼氬DubboProtocol瀵硅薄杩涜渚濊禆娉ㄥ叆锛堜篃灏辨槸鑷姩缁欏睘鎬ц祴鍊硷紝灞炴€х殑绫诲瀷涓轰竴涓帴鍙o紝璁颁负A鎺ュ彛锛夛紝杩欎釜鏃跺€欙紝瀵逛簬Dubbo鏉ヨ瀹冨苟涓嶇煡閬撹缁欒繖涓睘鎬ц祴浠€涔堝€硷紝鎹㈠彞璇濊锛孌ubbo骞朵笉鐭ラ亾鍦ㄨ繘琛屼緷璧栨敞鍏ユ椂璇ユ壘涓€涓粈涔堢殑鐨勬墿灞曠偣瀵硅薄缁欒繖涓睘鎬э紝杩欐椂灏变細棰勫厛璧嬪€间竴涓狝鎺ュ彛鐨勮嚜閫傚簲鎵╁睍鐐瑰疄渚嬶紝涔熷氨鏄疉鎺ュ彛鐨勪竴涓唬鐞嗗璞°€?/span>
鍚庣画锛屽湪A鎺ュ彛鐨勪唬鐞嗗璞¤鐪熸鐢ㄥ埌鏃讹紝鎵嶄細缁撳悎URL淇℃伅鎵惧埌鐪熸鐨凙鎺ュ彛瀵瑰簲鐨勬墿灞曠偣瀹炰緥杩涜璋冪敤銆?/span>
鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽


getExtension(String name)鏂规硶

鍦ㄨ皟鐢╣etExtension鍘昏幏鍙栦竴涓墿灞曠偣瀹炰緥鍚庯紝浼氬瀹炰緥杩涜缂撳瓨锛屼笅娆″啀鑾峰彇鍚屾牱鍚嶅瓧鐨勬墿灞曠偣瀹炰緥鏃跺氨浼氫粠缂撳瓨涓嬁浜嗐€?

createExtension(String name)鏂规硶

鍦ㄨ皟鐢╟reateExtension(String name)鏂规硶鍘诲垱寤轰竴涓墿灞曠偣瀹炰緥鏃讹紝瑕佺粡杩囦互涓嬪嚑涓楠わ細
  1. 鏍规嵁name鎵惧埌瀵瑰簲鐨勬墿灞曠偣瀹炵幇绫?/span>

  2. 鏍规嵁瀹炵幇绫荤敓鎴愪竴涓疄渚嬶紝鎶?span>瀹炵幇绫诲拰瀵瑰簲鐢熸垚鐨勫疄渚?/strong>杩涜缂撳瓨

  3. 瀵圭敓鎴愬嚭鏉ョ殑瀹炰緥杩涜渚濊禆娉ㄥ叆锛堢粰瀹炰緥鐨勫睘鎬ц繘琛岃祴鍊硷級

  4. 瀵逛緷璧栨敞鍏ュ悗鐨勫疄渚嬭繘琛孉OP锛圵rapper锛?鎶婂綋鍓嶆帴鍙g被鐨勬墍鏈夌殑Wrapper鍏ㄩ儴涓€灞備竴灞傚寘瑁瑰湪瀹炰緥瀵硅薄涓婏紝娌″寘瑁逛釜Wrapper鍚庯紝涔熶細瀵筗rapper瀵硅薄杩涜渚濊禆娉ㄥ叆

  5. 杩斿洖鏈€缁堢殑Wrapper瀵硅薄

鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽


getExtensionClasses

getExtensionClasses()鏄敤鏉ュ姞杞藉綋鍓嶆帴鍙f墍鏈夌殑鎵╁睍鐐瑰疄鐜扮被鐨勶紝杩斿洖涓€涓狹ap銆備箣鍚庡彲浠ヤ粠杩欎釜Map涓寜鐓ф寚瀹氱殑name鑾峰彇瀵瑰簲鐨勬墿灞曠偣瀹炵幇绫汇€?/span>
褰撴妸褰撳墠鎺ュ彛鐨勬墍鏈夋墿灞曠偣瀹炵幇绫婚兘鍔犺浇鍑烘潵鍚庝篃浼氳繘琛岀紦瀛橈紝涓嬫闇€瑕佸姞杞芥椂鐩存帴鎷跨紦瀛樹腑鐨勩€?/span>
Dubbo鍦ㄥ姞杞戒竴涓帴鍙g殑鎵╁睍鐐规椂锛屾€濊矾鏄繖鏍风殑锛?/span>
  1. 鏍规嵁鎺ュ彛鐨勫叏闄愬畾鍚嶅幓META-INF/dubbo/internal/鐩綍涓嬪鎵惧搴旂殑鏂囦欢锛岃皟鐢╨oadResource鏂规硶杩涜鍔犺浇

  2. 鏍规嵁鎺ュ彛鐨勫叏闄愬畾鍚嶅幓META-INF/dubbo/鐩綍涓嬪鎵惧搴旂殑鏂囦欢锛岃皟鐢╨oadResource鏂规硶杩涜鍔犺浇

  3. 鏍规嵁鎺ュ彛鐨勫叏闄愬畾鍚嶅幓META-INF/services/鐩綍涓嬪鎵惧搴旂殑鏂囦欢锛岃皟鐢╨oadResource鏂规硶杩涜鍔犺浇

杩欓噷鍏跺疄浼氳璁″埌鑰佺増鏈吋瀹圭殑閫昏緫锛屼笉瑙i噴浜?/span>銆?

loadResource鏂规硶

loadResource鏂规硶灏辨槸瀹屾垚瀵规枃浠跺唴瀹圭殑瑙f瀽锛屾寜琛岃繘琛岃В鏋愶紝浼氳В鏋愬嚭"="涓よ竟鐨勫唴瀹癸紝"="宸﹁竟鐨勫唴瀹瑰氨鏄墿灞曠偣鐨刵ame锛屽彸杈圭殑鍐呭灏辨槸鎵╁睍鐐瑰疄鐜扮被锛屽苟涓斾細鍒╃敤ExtensionLoader绫荤殑绫诲姞杞藉櫒鏉ュ姞杞芥墿灞曠偣瀹炵幇绫汇€?/span>
鐒跺悗璋冪敤loadClass鏂规硶瀵筺ame鍜屾墿灞曠偣瀹炰緥杩涜璇︾粏鐨勮В鏋愶紝骞朵笖鏈€缁堟妸浠栦滑鏀惧埌Map涓幓銆?/span>

loadClass鏂规硶

loadClass鏂规硶浼氬仛濡備笅鍑犱欢浜嬫儏锛?/span>
  1. 褰撳墠鎵╁睍鐐瑰疄鐜扮被涓婃槸鍚﹀瓨鍦ˊAdaptive娉ㄨВ锛屽鏋滃瓨鍦ㄥ垯鎶婅绫昏涓烘槸褰撳墠鎺ュ彛鐨勯粯璁よ嚜閫傚簲绫伙紙鎺ュ彛浠g悊绫伙級锛屽苟鎶婅绫诲瓨鍒癱achedAdaptiveClass灞炴€т笂銆?/span>

  2. 褰撳墠鎵╁睍鐐瑰疄鐜版槸鍚︽槸涓€涓綋鍓嶆帴鍙g殑涓€涓猈rapper绫伙紝濡傛灉鍒ゆ柇鐨勶紵灏辨槸鐪嬪綋鍓嶇被涓槸鍚﹀瓨鍦ㄤ竴涓瀯閫犳柟娉曪紝璇ユ瀯閫犳柟娉曞彧鏈変竴涓弬鏁帮紝鍙傛暟绫诲瀷涓烘帴鍙g被鍨嬶紝濡傛灉瀛樺湪杩欎竴鐨勬瀯閫犳柟娉曪紝閭d箞杩欎釜绫诲氨鏄鎺ュ彛鐨刉rapper绫伙紝濡傛灉鏄紝鍒欐妸璇ョ被娣诲姞鍒癱achedWrapperClasses涓幓锛?cachedWrapperClasses鏄竴涓猻et銆?/span>

  3. 濡傛灉涓嶆槸鑷€傚簲绫伙紝鎴栬€呬篃涓嶆槸Wrapper绫伙紝鍒欏垽鏂槸鏈夊瓨鍦╪ame锛屽鏋滄病鏈塶ame锛屽垯鎶ラ敊銆?/span>

  4. 濡傛灉鏈夊涓猲ame锛屽垯鍒ゆ柇涓€涓嬪綋鍓嶆墿灞曠偣瀹炵幇绫讳笂鏄惁瀛樺湪@Activate娉ㄨВ锛?/span>濡傛灉瀛樺湪锛屽垯鎶婅绫绘坊鍔犲埌cachedActivates涓紝cachedWrapperClasses鏄竴涓猰ap銆?/strong>

  5. 鏈€鍚庯紝閬嶅巻澶氫釜name锛屾妸姣忎釜name鍜屽搴旂殑瀹炵幇绫诲瓨鍒癳xtensionClasses涓幓锛宔xtensionClasses灏辨槸涓婃枃鎵€鎻愬埌鐨刴ap銆?/span>

鑷虫锛屽姞杞界被灏辫蛋瀹屼簡銆?/span>
鍥炲埌createExtension(String name)鏂规硶涓殑閫昏緫锛屽綋鍓嶈繖涓帴鍙g殑鎵€鏈夋墿灞曠偣瀹炵幇绫婚兘鎵弿瀹屼簡涔嬪悗锛屽氨鍙互鏍规嵁鐢ㄦ埛鎵€鎸囧畾鐨勫悕瀛楋紝鎵惧埌瀵瑰簲鐨勫疄鐜扮被浜嗭紝鐒跺悗杩涜瀹炰緥鍖栵紝鐒跺悗杩涜IOC(渚濊禆娉ㄥ叆)鍜孉OP銆?/span>

Dubbo涓殑IOC

  1. 鏍规嵁褰撳墠瀹炰緥鐨勭被锛屾壘鍒拌繖涓被涓殑setter鏂规硶锛岃繘琛屼緷璧栨敞鍏?/span>

  2. 鍏堝垎鏋愬嚭setter鏂规硶鐨勫弬鏁扮被鍨媝t

  3. 鍦ㄦ埅鍙栧嚭setter鏂规硶鎵€瀵瑰簲鐨勫睘鎬у悕property

  4. 璋冪敤objectFactory.getExtension(pt, property)寰楀埌涓€涓璞★紝杩欓噷灏变細浠嶴pring瀹瑰櫒鎴栭€氳繃DubboSpi鏈哄埗寰楀埌涓€涓璞★紝姣旇緝鐗规畩鐨勬槸锛屽鏋滄槸閫氳繃DubboSpi鏈哄埗寰楀埌鐨勫璞★紝鏄痯t杩欎釜绫诲瀷鐨勪竴涓嚜閫傚簲瀵硅薄(浠g悊瀵硅薄)銆?/span>

  5. 鍐嶅弽灏勮皟鐢╯etter鏂规硶杩涜娉ㄥ叆

Dubbo涓殑AOP

dubbo涓篃瀹炵幇浜嗕竴濂楅潪甯哥畝鍗曠殑AOP锛屽氨鏄埄鐢╓rapper锛屽鏋滀竴涓帴鍙g殑鎵╁睍鐐逛腑鍖呭惈浜嗗涓猈rapper绫伙紝閭d箞鍦ㄥ疄渚嬪寲瀹屾煇涓墿灞曠偣鍚庯紝灏变細鍒╃敤杩欎簺Wrapper绫诲杩欎釜瀹炰緥杩涜鍖呰9锛屾瘮濡傦細鐜板湪鏈変竴涓狣ubboProtocol鐨勫疄渚嬶紝鍚屾椂瀵逛簬Protocol杩欎釜鎺ュ彛杩樻湁寰堝鐨刉rapper锛屾瘮濡侾rotocolFilterWrapper銆丳rotocolListenerWrapper锛岄偅涔堬紝褰撳DubboProtocol鐨勫疄渚嬪畬鎴愪簡IOC涔嬪悗锛屽氨浼氬厛璋冪敤new ProtocolFilterWrapper(DubboProtocol瀹炰緥)鐢熸垚涓€涓柊鐨凱rotocol鐨勫疄渚嬶紝鍐嶅姝ゅ疄渚嬭繘琛孖OC锛屽畬浜嗕箣鍚庯紝浼氬啀璋冪敤new ProtocolListenerWrapper(ProtocolFilterWrapper瀹炰緥)鐢熸垚涓€涓柊鐨凱rotocol鐨勫疄渚嬶紝鐒跺悗杩涜IOC锛屼粠鑰屽畬鎴怐ubboProtocol瀹炰緥鐨凙OP銆?/span>



鑷€傚簲鎵╁睍鐐硅ˉ鍏?/span>

涓婇潰鎻愬埌鐨勮嚜閫傚簲鎵╁睍鐐瑰璞★紝涔熷氨鏄煇涓帴鍙g殑浠g悊瀵硅薄鏄€氳繃Dubbo鍐呴儴鐢熸垚浠g悊绫伙紝鐒跺悗鐢熸垚浠g悊瀵硅薄鐨勩€?/span>
棰濆鐨勶紝鍦―ubbo涓繕璁捐鍙﹀涓€绉嶆満鍒舵潵鐢熸垚鑷€傚簲鎵╁睍鐐癸紝杩欑鏈哄埗灏辨槸鍙互閫氳繃@Adaptive娉ㄨВ鏉ユ寚瀹氭煇涓被涓烘煇涓帴鍙g殑浠g悊绫伙紝濡傛灉鎸囧畾浜嗭紝Dubbo鍦ㄧ敓鎴愯嚜閫傚簲鎵╁睍鐐瑰璞℃椂瀹為檯涓婄敓鎴愮殑灏辨槸@Adaptive娉ㄨВ鎵€娉ㄨВ鐨勭被鐨勫疄渚嬪璞°€?/span>
濡傛灉鏄敱Dubbo榛樿瀹炵幇鐨勶紝閭d箞鎴戜滑灏辩湅鐪婦ubbo鏄浣曠敓鎴愪唬鐞嗙被鐨勩€?/span>

createAdaptiveExtensionClass鏂规硶

createAdaptiveExtensionClass鏂规硶灏辨槸Dubbo涓粯璁ょ敓鎴怉daptive绫诲疄渚嬬殑閫昏緫銆傝鐧戒簡锛岃繖涓疄渚嬪氨鏄綋鍓嶈繖涓帴鍙g殑涓€涓唬鐞嗗璞°€傛瘮濡備笅闈㈢殑浠g爜锛?
ExtensionLoader<Protocol> extensionLoader = ExtensionLoader.getExtensionLoader(Protocol.class); Protocol protocol = extensionLoader.getAdaptiveExtension();
杩欎釜浠g爜灏辨槸Protocol鎺ュ彛鐨勪竴涓唬鐞嗗璞★紝閭d箞浠g悊閫昏緫灏辨槸鍦?span>new AdaptiveClassCodeGenerator(type, cachedDefaultName).generate()鏂规硶涓€?/span>
  1. type灏辨槸鎺ュ彛

  2. cacheDefaultName灏辨槸璇ユ帴鍙i粯璁ょ殑鎵╁睍鐐瑰疄鐜扮殑鍚嶅瓧

鐪嬩釜渚嬪瓙锛孭rotocol鎺ュ彛鐨凙daptive绫伙細
   
     
     
   
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);    }}
鍙互鐪嬪埌锛孭rotocol鎺ュ彛涓湁鍥涗釜鏂规硶锛屼絾鏄彧鏈塭xport鍜宺efer涓や釜鏂规硶杩涜浠g悊銆?/span> 涓轰粈涔堬紵 鍥犱负Protocol鎺ュ彛涓湪export鏂规硶鍜宺efer鏂规硶涓婂姞浜?/span> @Adaptive 娉ㄨВ銆?/span> 浣嗘槸锛屼笉鏄彧瑕佸湪鏂规硶涓婂姞浜咢Adaptive娉ㄨВ灏卞彲浠ヨ繘琛屼唬鐞嗭紝杩樻湁鍏朵粬鏉′欢锛屾瘮濡傦細
  1. 璇ユ柟娉曞鏋滄槸鏃犲弬鐨勶紝閭d箞鍒欎細鎶ラ敊

  2. 璇ユ柟娉曟湁鍙傛暟锛屽彲浠ユ湁澶氫釜锛屽苟涓斿叾涓煇涓弬鏁扮被鍨嬫槸URL锛岄偅涔堝垯鍙互杩涜浠g悊

  3. 璇ユ柟娉曟湁鍙傛暟锛屽彲浠ユ湁澶氫釜锛屼絾鏄病鏈塙RL绫诲瀷鐨勫弬鏁帮紝閭d箞鍒欎笉鑳借繘琛屼唬鐞?/span>

  4. 璇ユ柟娉曟湁鍙傛暟锛屽彲浠ユ湁澶氫釜锛屾病鏈塙RL绫诲瀷鐨勫弬鏁帮紝浣嗘槸濡傛灉杩欎簺鍙傛暟绫诲瀷锛屽搴旂殑绫讳腑瀛樺湪getUrl鏂规硶锛堣繑鍥炲€肩被鍨嬩负URL锛夛紝閭d箞涔熷彲浠ヨ繘琛屼唬鐞?/span>

鎵€浠ワ紝鍙互鍙戠幇锛屾煇涓帴鍙g殑Adaptive瀵硅薄锛屽湪璋冪敤鏌愪釜鏂规硶鏃讹紝鏄€氳繃璇ユ柟娉曚腑鐨刄RL鍙傛暟锛岄€氳繃璋冪敤ExtensionLoader.getExtensionLoader(com.luban.Car.class).getExtension(extName);寰楀埌涓€涓墿灞曠偣瀹炰緥锛岀劧鍚庤皟鐢ㄨ瀹炰緥瀵瑰簲鐨勬柟娉曘€?/span>


Activate鎵╁睍鐐?/span>

涓婃枃璇村埌锛屾瘡涓墿灞曠偣閮芥湁涓€涓猲ame锛岄€氳繃杩欎釜name鍙互鑾峰緱璇ame瀵瑰簲鐨勬墿灞曠偣瀹炰緥锛屼絾鏄湁鐨勫満鏅笅锛屽笇鏈涗竴娆℃€ц幏寰楀涓墿灞曠偣瀹炰緥

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);}
浼氭壘鍒?涓狥ilter
org.apache.dubbo.rpc.filter.ConsumerContextFilter@4566e5bd org.apache.dubbo.rpc.protocol.dubbo.filter.FutureFilter@1ed4004b org.apache.dubbo.monitor.support.MonitorFilter@ff5b51f org.apache.dubbo.cache.filter.CacheFilter@25bbe1b6 org.apache.dubbo.validation.filter.ValidationFilter@5702b3b1
鍓嶄笁涓槸閫氳繃CommonConstants.CONSUMER鎵惧埌鐨?
CacheFilter鏄€氳繃url涓殑鍙傛暟鎵惧埌鐨?
ValidationFilter鏄€氳繃鎸囧畾鐨刵ame鎵惧埌鐨?

鍦ㄤ竴涓墿灞曠偣绫讳笂锛屽彲浠ユ坊鍔燖Activate娉ㄨВ锛?/span>杩欎釜娉ㄨВ鐨勫睘鎬ф湁锛?/span>
  1. String[] group()锛氳〃绀鸿繖涓墿灞曠偣鏄睘浜庡摢缁勭殑锛岃繖閲岀粍閫氬父鍒嗕负PROVIDER鍜孋ONSUMER锛岃〃绀鸿鎵╁睍鐐硅兘鍦ㄦ湇鍔℃彁渚涜€呯锛屾垨鑰呮秷璐圭浣跨敤

  2. String[] value()锛氳〃绀虹殑鏄疷RL涓殑鏌愪釜鍙傛暟key锛屽綋鍒╃敤getActivateExtension鏂规硶鏉ュ鎵炬墿灞曠偣鏃讹紝濡傛灉浼犲叆鐨剈rl涓寘鍚殑鍙傛暟鐨勬墍鏈塳ey涓紝鍖呮嫭浜嗗綋鍓嶆墿灞曠偣涓殑value鍊硷紝閭d箞鍒欒〃绀哄綋鍓島rl鍙互浣跨敤璇ユ墿灞曠偣銆?/span>

鏈€鍚庯細
鑷虫鎴戜滑瀵逛簬dubbo 鐨勫彲鎵╁睍鏈哄埗spi婧愮爜鏈変簡涓€瀹氱殑浜嗚В锛屽悗闈㈡垜浠皢浼氬甫缁欏ぇ瀹?Spring 濡備綍鍘绘暣鍚坉ubbo銆傚枩娆㈢殑鍙互鐐硅禐鍏虫敞鍝

鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽

鈼?/p>

鈼?/p>

鈼?/p>

鈼?/p>





鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽
Java杩欑偣浜?
姣忔櫄10鐐瑰垎浜獼ava鎶€鏈€?
IT璧勮锛屼笌浣犱竴璧疯繘姝ワ紒


濡傛灉瑙夊緱鎴戠殑鍒嗕韩涓嶉敊锛屽氨闅忔墜鈥滃湪鐪嬧€?

以上是关于鏁欏コ鏈嬪弸璇绘噦绯诲垪锛歞ubbo鐨勫彲鎵╁睍鏈哄埗SPI 婧愮爜鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章

鏄撻敊鏄撴贩娣嗙煡璇嗙偣璁茶В绯诲垪锛堜節锛夆€斿缓绔嬪姩鎬佽鍒掓ā鍨嬬殑鍩烘湰姝ラ

闇夐湁楂樻竻澹佺焊 | Taylor Swift

銆婅绠楁満缃戠粶绯诲垪銆嬧€斺€斾负浠€涔堥渶瑕佷簲灞傜綉缁滄ā鍨嬶紵