首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Netflix Feign和Hystrix设置请求超时

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

Stack Overflow用户
提问于 2015-11-06 02:04:27
回答 2查看 9.4K关注 0票数 7

我正在使用Feign创建一个REST客户端。我已经让我的呼叫正常工作了,但我想添加一些超时支持,并且我正在花很长时间来弄清楚如何做到这一点。

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

代码语言:javascript
复制
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构造函数中,如下所示:

代码语言:javascript
复制
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会将我指向一个非Netflix的repo。即使我使用了它,我也不知道如何配置Feign来使用它。

请告诉我,我很愚蠢,这非常简单,这会让我感到高兴,因为我已经克服了这个问题,并为不能自己解决这个问题而感到羞愧。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-07 07:53:39

事实证明,您可以使用com.netflix.config.ConfigurationManager的一个实例来设置Hystrix属性(来自com.netflix。

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

代码语言:javascript
复制
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command." + methodName + ".execution.isolation.thread.timeoutInMilliseconds", 1500);

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

为了简单起见,我创建了一个方法循环,以便可以在整个服务范围内应用超时:

代码语言:javascript
复制
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());
    }
}
票数 14
EN

Stack Overflow用户

发布于 2021-12-03 12:18:42

经过一些调试,我成功地将Hystrix超时设置如下:

代码语言:javascript
复制
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));
        };
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33551688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档