1

下面是我的 Hystrix 命令配置:

@HystrixCommand(fallbackMethod = "fall", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000") })
    public <T> T getInfo(Class clazz) {....}

回退方法:

public <T> T fall(Class clazz) {
        System.out.println("fallback");
        throw new CustomRuntimeException("API Down"); 
    }

我了解根据以下配置,即

@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000") }

5 个请求将在 10 秒内被允许,直到电路跳闸打开并且来自第 5 个请求的每个请求都将被拒绝,并且由于我在后备方法中抛出异常,它将被包装为HystrixRuntimeException.

但我面临以下问题:

  • 直到电路跳闸打开,回退正常执行并抛出CustomRuntimeException(注意:Hystrix Command 方法也抛出CustomRuntimeException
  • 电路跳闸打开后,我得到Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: getInfo short-circuited and fallback failed.

问题

  1. 为什么在电路打开之前异常没有包装为 HystrixRuntimeException 即当前回退正常执行并抛出 CustomRuntimeException 直到电路打开?*

Hystrix 的工作原理

  1. 为什么在流程 1->2->3->4->5->6->8 中,即使在失败(即 Throwing CustomRuntimeException)之后执行回退方法并且不会抛出 Wrapped HystrixRuntimeException,这发生在流程 1 的情况下->2->3->4->8 和 1->2->3->5->8
4

1 回答 1

4

有关异常处理,请参阅 Hystrix Javanica 文档:https ://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#error-propagation

引用文档:

“值得注意的是,默认情况下,调用者总是会得到根本原因异常......绝不会出现 HystrixBadRequestException 或 HystrixRuntimeException”

“如果命令有回退,那么只有触发回退逻辑的第一个异常才会传播给调用者。”

这两个引号回答了您的第一个问题:第一个方法的异常将是抛出的异常,而不是 HystrixRuntimeException。将永远不会显示回退中引发的异常。

当断路器打开时,将抛出 RuntimeException。同样,在回退中抛出的异常将永远不会显示。

我为此写了一个测试用例:https ://github.com/ahus1/hystrix-spring-examples/blob/master/src/test/java/de/ahus1/hystrixspring/HystrixExceptionHandlingInSpring.java

旁注:

  1. 您将 cicruit 断路器的配置添加到代码中。它们通常最好用它们的全名放在弹簧配置中:hystrix.command.default.circuitBreaker.requestVolumeThreshold.

  2. requestVolumeThreshold 的工作方式与您描述的有所不同:它定义了在允许触发 cicruit 断路器之前的时间窗口中所需的最小请求数。errorThresholdPercentage 是达到最小请求数(在您的情况下为 5 个)后允许的错误百分比。在您的情况下,5 个呼叫中有 5 个失败,即 100%。100% 大于 50%(断路器的默认值),因此它打开。

于 2017-09-02T19:45:17.397 回答