gpt4 book ai didi

hystrix - 使用 Netflix Feign 和 Hystrix 设置请求超时

转载 作者:行者123 更新时间:2023-12-04 10:16:10 26 4
gpt4 key购买 nike

我正在使用 Feign 创建一个 REST 客户端。我的电话工作正常,但我想添加一些超时支持,而且我有一段时间想弄清楚如何做到这一点。

Feign 的文档说“要将 Hystrix 与 Feign 一起使用,请将 Hystrix 模块添加到您的类路径中。然后使用 HystrixFeign 构建器。”好的,所以现在我有了这个:

service = HystrixFeign.builder()
.decoder(new GsonDecoder())
.target(ProjectService.class, URL_TO_BE_MOVED_TO_PROPS);

现在我所有的方法都返回了 HystrixCommands,我可以执行或排队,但我仍然看不到如何配置它们。

Hystrix wiki ( https://github.com/Netflix/Hystrix/wiki/Configuration ) 说配置应该像这样添加到 HystrixCommand 构造函数中:
public HystrixCommandInstance(int id) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(500)));
this.id = id;

但是我的命令是由 Feign 构建/返回的,所以我无权访问构造函数。

还有一点值得注意的是,Feign-Hystrix 自述文件 ( https://github.com/Netflix/feign/tree/master/hystrix) 说“要将 Hystrix 与 Feign 一起使用,请将 Hystrix 模块添加到您的类路径中。然后,将 Feign 配置为使用 HystrixInvocationHandler”,但是 Google 搜索 HystrixInvocationHandler 点我走向非 Netflix repo 。即使我使用了它,我也看不到如何配置 Feign 来使用它。

请告诉我我很笨,这非常简单,这会让我为自己已经解决了这个问题而感到高兴,并为自己无法解决这个问题而感到羞耻。

TL;DR:我想为我的 Feign 客户端发出的请求设置超时。怎么做?

最佳答案

事实证明,您可以使用 com.netflix.config.ConfigurationManager 的实例(来自 com.netflix.archaius:archaius-core)设置 Hystrix 属性。

Feign 使用方法名称作为 HystrixCommandKeys,因此您可以使用这些名称访问它们的属性:

    ConfigurationManager.getConfigInstance().setProperty("hystrix.command." + methodName + ".execution.isolation.thread.timeoutInMilliseconds", 1500);

这是假设您已经使用 HystrixFeign 来构建您的客户端,该客户端将每个调用包装在 HystrixCommand 对象中。

为了简化,我创建了一个方法循环,​​以便可以在整个服务范围内应用超时:
private void configureHystrix() {
Method[] methods = ProjectService.class.getMethods();

String methodName;
for(int i = 0; i < methods.length; i++) {
methodName = methods[i].getName();
ConfigurationManager.getConfigInstance().setProperty(String.format("hystrix.command.%s.execution.isolation.thread.timeoutInMilliseconds", methodName), config.getTimeoutInMillis());
}
}

关于hystrix - 使用 Netflix Feign 和 Hystrix 设置请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33551688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com