天天看點

FeignClient注解及參數

一、FeignClient注解

  FeignClient注解被@Target(ElementType.TYPE)修飾,表示FeignClient注解的作用目标在接口上

1

2

3

4

5

@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标簽的常用屬性如下:

  • name:指定FeignClient的名稱,如果項目使用了Ribbon,name屬性會作為微服務的名稱,用于服務發現
  • url: url一般用于調試,可以手動指定@FeignClient調用的位址
  • decode404:當發生http 404錯誤時,如果該字段位true,會調用decoder進行解碼,否則抛出FeignException
  • configuration: Feign配置類,可以自定義Feign的Encoder、Decoder、LogLevel、Contract
  • fallback: 定義容錯的處理類,當調用遠端接口失敗或逾時時,會調用對應接口的容錯邏輯,fallback指定的類必須實作@FeignClient标記的接口
  • fallbackFactory: 工廠類,用于生成fallback類示例,通過這個屬性我們可以實作每個接口通用的容錯邏輯,減少重複的代碼
  • path: 定義目前FeignClient的統一字首

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@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);

/**

* 容錯處理類,當調用失敗時,簡單傳回空字元串

*/

@Component

public

class

DefaultFallback implements GitHubClient {

@Override

public

String searchRepo(@RequestParam(

"q"

) String queryStr) {

return

""

;

}

}

}

 在使用fallback屬性時,需要使用@Component注解,保證fallback類被Spring容器掃描到,GitHubExampleConfig内容如下:

@Configuration

public

class

GitHubExampleConfig {

@Bean

Logger.Level feignLoggerLevel() {

return

Logger.Level.FULL;

}

}

  在使用FeignClient時,Spring會按name建立不同的ApplicationContext,通過不同的Context來隔離FeignClient的配置資訊,在使用配置類時,不能把配置類放到Spring App Component scan的路徑下,否則,配置類會對所有FeignClient生效.

二、Feign Client 和@RequestMapping

目前工程中有和Feign Client中一樣的Endpoint時,Feign Client的類上不能用@RequestMapping注解否則,目前工程該endpoint http請求且使用accpet時會報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類上,當像如下代碼請求/v1/card/balance時,注意有Accept header:

Content-Type:application/json

Accept:application/json

POST http:

//localhost:7913/v1/card/balance

那麼會傳回 404。

如果不包含Accept header時請求,則是OK:

Content-Type:application/json

POST http:

//localhost:7913/v1/card/balance

或者像下面不在Feign Client上使用@RequestMapping注解,請求也是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請求逾時問題

Hystrix預設的逾時時間是1秒,如果超過這個時間尚未響應,将會進入fallback代碼。而首次請求往往會比較慢(因為Spring的懶加載機制,要執行個體化一些類),這個響應時間可能就大于1秒了

解決方案有三種,以feign為例。

方法一

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

該配置是讓Hystrix的逾時時間改為5秒

方法二

hystrix.command.default.execution.timeout.enabled: false

該配置,用于禁用Hystrix的逾時時間

方法三

feign.hystrix.enabled: false

該配置,用于索性禁用feign的hystrix。該做法除非一些特殊場景,不推薦使用。

參見:http://www.itmuch.com/spring-cloud-sum-feign/

原文位址:https://www.cnblogs.com/smiler/p/10689894.html

繼續閱讀