springboot FeignClient注解及參數(shù)
一、FeignClient注解
FeignClient注解被@Target(ElementType.TYPE)修飾,表示FeignClient注解的作用目標(biāo)在接口上
@FeignClient(name = "github-client", url = "https://api.github.com", configuration = GitHubExampleConfig.class) public interface GitHubClient { @RequestMapping(value = "/search/repositories", method = RequestMethod.GET) String searchRepo(@RequestParam("q") String queryStr); }
聲明接口之后,在代碼中通過@Resource注入之后即可使用。@FeignClient標(biāo)簽的常用屬性如下:
- name:指定FeignClient的名稱,如果項(xiàng)目使用了Ribbon,name屬性會(huì)作為微服務(wù)的名稱,用于服務(wù)發(fā)現(xiàn)
- url: url一般用于調(diào)試,可以手動(dòng)指定@FeignClient調(diào)用的地址
- decode404:當(dāng)發(fā)生http 404錯(cuò)誤時(shí),如果該字段位true,會(huì)調(diào)用decoder進(jìn)行解碼,否則拋出FeignException
- configuration: Feign配置類,可以自定義Feign的Encoder、Decoder、LogLevel、Contract
- fallback: 定義容錯(cuò)的處理類,當(dāng)調(diào)用遠(yuǎn)程接口失敗或超時(shí)時(shí),會(huì)調(diào)用對(duì)應(yīng)接口的容錯(cuò)邏輯,fallback指定的類必須實(shí)現(xiàn)@FeignClient標(biāo)記的接口
- fallbackFactory: 工廠類,用于生成fallback類示例,通過這個(gè)屬性我們可以實(shí)現(xiàn)每個(gè)接口通用的容錯(cuò)邏輯,減少重復(fù)的代碼
- path: 定義當(dāng)前FeignClient的統(tǒng)一前綴
@FeignClient(name = "github-client", url = "https://api.github.com", configuration = GitHubExampleConfig.class, fallback = GitHubClient.DefaultFallback.class) public interface GitHubClient { @RequestMapping(value = "/search/repositories", method = RequestMethod.GET) String searchRepo(@RequestParam("q") String queryStr); /** * 容錯(cuò)處理類,當(dāng)調(diào)用失敗時(shí),簡(jiǎn)單返回空字符串 */ @Component public class DefaultFallback implements GitHubClient { @Override public String searchRepo(@RequestParam("q") String queryStr) { return ""; } } }
在使用fallback屬性時(shí),需要使用@Component注解,保證fallback類被Spring容器掃描到,GitHubExampleConfig內(nèi)容如下:
@Configuration public class GitHubExampleConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
在使用FeignClient時(shí),Spring會(huì)按name創(chuàng)建不同的ApplicationContext,通過不同的Context來隔離FeignClient的配置信息,在使用配置類時(shí),不能把配置類放到Spring App Component scan的路徑下,否則,配置類會(huì)對(duì)所有FeignClient生效.
二、Feign Client 和@RequestMapping
當(dāng)前工程中有和Feign Client中一樣的Endpoint時(shí),F(xiàn)eign Client的類上不能用@RequestMapping注解否則,當(dāng)前工程該endpoint http請(qǐng)求且使用accpet時(shí)會(huì)報(bào)404
Controller:
@RestController @RequestMapping("/v1/card") public class IndexApi { @PostMapping("balance") @ResponseBody public Info index() { Info.Builder builder = new Info.Builder(); builder.withDetail("x", 2); builder.withDetail("y", 2); return builder.build(); } }
Feign Client
@FeignClient( name = "card", url = "http://localhost:7913", fallback = CardFeignClientFallback.class, configuration = FeignClientConfiguration.class ) @RequestMapping(value = "/v1/card") public interface CardFeignClient { @RequestMapping(value = "/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) Info info(); }
if @RequestMapping is used on class, when invoke http /v1/card/balance, like this :
如果 @RequestMapping注解被用在FeignClient類上,當(dāng)像如下代碼請(qǐng)求/v1/card/balance時(shí),注意有Accept header:
Content-Type:application/json Accept:application/json POST http://localhost:7913/v1/card/balance
那么會(huì)返回 404。
如果不包含Accept header時(shí)請(qǐng)求,則是OK:
Content-Type:application/json POST http://localhost:7913/v1/card/balance
或者像下面不在Feign Client上使用@RequestMapping注解,請(qǐng)求也是ok,無論是否包含Accept:
@FeignClient( name = "card", url = "http://localhost:7913", fallback = CardFeignClientFallback.class, configuration = FeignClientConfiguration.class ) public interface CardFeignClient { @RequestMapping(value = "/v1/card/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) Info info(); }
三、Feign請(qǐng)求超時(shí)問題
Hystrix默認(rèn)的超時(shí)時(shí)間是1秒,如果超過這個(gè)時(shí)間尚未響應(yīng),將會(huì)進(jìn)入fallback代碼。而首次請(qǐng)求往往會(huì)比較慢(因?yàn)镾pring的懶加載機(jī)制,要實(shí)例化一些類),這個(gè)響應(yīng)時(shí)間可能就大于1秒了
解決方案有三種,以feign為例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
該配置是讓Hystrix的超時(shí)時(shí)間改為5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
該配置,用于禁用Hystrix的超時(shí)時(shí)間
方法三
feign.hystrix.enabled: false
該配置,用于索性禁用feign的hystrix。該做法除非一些特殊場(chǎng)景,不推薦使用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java之Spring認(rèn)證使用Profile配置運(yùn)行環(huán)境講解
這篇文章主要介紹了Java之Spring認(rèn)證使用Profile配置運(yùn)行環(huán)境講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07tdesign的文件上傳功能實(shí)現(xiàn)(微信小程序+idea的springboot)
這篇文章主要介紹了tdesign的文件上傳(微信小程序+idea的springboot)的相關(guān)知識(shí),本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-09-09springboot下添加全局異常處理和自定義異常處理的過程解析
在spring項(xiàng)目中,優(yōu)雅處理異常,好處是可以將系統(tǒng)產(chǎn)生的全部異常統(tǒng)一捕獲處理,自定義的異常也由全局異常來捕獲,如果涉及到validator參數(shù)校驗(yàn)器使用全局異常捕獲也是較為方便,這篇文章主要介紹了springboot下添加全局異常處理和自定義異常處理,需要的朋友可以參考下2023-12-12基于maven使用IDEA創(chuàng)建多模塊項(xiàng)目
這篇文章主要介紹了基于maven使用IDEA創(chuàng)建多模塊項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04