参数说明
属性 | 默认值 | 说明 |
---|---|---|
maxAttempts | 3 | 最大重试次数(包含最初的第一次调用) |
waitDuration | 500[ms] | 固定的重试间隔 |
intervalFunction | numOfAttempts -> waitDuration | 计算重试等待时间的函数,默认为固定值 |
retryOnResultPredicate | result -> false | 配置某个结果是否要重试 |
retryOnExceptionPredicate | throwable -> true | 配置某个异常是否要重试 |
retryExceptions | empty | 要重试的异常列表,包含子类型 |
ignoreExceptions | empty | 忽略的异常列表,包含子类型 |
配置文件
resilience4j.retry:
instances:
backendA:
maxRetryAttempts: 3
waitDuration: 10s
enableExponentialBackoff: true
exponentialBackoffMultiplier: 2
retryExceptions:
- org.springframework.web.client.HttpServerErrorException
- java.io.IOException
ignoreExceptions:
- io.github.robwin.exception.BusinessException
backendB:
maxRetryAttempts: 3
waitDuration: 10s
retryExceptions:
- org.springframework.web.client.HttpServerErrorException
- java.io.IOException
ignoreExceptions:
- io.github.robwin.exception.BusinessException
代码
@CircuitBreaker(name = "backendA", fallbackMethod = "fallback")
@RateLimiter(name = "backendA")
@Bulkhead(name = "backendA")
@Retry(name = "backendA", fallbackMethod = "fallback")
@TimeLimiter(name = "backendA")
public Mono<String> method(String param1) {
return Mono.error(new NumberFormatException());
}
private Mono<String> fallback(String param1, IllegalArgumentException e) {
return Mono.just("test");
}
private Mono<String> fallback(String param1, RuntimeException e) {
return Mono.just("test");
}
执行顺序:
Retry ( CircuitBreaker ( RateLimiter ( TimeLimiter ( Bulkhead ( Function ) ) ) ) )
Retry是最后执行
更多
spring cloud resilience4j-retry 重试
spring cloud resilience4j - Bulkhead 线程隔离 并发控制
spring cloud Resilience4j - 熔断器 CircuitBreaker
spring cloud resilience4j - RateLimiter 流控
https://resilience4j.readme.io/docs/retry