【问题标题】:Hystrix circuitBreaker.sleepWindowInMilliseconds not workingHystrix circuitBreaker.sleepWindowInMilliseconds 不工作
【发布时间】:2018-01-09 15:28:49
【问题描述】:

我有一个 Spring Boot 应用程序,它通过 hystrix 命令迭代调用 mockserver 实例,并带有回退方法。

mockserver 被配置为总是以状态码 500 响应。在没有 circuitBreaker.sleepWindowInMilliseconds 的情况下运行时,一切正常,对 mockserver 的调用完成,然后调用 fallback 方法。

在将 circuitBreaker.sleepWindowInMilliseconds 值配置为 5 分钟左右后,我预计在 5 分钟内不会对 mockserver 进行任何调用,所有调用都被定向到 fallback 方法,但这不是案例。

看起来 circuitBreaker.sleepWindowInMilliseconds 配置被忽略了。

例如,如果我在迭代仍在运行时重新配置模拟服务以回复状态码 200,它将立即打印“模拟服务响应”,而无需等待 5 分钟。

在spring boot主应用类中:

@RequestMapping("/iterate")
  public void iterate() {
    for (int i = 1; i<100; i++ ) {
      try {
        System.out.println(bookService.readingMockService());
        Thread.sleep(3000);
      } catch (Exception e) {
        System.out.println(e.getMessage());
      }
    }
  }

在 Spring Boot 服务中:

@HystrixCommand(groupKey = "ReadingMockService", commandKey = "ReadingMockService", threadPoolKey = "ReadingMockService", fallbackMethod = "reliableMock", commandProperties = {
          @HystrixProperty(name ="circuitBreaker.sleepWindowInMilliseconds", value = "300000") })
  public String readingMockService() {
    URI uri = URI.create("http://localhost:1080/askmock");
    return this.restTemplate.getForObject(uri, String.class);
  }

模拟服务器也在同一台机器上运行,配置如下:

new MockServerClient("127.0.0.1", 1080).reset();
   new MockServerClient("127.0.0.1", 1080)
           .when(request("/askmock"))
           .respond(response()
                   .withStatusCode(500)
                   .withBody("mockservice response")
                   .applyDelay());

【问题讨论】:

  • sleepWindowInMilliseconds:{@link HystrixCircuitBreaker} 跳闸打开后,它应该在再次尝试请求之前等待的时间(以毫秒为单位)。当 mock 返回 500 时,它会在 5 分钟内重试吗?
  • 不,它实际上会立即重试 - 这就是我遇到的问题。我希望,确实会在 5 分钟内重试,但它会继续模拟而不是使用回退

标签: java spring-boot hystrix


【解决方案1】:

来自文档: https://github.com/Netflix/Hystrix/wiki/configuration#circuitBreaker.sleepWindowInMilliseconds

并通过查看源代码:

https://github.com/Netflix/Hystrix/blob/master/hystrix-core/src/main/java/com/netflix/hystrix/HystrixCommandProperties.java

使用

@HystrixProperty(name="hystrix.command.ReadingMockService.circuitBreaker.sleepWindowInMilliseconds"

应该可以。

【讨论】:

    【解决方案2】:

    发现问题: 此属性 (...circuitBreaker.sleepWindowInMilliseconds ) 与另一个属性 (...circuitBreaker.requestVolumeThreshold ) 一起使用。 如果没有特别将此默认设置为 20,这意味着第一个 hystrix 将尝试以通常的方式连接 20 次,并且只有在之后 sleepWindowInMilliseconds 才会被激活并且只会回退。

    此外,仅当失败调用的百分比超过 circuitBreaker.errorThresholdPercentage 时才会打开断路器 同时失败的调用总数超过 circuitBreaker.requestVolumeThreshold,都在 metrics.rollingStats.timeInMilliseconds

    的窗口内

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 2023-03-12
      • 2018-10-05
      • 2018-09-16
      • 2020-05-16
      • 2020-11-30
      • 2018-03-07
      • 2019-06-11
      • 2016-02-25
      相关资源
      最近更新 更多