1. feign鐨勪娇鐢ㄥ強鍘熺悊
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. feign鐨勪娇鐢ㄥ強鍘熺悊相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%96%b9%e6%a1%88' title='鏂规'>鏂规
ctp cloud tpc 渚濊禆 涓嶇敤 bsp tar 缂栧啓涓€:Feign浠嬬粛
浜? Ribbon VS Feign
feign鍜宺ibbon鏄疭pring Cloud鐨凬etflix涓彁渚涚殑涓や釜瀹炵幇杞礋杞藉潎琛$殑缁勪欢锛孯ibbon鍜孎eign閮芥槸鐢ㄤ簬璋冪敤鍏朵粬鏈嶅姟鐨勶紝鏂瑰紡涓嶅悓銆?span style="color: #ff0000;">Feign鍒欐槸鍦≧ibbon鐨勫熀纭€涓婅繘琛屼簡涓€娆℃敼杩涳紝閲囩敤鎺ュ彛鐨勬柟寮忋€?br />灏嗛渶瑕佽皟鐢ㄧ殑鍏朵粬鏈嶅姟鐨勬柟娉曞畾涔夋垚鎶借薄鏂规硶鍗冲彲锛屼笉闇€瑕佽嚜宸辨瀯寤?http 璇锋眰銆?br />涓嶈繃瑕佹敞鎰忕殑鏄娊璞℃柟娉曠殑娉ㄨВ銆佹柟娉曠鍚嶈鍜屾彁渚涙湇鍔$殑鏂规硶瀹屽叏涓€鑷?br />
1.鍚姩绫讳娇鐢ㄧ殑娉ㄨВ涓嶅悓锛孯ibbon 鐢ㄧ殑鏄疈RibbonClient锛孎eign 鐢ㄧ殑鏄疈EnableFeignClients銆?/p>
2.鏈嶅姟鐨勬寚瀹氫綅缃笉鍚岋紝Ribbon 鏄湪@RibbonClient 娉ㄨВ涓婂0鏄庯紝Feign 鍒欐槸鍦ㄥ畾涔夋娊璞℃柟娉曠殑鎺ュ彛涓娇鐢ˊFeignClient 澹版槑銆?/p>
3.璋冪敤鏂瑰紡涓嶅悓锛孯ibbon 闇€瑕佽嚜宸辨瀯寤?http 璇锋眰锛屾ā鎷?http 璇锋眰鐒跺悗浣跨敤 RestTemplate 鍙戦€佺粰鍏朵粬鏈嶅姟锛屾楠ょ浉褰撶箒鐞愩€?/strong>
涓汉瑙夊緱涓昏鍘熷洜鏄繖涓?/p>
Feign
Feign 鏄湪 Ribbon 鐨勫熀纭€涓婅繘琛屼簡涓€娆℃敼杩涳紝鏄竴涓娇鐢ㄨ捣鏉ユ洿鍔犳柟渚跨殑 HTTP 瀹㈡埛绔€傞噰鐢ㄦ帴鍙g殑鏂瑰紡锛?鍙渶瑕佸垱寤轰竴涓帴鍙o紝鐒跺悗鍦ㄤ笂闈㈡坊鍔犳敞瑙e嵆鍙?锛屽皢闇€瑕佽皟鐢ㄧ殑鍏朵粬鏈嶅姟鐨勬柟娉曞畾涔夋垚鎶借薄鏂规硶鍗冲彲锛?涓嶉渶瑕佽嚜宸辨瀯寤?http 璇锋眰銆傜劧鍚庡氨鍍忔槸璋冪敤鑷韩宸ョ▼鐨勬柟娉曡皟鐢紝鑰屾劅瑙変笉鍒版槸璋冪敤杩滅▼鏂规硶锛屼娇寰楃紪鍐?瀹㈡埛绔彉寰楅潪甯稿鏄撱€?/p>
Ribbon
Ribbon 鏄竴涓熀浜?HTTP 鍜?TCP 瀹㈡埛绔?鐨勮礋杞藉潎琛$殑宸ュ叿銆傚畠鍙互 鍦ㄥ鎴风 閰嶇疆 RibbonServerList锛堟湇鍔$鍒楄〃锛夛紝浣跨敤 HttpClient 鎴?RestTemplate 妯℃嫙 http 璇锋眰锛屾楠ょ浉瀵圭箒鐞愩€?/p>
涓? 鍦ㄥ伐绋嬩腑寮曞叆feign
1. 鍔犲叆feign渚濊禆
<!-- 鏈嶅姟涔嬮棿http璋冪敤, 寮曞叆feign --> <dependency> 銆€銆€<groupId>org.springframework.cloud</groupId> 銆€銆€<artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2. 娣诲姞娉ㄨВ
鍦ㄥ惎鍔ㄧ被澧炲姞娉ㄨВ
@EnableFeignClients
3. 淇敼宸ョ▼涓烘櫘閫氱殑jar
鎴戜滑鍗冲皢鍒涘缓鐨勮繖涓伐绋? 涓撻棬鐢ㄦ潵绠$悊缁存姢feign璋冪敤璇锋眰
鍥犱负璇ュ伐绋嬪紡涓€涓櫘閫氱殑jar 涓嶉渶瑕佹墦鍙墽琛岀殑jar
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId> 5
<artifactId>maven鈥恓ar鈥恜lugin</artifactId>
</plugin>
</plugins>
</build>
4. 缂栧啓澹版槑寮忔帴鍙?/h2>
@FeignClient(name="product鈥恈enter")
publicinterfaceProductCenterFeignApi{
銆€銆€/**
銆€銆€* 澹版槑寮忔帴鍙?杩滅▼璋冪敤http://product鈥恈enter/selectProductInfoById/{productNo}
銆€銆€* @param productNo
銆€銆€* @return
銆€銆€*/
銆€銆€@RequestMapping("/selectProductInfoById/{productNo}")
銆€銆€ProductInfo selectProductInfoById(@PathVariable("productNo") String productNo);
}
鍦╫rder椤圭洰涓厤缃?/em>
5. 缁嗙矑搴﹂厤缃?/h2>
5.1 璁剧疆鏃ュ織鎵撳嵃绾у埆
@FeignClient(name="product鈥恈enter") publicinterfaceProductCenterFeignApi{ 銆€銆€/** 銆€銆€* 澹版槑寮忔帴鍙?杩滅▼璋冪敤http://product鈥恈enter/selectProductInfoById/{productNo} 銆€銆€* @param productNo 銆€銆€* @return 銆€銆€*/ 銆€銆€@RequestMapping("/selectProductInfoById/{productNo}") 銆€銆€ProductInfo selectProductInfoById(@PathVariable("productNo") String productNo); }
鍦╫rder椤圭洰涓厤缃?/em>
5.1 璁剧疆鏃ュ織鎵撳嵃绾у埆
as
以上是关于1. feign鐨勪娇鐢ㄥ強鍘熺悊的主要内容,如果未能解决你的问题,请参考以下文章
鐮佸啘鎵嬭 | Spring WebFlux瀹炴垬浠ュ強鍘熺悊娴呮瀽