溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot中FeignClient注解的作用是什么

發(fā)布時(shí)間:2021-07-08 17:14:06 來源:億速云 閱讀:1017 作者:Leah 欄目:編程語言

這篇文章給大家介紹springboot中FeignClient注解的作用是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

一、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)容如下:

@Configurationpublic 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)404Controller:

@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/jsonAccept:application/json POST http://localhost:7913/v1/card/balance

那么會(huì)返回 404。

如果不包含Accept header時(shí)請(qǐng)求,則是OK:

Content-Type:application/jsonPOST 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)景,不推薦使用。

關(guān)于springboot中FeignClient注解的作用是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI