英文:
Handle exception after reaching max attempts in resilience4j-retry using Spring Boot
问题
我有一个场景,我想记录每次重试尝试以及当最后一次尝试失败(即达到maxAttempts
次数)时抛出异常,假设还会创建一个数据库条目。
我尝试使用Resilience4j-retry与Spring Boot来实现这一点,因此我使用application.yml
和注解。
@Retry(name = "default", fallbackMethod="fallback")
@CircuitBreaker(name = "default", fallbackMethod="fallback")
public ResponseEntity<List<Person>> person() {
return restTemplate.exchange(...); // 假设这总是抛出500
}
回退方法将异常的原因记录到应用程序日志中。
public ResponseEntity<?> fallback(Exception e) {
var status = HttpStatus.INTERNAL_SERVER_ERROR;
var cause = "未知原因";
if (e instanceof ResourceAccessException) {
var resourceAccessException = (ResourceAccessException) e;
if (e.getCause() instanceof ConnectTimeoutException) {
cause = "连接超时";
}
if (e.getCause() instanceof SocketTimeoutException) {
cause = "读取超时";
}
} else if (e instanceof HttpServerErrorException) {
var httpServerErrorException = (HttpServerErrorException) e;
cause = "服务器错误";
} else if (e instanceof HttpClientErrorException) {
var httpClientErrorException = (HttpClientErrorException) e;
cause = "客户端错误";
} else if (e instanceof CallNotPermittedException) {
var callNotPermittedException = (CallNotPermittedException) e;
cause = "断路器打开";
}
var message = String.format("%s 导致回退,捕获异常 %s",
cause, e.getMessage());
log.error(message); // 应用程序日志条目
throw new MyRestException (message, e);
}
当我调用这个person()
方法时,重试会发生,直到达到配置的maxAttempt
次数。我期望我的自定义运行时异常MyRestException
在每次重试时被捕获,并在最后一次重试(达到maxAttempt
次数)时被抛出,因此我在try-catch中包装了调用。
public List<Person> person() {
try {
return myRestService.person().getBody();
} catch (MyRestException ex) {
log.error("在这里,我准备将问题记录到数据库中");
throw new ex;
}
}
不幸的是,重试似乎被忽略了,因为回退遇到并重新抛出异常,该异常立即在我的try-catch
中捕获,而不是使用Resilience4j-retry机制。
如何在达到maxAttempts
时实现所需的行为?是否有办法为这种情况定义特定的回退方法?
英文:
I have a scenario I want to log each retry attempt and when the last one fails (i.e. maxAttempts
reached) a exception is thrown and let's say an entry to a database is created.
I try to achieve this using Resilience4j-retry with Spring Boot, therefore I use application.yml
and annotations.
@Retry(name = "default", fallbackMethod="fallback")
@CircuitBreaker(name = "default", fallbackMethod="fallback")
public ResponseEntity<List<Person>> person() {
return restTemplate.exchange(...); // let's say this always throws 500
}
The fallback logs the cause of the exception into an application log.
public ResponseEntity<?> fallback(Exception e) {
var status = HttpStatus.INTERNAL_SERVER_ERROR;
var cause = "Something unknown";
if (e instanceof ResourceAccessException) {
var resourceAccessException = (ResourceAccessException) e;
if (e.getCause() instanceof ConnectTimeoutException) {
cause = "Connection timeout";
}
if (e.getCause() instanceof SocketTimeoutException) {
cause = "Read timeout";
}
} else if (e instanceof HttpServerErrorException) {
var httpServerErrorException = (HttpServerErrorException) e;
cause = "Server error";
} else if (e instanceof HttpClientErrorException) {
var httpClientErrorException = (HttpClientErrorException) e;
cause = "Client error";
} else if (e instanceof CallNotPermittedException) {
var callNotPermittedException = (CallNotPermittedException) e;
cause = "Open circuit breaker";
}
var message = String.format("%s caused fallback, caught exception %s",
cause, e.getMessage());
log.error(message); // application log entry
throw new MyRestException (message, e);
}
When I call this method person()
the retry happens as maxAttempt
configured. I expect my custom runtime MyRestException
is caught on each retry and thrown on the last one (when maxAttempt
is reached), so I wrap the call in the try-catch.
public List<Person> person() {
try {
return myRestService.person().getBody();
} catch (MyRestException ex) {
log.error("Here I am ready to log the issue into the database");
throw new ex;
}
}
Unfortunatelly, the retry seems to be ignored as the fallback encounters and rethrows the exception that is immediatelly caught with my try-catch
instead of the Resilience4j-retry mechanism.
How to achieve the behavior when the maxAttempts
is hit? Is there a way to define a specific fallback method for such case?
答案1
得分: 1
为什么不在您的服务方法内部捕获并将异常映射到MyRestException
?例如,myRestService.person()
?这会使您的配置更加简单,因为您只需要将MyRestException
添加到RetryConfig和CircuitBreakerConfig的配置中。
Spring RestTemplate还具有机制,可以注册自定义的ResponseErrorHandler,如果您不想将样板代码添加到每个服务方法中。-> https://www.baeldung.com/spring-rest-template-error-handling
我不会将CallNotPermittedException
映射到MyRestException
。在断路器打开时,您不希望重试。将CallNotPermittedException
添加到RetryConfig中的忽略异常列表中。
我认为您根本不需要回退机制。我认为将异常映射到另一个异常不是一种“回退”。
英文:
Why don't you catch and map exceptions to MyRestException
inside of your Service methods, e.g. myRestService.person()
?
It makes your configuration even simpler, because you only have to add MyRestException
to the configuration of your RetryConfig and CircuitBreakerConfig.
Spring RestTemplate also has mechanisms to register a custom ResponseErrorHandler, if you don't want to add the boilerplate code to every Service method. -> https://www.baeldung.com/spring-rest-template-error-handling
I would not map CallNotPermittedException
to MyRestException
. You don't want to retry when the CircuitBreaker is open. Add CallNotPermittedException
to the list of ignored exceptions in your RetryConfig.
I think you don't need the fallback mechanism at all. I thing mapping an exception to another exception is not a "fallback".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论