springboot FeignClient注解及参数
一、FeignClient注解
FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上
@FeignClient(name="github-client",url="https://api.github.com",configuration=GitHubExampleConfig.class) publicinterfaceGitHubClient{ @RequestMapping(value="/search/repositories",method=RequestMethod.GET) StringsearchRepo(@RequestParam("q")StringqueryStr); }
声明接口之后,在代码中通过@Resource注入之后即可使用。@FeignClient标签的常用属性如下:
- name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
- url:url一般用于调试,可以手动指定@FeignClient调用的地址
- decode404:当发生http404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
- configuration:Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
- fallback:定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
- fallbackFactory:工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
- path:定义当前FeignClient的统一前缀
@FeignClient(name="github-client", url="https://api.github.com", configuration=GitHubExampleConfig.class, fallback=GitHubClient.DefaultFallback.class) publicinterfaceGitHubClient{ @RequestMapping(value="/search/repositories",method=RequestMethod.GET) StringsearchRepo(@RequestParam("q")StringqueryStr); /** *容错处理类,当调用失败时,简单返回空字符串 */ @Component publicclassDefaultFallbackimplementsGitHubClient{ @Override publicStringsearchRepo(@RequestParam("q")StringqueryStr){ return""; } } }
在使用fallback属性时,需要使用@Component注解,保证fallback类被Spring容器扫描到,GitHubExampleConfig内容如下:
@Configuration publicclassGitHubExampleConfig{ @Bean Logger.LevelfeignLoggerLevel(){ returnLogger.Level.FULL; } }
在使用FeignClient时,Spring会按name创建不同的ApplicationContext,通过不同的Context来隔离FeignClient的配置信息,在使用配置类时,不能把配置类放到SpringAppComponentscan的路径下,否则,配置类会对所有FeignClient生效.
二、FeignClient和@RequestMapping
当前工程中有和FeignClient中一样的Endpoint时,FeignClient的类上不能用@RequestMapping注解否则,当前工程该endpointhttp请求且使用accpet时会报404
Controller:
@RestController @RequestMapping("/v1/card") publicclassIndexApi{ @PostMapping("balance") @ResponseBody publicInfoindex(){ Info.Builderbuilder=newInfo.Builder(); builder.withDetail("x",2); builder.withDetail("y",2); returnbuilder.build(); } }
FeignClient
@FeignClient( name="card", url="http://localhost:7913", fallback=CardFeignClientFallback.class, configuration=FeignClientConfiguration.class ) @RequestMapping(value="/v1/card") publicinterfaceCardFeignClient{ @RequestMapping(value="/balance",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE) Infoinfo(); }
if@RequestMappingisusedonclass,wheninvokehttp/v1/card/balance,likethis:
如果@RequestMapping注解被用在FeignClient类上,当像如下代码请求/v1/card/balance时,注意有Acceptheader:
Content-Type:application/json Accept:application/json POSThttp://localhost:7913/v1/card/balance
那么会返回404。
如果不包含Acceptheader时请求,则是OK:
Content-Type:application/json POSThttp://localhost:7913/v1/card/balance
或者像下面不在FeignClient上使用@RequestMapping注解,请求也是ok,无论是否包含Accept:
@FeignClient( name="card", url="http://localhost:7913", fallback=CardFeignClientFallback.class, configuration=FeignClientConfiguration.class ) publicinterfaceCardFeignClient{ @RequestMapping(value="/v1/card/balance",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE) Infoinfo(); }
三、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。该做法除非一些特殊场景,不推荐使用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。