gpt4 book ai didi

java - 使用 RetryPolicy 和 CircuitBreaker 进行故障保护会抛出 CircuitBreakerOpenException

转载 作者:行者123 更新时间:2023-11-30 02:17:02 29 4
gpt4 key购买 nike

我尝试将重试策略与带有 Failsafe 的 CircuitBreaker 模式结合起来,但当尝试打开电路并中断时,我收到了 CircuitBreakerOpenException 异常。

https://github.com/jhalterman/failsafe

问题是由于设置的重试延迟小于电路的关闭时间而产生的。

如何控制这个异常,使重试策略不被中断?我想这样做是因为我可以让多个实例同时向休息服务发起请求,并且重试不会中断。

我的代码:

public class UnstableApplication {
private final int MAX_FAILS = 4;
private AtomicInteger failCount = new AtomicInteger(1);

public String generateId() throws Exception {
if (failCount.getAndIncrement() < MAX_FAILS) {
System.err.printf("UnstableApplication throws SampleException at '%s'\n", ZonedDateTime.now());
throw new Exception();
}

final String id = UUID.randomUUID().toString();
System.out.printf("UnstableApplication: id '%s' generated at '%s'\n", id, ZonedDateTime.now());

return id;
}

}

public class FailsafeExample {

public static void main(String[] args) throws Exception {

UnstableApplication app = new UnstableApplication();

RetryPolicy retryPolicy = new RetryPolicy()
.retryOn(Exception.class)
.withDelay(2, TimeUnit.SECONDS)
.withMaxRetries(5);

CircuitBreaker breaker = new CircuitBreaker();
breaker.withFailureThreshold(2);
breaker.withDelay(5, TimeUnit.SECONDS);
breaker.withSuccessThreshold(3);
breaker.onOpen(() -> {
System.out.println("Circuit breaker is open");
});

breaker.onClose(() -> {
System.out.println("Circuit breaker is close");
});

breaker.onHalfOpen(() -> {
System.out.println("Circuit breaker is half-close");
});

Failsafe.with(retryPolicy)
.with(breaker)
.onFailedAttempt((a, b) -> {
System.out.println(
String.format("Failed with exception: %s, at %s, circuit-breaker state is: %s",
b, ZonedDateTime.now(), breaker.getState()));
})
.onSuccess(cxn -> {
System.out.println("Succcess!");
})
.onFailure(cxn -> {
System.out.println("Failed!");
})
.get(new Callable<String>() {
@Override
public String call() throws Exception {
return app.generateId();
}
});
}
}

我的结果:

UnstableApplication throws SampleException at '2019-05-31T16:30:09.214Z[Etc/UTC]'
Failed with exception: java.lang.Exception, at 2019-05-31T16:30:09.221Z[Etc/UTC], circuit-breaker state is: CLOSED
UnstableApplication throws SampleException at '2019-05-31T16:30:11.229Z[Etc/UTC]'
Circuit breaker is open
Failed with exception: java.lang.Exception, at 2019-05-31T16:30:11.230Z[Etc/UTC], circuit-breaker state is: OPEN
Exception in thread "main" net.jodah.failsafe.CircuitBreakerOpenException
at net.jodah.failsafe.SyncFailsafe.call(SyncFailsafe.java:136)
at net.jodah.failsafe.SyncFailsafe.get(SyncFailsafe.java:56)
at com.kash.test.Foo.main(Foo.java:63)

最佳答案

以下是我在此示例中尝试将断路器与重试策略结合使用时的理解:

CircuitBreaker breaker = new CircuitBreaker()
.withFailureThreshold(3, 10)
.withSuccessThreshold(5)
.failOn(Exception.class)
.withDelay(30, TimeUnit.SECONDS);

RetryPolicy retryPolicy = new RetryPolicy()
.retryOn(ConnectException.class)
.withDelay(3, TimeUnit.SECONDS)
.withMaxRetries(5);
  • 首先,它会重试 5 次,但由于断路器的阈值为 3,因此在第 3 次“连续”重试尝试时,它将断开并开路。
  • 因此,一旦打开,它可能“仍可能重试”,但由于电路已经打开,因此在时间延迟下重试将导致 CircuitBreakerOpenException。

关于java - 使用 RetryPolicy 和 CircuitBreaker 进行故障保护会抛出 CircuitBreakerOpenException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48025140/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com