【问题标题】:How to set set a HystrixProperty to a Feign request with spring cloud?如何使用 Spring Cloud 将 Hystrix 属性设置为 Feign 请求?
【发布时间】:2017-01-13 00:27:06
【问题描述】:

根据文档,当使用 Feign 和 Hystrix 时,每个请求都会包装到一个 Hystrix 命令中。

是否可以为这些命令设置 Hystrix 属性?我想做这样的事情:

@RequestMapping(commandProperties = {
    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000")})
List<Team> findAll();

或:

@FeignClient(name = "teams", commandProperties = {
    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000")})

作为记录,我已经尝试过使用属性,但没有成功。这些正在工作:

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
hystrix.command.findAll.execution.timeout.enabled=false
hystrix.command.default.execution.timeout.enabled=false

但是这个没有:

hystri‌​x.command.findAll.ex‌​ecution.isolation.thread.timeoutInMillis‌​econds=20000

确实,我们可以在HystrixCommandProperties 类中读入以下注释:

    //this property name is now misleading.  //TODO figure out a good way to deprecate this property name
    this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);

编辑:我尝试使用 feign 的 Request.Option 但这些属性似乎没有传播到 hystrix。

【问题讨论】:

  • 基于您尝试设置的属性,我假设您正在尝试处理一些请求超时,您有堆栈跟踪吗?是否涉及 Ribbon?
  • 远程服务响应时间有点长,所以我得到了HystrixRuntimeException异常。
  • 你只设置了application.properties中的属性吗?
  • 我就是这么做的
  • @HystrixProperty 仅适用于 @HystrixCommand,不适用于 @RequestMapping@FeignClient。通常在application.properties 中设置 hystrix 属性是可行的。您有时缺少 hystrix.command.&lt;command&gt; 前缀。也许您有一个示例项目或者可以在问题中添加更多代码?

标签: spring spring-cloud hystrix spring-cloud-netflix netflix-feign


【解决方案1】:

问题已解决:这是一个编码问题。我从文档中复制/粘贴了一行,但它不是 UTF-8 编码的(尽管 STS 的显示是正确的)。

【讨论】:

  • 在我的例子中 hystrix.command.myFeignMethod.execution.isolation.thread.timeoutInMilliseconds= 一些值没有被选择,不要认为它是一个编码问题,因为属性在点击云配置服务器时是明显可见的跨度>
【解决方案2】:

您还可以像下面这样以编程方式设置属性。

ConfigurationManager.getConfigInstance()
        .setProperty("hystri‌​x.command.default.ex‌​ecution.isolation.th‌​read.timeoutInMillis‌​econds", 1500);

【讨论】:

    猜你喜欢
    • 2016-02-06
    • 2019-04-26
    • 2017-08-22
    • 2017-07-14
    • 1970-01-01
    • 2016-03-31
    • 2015-08-31
    • 2018-02-01
    • 2019-08-16
    相关资源
    最近更新 更多