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浠嬬粛

銆€銆€Feign 鐨勮嫳鏂囪〃鎰忎负“鍋囪锛屼吉瑁咃紝鍙樺舰”锛?鏄竴涓猦ttp璇锋眰璋冪敤鐨勮交閲忕骇妗嗘灦锛屽彲浠ヤ互Java鎺ュ彛娉ㄨВ鐨勬柟寮忚皟鐢℉ttp璇锋眰锛岃€屼笉鐢ㄥ儚Java涓€氳繃灏佽HTTP璇锋眰鎶ユ枃鐨勬柟寮忕洿鎺ヨ皟鐢ㄣ€侳eign閫氳繃澶勭悊娉ㄨВ锛屽皢璇锋眰妯℃澘鍖栵紝褰撳疄闄呰皟鐢ㄧ殑鏃跺€欙紝浼犲叆鍙傛暟锛屾牴鎹弬鏁板啀搴旂敤鍒拌姹備笂锛岃繘鑰岃浆鍖栨垚鐪熸鐨勮姹傦紝杩欑璇锋眰鐩稿鑰岃█姣旇緝鐩磋銆?
Feign琚箍娉涘簲鐢ㄥ湪Spring Cloud 鐨勮В鍐虫柟妗堜腑锛屾槸瀛︿範鍩轰簬Spring Cloud 寰湇鍔℃灦鏋勪笉鍙垨缂虹殑閲嶈缁勪欢銆?/div>
Feign鏀寔澶氱娉ㄨВ锛屼緥濡侳eign鑷甫鐨勬敞瑙f垨鑰匤AX­RS娉ㄨВ绛夈€係pring Cloud瀵笷eign杩涜浜嗗寮猴紝浣垮叾鏀寔Spring MVC娉ㄨВ锛屽彟澶栬繕鏁村悎浜哛ibbon鍜孍ureka锛?/strong>浠庤€屼娇寰桭eign鐨勪娇鐢ㄦ洿鍔犳柟渚?

 

浜? 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 璁剧疆鏃ュ織鎵撳嵃绾у埆

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

as

以上是关于1. feign鐨勪娇鐢ㄥ強鍘熺悊的主要内容,如果未能解决你的问题,请参考以下文章

鐮佸啘鎵嬭 | Spring WebFlux瀹炴垬浠ュ強鍘熺悊娴呮瀽

Zuul鐨勪娇鐢紝璺敱璁块棶鏄犲皠瑙勫垯

Spring Could Feign 璁捐鍘熺悊

(瓒呯骇璇︾粏)Docker 鏋舵瀯鍘熺悊鍙婄畝鍗曚娇鐢╗鏈夐厤濂楄棰慮

Spring Cloud Feign鍘熺悊鍙婃€ц兘

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊