我正在尝试利用弹簧重试的重试和断路器机制。我尝试在特定功能(如下所示)中使用这两个注释(@Retryable 和@CircuitBreaker),但断路器不起作用。
@Service
public class CommandAndRetry {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);
@CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
@Retryable(
value = {TypeOneException.class},
maxAttempts = 3, backoff = @Backoff(2000))
public void retryWhenException() throws TypeOneException {
LOGGER.info("Retrying");
throw new TypeOneException();
}
@Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("SampleRetryService.recover");
throw t;
}
}
然后我尝试将功能分为两个不同的功能,分别具有@Retryable 和@CircuitBreaker。在这种情况下,重试机制不起作用。请在下面找到代码片段。
PS:exec 方法(Circuit Breaker 方法)是从控制器调用的。
@Service
public class CommandAndRetry {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);
@CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
public void exec() throws TypeOneException {
retryWhenException();
}
@Retryable(
value = {TypeOneException.class},
maxAttempts = 3, backoff = @Backoff(2000))
public void retryWhenException() throws TypeOneException {
LOGGER.info("Retrying");
throw new TypeOneException();
}
@Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("SampleRetryService.recover");
throw t;
}
}
谁能告诉它为什么会这样。
另外请告知是否存在更好的方法来实现重试和断路器。PS:我既不想使用resilience4j,也不想使用retryTemplate。