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的统一前缀
|
1
2
3
4
5
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内容如下:
|
1
2
3
4
5
6
7
|
@Configurationpublic class GitHubExampleConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; }} |
在使用FeignClient时,Spring会按name创建不同的ApplicationContext,通过不同的Context来隔离FeignClient的配置信息,在使用配置类时,不能把配置类放到Spring App Component scan的路径下,否则,配置类会对所有FeignClient生效.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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:
|
1
2
3
4
|
Content-Type:application/jsonAccept:application/jsonPOST http://localhost:7913/v1/card/balance |
那么会返回 404。
如果不包含Accept header时请求,则是OK:
|
1
2
|
Content-Type:application/jsonPOST http://localhost:7913/v1/card/balance |
或者像下面不在Feign Client上使用@RequestMapping注解,请求也是ok,无论是否包含Accept:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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.timeoutInMilliseconds: 5000
该配置是让Hystrix的超时时间改为5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
该配置,用于禁用Hystrix的超时时间
方法三
feign.hystrix.enabled: false
该配置,用于索性禁用feign的hystrix。该做法除非一些特殊场景,不推荐使用。
FeignClient注解及参数的更多相关文章
- SpringCloud 服务间互相调用 @FeignClient注解
SpringCloud搭建各种微服务之后,服务间通常存在相互调用的需求,SpringCloud提供了@FeignClient 注解非常优雅的解决了这个问题 首先,保证几个服务都在一个Eureka中注册 ...
- FeignClient注解属性configuration不生效问题排查思路
FeignClient注解属性configuration不生效问题排查思路 问题背景 我们知道,"如果需要自定义单个Feign配置,Feign的@Configuration 注解的类不能与@ ...
- 关于spring-mvc的InitBinder注解的参数
关于spring-mvc的InitBinder注解的参数 通过Spring-mvc的@InitBinder注释的方法可以对WebDataBinder做一些初始化操作.比如设置Validator. 我一 ...
- spring注解式参数校验
很痛苦遇到大量的参数进行校验,在业务中还要抛出异常或者返回异常时的校验信息,在代码中相当冗长,今天我们就来学习spring注解式参数校验. 其实就是:hibernate的validator. 开始啦. ...
- @RequestBody注解的参数仅仅读取一次的问题解决。
最近在写日志管理,想着使用拦截器加注解的方式,但是遇到了一个问题,就是如果使用@RequestBody注解接收的参数只能读取一次,造成了我在拦截器中如果接收了参数,在Controller层就接收不到了 ...
- SpringBoot注解验证参数
SpringBoot注解验证参数 废话不多说,直接上表格说明: 注解 作用类型 解释 @NotNull 任何类型 属性不能为null @NotEmpty 集合 集合不能为null,且size大于0 @ ...
- @Scheduled注解各参数详解
@Scheduled注解各参数详解 @Scheduled注解的使用可以参考这个:https://www.cnblogs.com/mengw/p/11564338.html 参数详解 1. cron 该 ...
- 那天晚上和@FeignClient注解的深度交流
废话篇 那晚,我和@FeignClient注解的深度交流了一次,爽! 主要还是在技术群里看到有同学在问相关问题,比如: contextId是干嘛的?name相同的多个Client会报错? 然后觉得有必 ...
- Swagger注解及参数细节的正确书写。
今天新开了一个api文件,结果怎么搞也在swagger里显示不出来,浪费半天后,去问老员工了. 一般有俩原因, 1.idea缓存,重启idea即可. 2.注解和参数上的修饰有问题,或者请求method ...
随机推荐
- MIP技术交流分享(3月9日)
3月9日上周四下午,MIP 团队工程师与去哪儿酒店云.众荟的 Web 前端工程师进行了一次面对面的技术交流. 在这次交流中,MIP 工程师主要分享了 MIP 技术原理,MIP 加速原理,以及 MIP ...
- Spark学习之数据读取与保存总结(二)
8.Hadoop输入输出格式 除了 Spark 封装的格式之外,也可以与任何 Hadoop 支持的格式交互.Spark 支持新旧两套Hadoop 文件 API,提供了很大的灵活性. 要使用新版的 Ha ...
- requests使用“proxy”代理访问接口
在requests中使用proxy代理访问 使用前先更新requests版本为支持socks的版本. 先pip安装对应库: >> pip install -U requests[so ...
- ab性能测试工具的使用
一.什么是ab ab,即Apache Benchmark,是一种用于测试Apache超文本传输协议(HTTP)服务器的工具. ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访 ...
- 『性能』ServiceStack.Redis 和 StackExchange.Redis 性能比较
背景 近来,需要用到 Redis 这类缓存技术 —— MongoDB 和 Redis 没有进行过比较. 我也懒得在这些细节上 纠结那么多 —— 按照网友给出的文章,听从网友建议,选择 Redis. R ...
- 微服务(入门一):netcore安装部署consul
环境准备 vs开发环境:vs2017 consul版本: 1.4.4 netcore版本:2.1 安裝Consul 1.从官网下载consul到本地,选择系统对应的版本进行下载到本地,下载地址:h ...
- nginx漏洞分析与升级修复
一 .此次漏洞分析 1 nginx HTTP/2漏洞 [nginx-announce] nginx安全公告(CVE-2018-16843,CVE-2018-16844)在nginx HTTP / 2实 ...
- vue 中使用sass实现主体换肤
有如下代码要实现换肤功能 <template> <div class="app-root" :class="themeClass"> & ...
- 在Docker中体验数据库之Microsoft SQL Server
前面记录了一下在docker中体验mongodb和mysql.今天记录一下mssql……其实早就体验了,就是没有记录,前几天看了一下2019的一些新闻,很喜欢Polybase这个特性,想体验一把,可惜 ...
- pytest进阶之xunit fixture
前言 今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearD ...