FeignClient注解及参数

一、FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上| 12345 | @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标签的常用属性如下:

  • | 12345678910111213141516171819 | @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内容如下:| 1234567 | @Configurationpublic 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时会报404Controller:| 12345678910111213 | @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| 12345678910111213 | @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:| 1234 | Content-Type:application/jsonAccept:application/json POST http://localhost:7913/v1/card/balance |
|
| | —- | —- | —- | —- |

那么会返回 404。如果不包含Accept header时请求,则是OK:| 12 | Content-Type:application/jsonPOST http://localhost:7913/v1/card/balance |
|
| | —- | —- | —- | —- |

或者像下面不在Feign Client上使用@RequestMapping注解,请求也是ok,无论是否包含Accept:| 12345678910111213 | @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.command.default.execution.isolation.thread.timeoutInMillisecondshystrix.command.default.execution.timeout.enabledhttp://www.itmuch.com/spring-cloud-sum-feign/