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

7
我正在使用Feign创建一个REST客户端。我的调用已经可以工作了,但我想添加一些超时支持,但我很难弄清楚如何做到这一点。
Feign的文档说:“要在Feign中使用Hystrix,请将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,”但是对HystrixInvocationHandler的Google搜索将我指向了一个非Netflix存储库。即使我使用了它,我也不知道如何配置Feign来使用它。
请告诉我我很愚蠢,这很简单,这会让我感到高兴,因为我已经解决了这个问题,并为自己无法独立解决它而感到羞愧。
TL;DR:我想在我的Feign客户端发出的请求上设置超时时间。 如何做?
2个回答

14

原来可以使用 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());
    }
}

对我有用。我设置了默认值而不是特定的方法。ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", 5000); - n0daft
你在哪里运行了这行代码?无论我将其设置为什么,所有的Feign客户端超时时间仍然是1000毫秒。 - bitsofinfo
我的 'configureHystrix()' 方法是在我使用 HystrixFeign 构建代理之后立即运行的: serviceProxy = HystrixFeign.builder().client(new OkHttpClient()) .target(ProjectServiceProxy.class, config.getHostURL()); configureHystrix(); - Disgruntor
1
如果您的方法有参数,它们将成为名称的一部分,例如 hystrix.command.ProjectService#someMethod(String).execution.isolation.thread.timeoutInMilliseconds,否则设置将不起作用。 - Henning

0
经过一些调试,我成功地设置了Hystrix的超时时间,如下所示:
HystrixFeign.builder()
   .setterFactory(getSetterFactory())
   .target(...);

// copy-paste feign.hystrix.SetterFactory.Default, just add andCommandPropertiesDefaults
private SetterFactory getSetterFactory() {
        return (target, method) ->  {
            String groupKey = target.name();
            String commandKey = Feign.configKey(target.type(), method);
            return HystrixCommand.Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                    .withExecutionTimeoutInMilliseconds(15000));
        };
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接