spring cloud resilience4j-retry 重试

博客介绍了Spring Cloud Resilience4j相关内容,包括参数说明、配置文件和代码。阐述了组件执行顺序,Retry最后执行。还提及了spring cloud resilience4j - retry重试、Bulkhead线程隔离并发控制、CircuitBreaker熔断器、RateLimiter流控等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参数说明

属性默认值说明
maxAttempts3最大重试次数(包含最初的第一次调用)
waitDuration500[ms]固定的重试间隔
intervalFunctionnumOfAttempts -> waitDuration计算重试等待时间的函数,默认为固定值
retryOnResultPredicateresult -> false配置某个结果是否要重试
retryOnExceptionPredicatethrowable -> true配置某个异常是否要重试
retryExceptionsempty要重试的异常列表,包含子类型
ignoreExceptionsempty忽略的异常列表,包含子类型

配置文件

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

### 关于 Resilience4j Retry 机制 Resilience4j 是一种轻量级的容错库,专为 Java JVM 生态系统设计。它提供了多种功能模块来增强应用程序的弹性,其中包括 `Retry` 模块用于处理失败请求并自动重试。 以下是关于 Resilience4j 中 `Retry` 机制的核心概念及其配置使用的说明: #### 核心特性 - **可配置的最大尝试次数**:可以定义最大允许的重试次数。 - **固定延迟或指数退避策略**:支持设置固定的等待时间间隔或者采用指数增长的时间间隔来进行下一次重试- **异常过滤器**:可以选择仅针对某些特定类型的异常执行重试逻辑。 - **结果谓词**:可以根据返回的结果决定是否触发重试行为。 下面是一个简单的例子展示如何使用 Resilience4j 的 `Retry` 功能[^4]: ```java import io.github.resilience4j.retry.Retry; import io.github.resilience4j.retry.RetryConfig; import io.vavr.control.Try; public class Resilience4jExample { public static void main(String[] args) { // 配置重试参数 RetryConfig config = RetryConfig.custom() .maxAttempts(5) // 设置最多重试五次 .waitDuration(java.time.Duration.ofSeconds(2)) // 每次重试之间等待两秒 .retryExceptions(RuntimeException.class) // 对 RuntimeException 进行重试 .build(); // 创建 Retry 实例 Retry retry = Retry.of("backendName", config); // 使用装饰函数包裹需要重试的操作 Callable<String> callable = () -> callExternalService(); Try<String> result = Try.ofCallable(CallableDecorator.decorateCallable(callable, retry)); System.out.println(result.isSuccess()); } private static String callExternalService() throws Exception { throw new RuntimeException("Simulated exception"); } } ``` 上述代码片段展示了如何通过自定义配置创建一个 `Retry` 对象,并将其应用于对外部服务调用的保护上。如果外部服务抛出了指定类型的异常,则会按照设定好的规则重新发起请求直到达到最大尝试次数为止[^4]。 对于更复杂的场景,比如结合其他模式(如 Circuit Breaker 或 Bulkhead),也可以轻松集成这些组件形成更加健壮的服务架构。 #### 注意事项 当部署应用到客户托管环境下的 Mule Runtime 时,可能还需要考虑负载均衡等因素影响整体系统的可用性性能表现[^1]。 同样,在微服务通信中选择合适的远程过程调用(RPC)方式也很重要;虽然现代技术如 gRPC 提供了更好的解决方案,但仍需注意其带来的紧耦合问题[^2]。 如果涉及到 HTTP 客户端开发,则可以利用像 Feign 这样的声明式 REST 客户端框架,并为其注册错误解码器以便更好地管理意外响应情况[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值