如何解决OpenFeign + Hystrix - 不同客户端的不同超时
我让 Hystrix 在我的 Spring Boot 应用程序中使用 Feign。
我已将 Hystrix 的默认超时设置为 10000 毫秒:
feign:
hystrix:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
问题是我有这个客户端,我们称之为 HeavyClient
,这是一个繁重的调用,有时需要更多时间并导致电路中断。
我只想为这个人增加 Hystrix 中的超时上限。可能吗?
我尝试过使用 Feign 属性,例如:
feign:
client:
config:
HeavyClient:
connectTimeout: 30000
readTimeout: 30000
但这行不通。我猜 Hystrix 不会检查 Feign 属性。
我正在使用:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
感谢任何帮助,谢谢!
解决方法
经过周转和更深入的搜索后,我在 this GitHub issue 上发现 Hystrix 命令的默认命名类似于 InterfaceName#methodNameSignature()
。
因此,例如,给定以下 @FeignClient
:
@FeignClient(name = "heavyClient",url = "${heavyclient.url}")
public interface HeavyClient {
@RequestMapping("/process/{operation}")
public ResponseEntity<Response> process(@PathVariable("operation") String operation);
}
它将被配置为:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
HeavyClient#process(String):
execution:
isolation:
thread:
timeoutInMilliseconds: 30000
不幸的是,这对我不起作用...不知道为什么...=s
所以为了解决这个问题,我注册了一个 SetterFactory
类型的 bean,它将创建给定 @FeignClient
名称的命令键:
@Bean
public SetterFactory setterFactory() {
return (target,method) -> HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(target.name()))
.andCommandKey(HystrixCommandKey.Factory.asKey(target.name()));
}
然后我可以简单地使用配置:
hystrix:
command:
heavyClient:
execution:
isolation:
thread:
timeoutInMilliseconds: 30000
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。