我试图将重试策略与CircuitBreaker模式与故障安全结合起来,但当尝试打开电路并中断时,我会得到一个CircuitBreakerOpenException异常。
https://github.com/jhalterman/failsafe
这个问题是由设置一个延迟重试小于电路的关闭时间产生的。
如何控制此异常,使重试策略不被中断?我之所以想这样做,是因为我可以让多个同时执行的实例向rest服务发出请求,并且不会中断重试。
我的代码:
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)发布于 2018-03-15 06:45:29
下面是我在本例中尝试将断路器与重试策略相结合时的理解:
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);发布于 2018-12-11 16:13:42
这就是预期的行为。当电路打开时,对那个端点的请求被停止--如果依赖被破坏或不可靠,当然你的重试也必须被切断,否则你会很容易地经历一堆重试,就像你根本没有cb一样。
如果您想继续发送请求,您的触发断开电路太敏感,或者您处于不需要中断电路的情况。更有可能的是,故障安全强制执行的行为是正确的方法。
如果您真的想这样做,请将重试逻辑放在发送到断路器的函数中,而不是使用来自故障安全的RetryPolicy。或者在重试策略中使用指数退避函数。
https://stackoverflow.com/questions/48025140
复制相似问题