我有一个 spring boot 应用程序,它迭代地调用 mockserver通过 hystrix 命令实例化,带有回退方法。
模拟服务器被配置为始终以状态代码 500 响应。当在没有 circuitBreaker.sleepWindowInMilliseconds 的情况下运行时,一切正常,对模拟服务器的调用完成,然后调用回退方法。
将 circuitBreaker.sleepWindowInMilliseconds 值配置为 5 分钟左右后,我预计在 5 分钟内不会对模拟服务器进行任何调用,所有调用都被定向到回退方法,但这不是案例。
看起来 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());
最佳答案
发现问题: 此属性 (...circuitBreaker.sleepWindowInMilliseconds ) 与另一个属性 (...circuitBreaker.requestVolumeThreshold ) 一起工作。 如果没有特别将此默认值设置为 20,这意味着第一个 hystrix 将尝试以通常的方式连接 20 次,并且只有在之后 sleepWindowInMilliseconds 才会被激活并且只会进行回退。
此外,仅当失败调用的百分比超过 circuitBreaker.errorThresholdPercentage 时熔断器才会打开 同时,失败调用的总数超过了 circuitBreaker.requestVolumeThreshold,所有这些都在 metrics.rollingStats.timeInMilliseconds
的窗口内关于java - Hystrix circuitBreaker.sleepWindowInMilliseconds 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45460259/