Resilience4j系列 - 使用Resilience4j-Retry实现优雅重试

Maven配置

        <!-- resilience4j -->
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-retry</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot2</artifactId>
            <version>1.7.0</version>
        </dependency>
        <!-- resilience spring boot integration with annotation requires spring aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

使用@Retry注解方式集成

配置文件

application.yaml

resilience4j:
  retry:
    configs:
      default:
        maxAttempts: 3     #总尝试次数
        waitDuration: 10s    #重试间隔时间
        enableExponentialBackoff: false    # 是否允许使用指数退避算法进行重试间隔时间的计算
        exponentialBackoffMultiplier: 2     # 指数退避算法的乘数
        enableRandomizedWait: false       # 是否允许使用随机的重试间隔
        randomizedWaitFactor: 0.5         # 随机因子
        retryExceptions:
          - im.marlinlm.exception.RetryException     #触发重试的异常类
        ignoreExceptions:
          - im.marlinlm.exception.RetryIgnoredException     #不会触发重试的异常类
    instances:
      myRetry:
        baseConfig: default
        waitDuration: 1 #1毫秒,最小等待时间为1毫秒

在方法中使用@Retry注解

@Component
public class SomeClass{
    //name参数的值必须与配置文件中的instances下的名称一致
    @Retry(name = "myRetry", fallbackMethod = "doSomethingFallback")
    public int doSomething(String someInput) {
        //...
        //写一些会抛异常的代码
        //...
    }

    //方法名必须与@Retry注解的fallbackMethod参数的值一致
    //返回类型必须与Retry注解的方法的返回类型一致
    //必须传入Throwable参数
    private int doSomethingFallback(Throwable t){
        //fallback逻辑,只有重试次数用完了才会运行此代码
    }

    //可以用更加明确的异常类型,Retry会优先使用匹配得更加准确的fallback方法。
    private int doSomethingFallback(RuntimeException e){
        //fallback逻辑
    }

    //可以传入注解了@Retry的方法的参数
    private int doSomethingFallback(String someinput, Throwable t){
        //fallback逻辑
    }
}

直接调用doSomething(String someInput)方法,就可以产生重试的效果。注意,由于resilience4j通过Aop实现,因此想要通过注解实现Retry机制的类,必须是一个spring的bean,如上例中注解了@Component。

使用编程方式集成

MyRetry.java

public class MyRetry{
    /**
    * 对Retry方法进行封装
    * 
    **/
    public <T> T doSomethingWithRetry(int attempts, RetryFunction<T> function, RetryRecoverFunction<T> recoverFunction){
        //建立Retry配置
        RetryConfig config = RetryConfig.custom()
            .maxAttempts(attempts) //尝试次数
            .waitDuration(Duration.ofMillis(100))//每次重试的等待时间(毫秒)
            .build();

        //创建Retry对象
        Retry retry = Retry.of("myRetry", config);

        //把要运行的方法封装为CheckedFunction
        CheckedFunction0<T> f = Retry.decorateCheckedSupplier(
                retry, () -> {
                    return function.invoke();
                });    

        //设置重试的Recover方法。这个方法只有在所有重试次数都用完之后才会被调用
        return Try.of(f).recover(throwable -> {
            if(recoverFunction != null){
                return recoverFunction.invoke(throwable);
            }
            return null;
        }).getOrElse(() -> null);
    }
}

使用上述这个封装

MyRetry myRetry;

//...
public void doSomething(){
//    ...
    myRetry.doSomethingWithRetry(3, () -> {
        //写一些会抛异常的代码
    });
//    ...
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Resilience4j-cache 是一个基于 Resilience4j 的缓存库,提供了对缓存的重试、熔断和限流等功能。它可以很方便地与 Spring Boot 集成使用。 以下是使用 Resilience4j-cache 的步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-cache</artifactId> <version>1.7.0</version> </dependency> ``` 2. 配置缓存 在 Spring Boot 的配置文件中添加以下配置: ``` resilience4j.cache.caffeine.instances.myCache.maximum-size=1000 resilience4j.cache.caffeine.instances.myCache.expire-after-write=5s ``` 该配置表示创建一个名为 `myCache` 的缓存,最大容量为 1000,写入后 5 秒过期。 3. 创建缓存 在代码中创建缓存实例: ``` Cache<String, String> cache = Cache.of("myCache", CacheConfig .custom() .expireAfterWrite(Duration.ofSeconds(5)) .maximumSize(1000) .build() ); ``` 4. 使用缓存 使用 `cache.get(key, loader)` 方法获取缓存项,如果缓存不存在,则会调用 `loader` 方法加载数据。 ``` String value = cache.get("key", () -> { // 从数据库或其他地方加载数据 return "value"; }); ``` 5. 配置重试、熔断和限流 可以使用 Resilience4j 提供的 `Retry`、`CircuitBreaker` 和 `RateLimiter` 等组件对缓存进行重试、熔断和限流等操作。 例如,使用 `Retry` 组件对缓存进行重试操作: ``` Cache<String, String> cache = Cache.of("myCache", CacheConfig .custom() .expireAfterWrite(Duration.ofSeconds(5)) .maximumSize(1000) .build() ); Retry retry = Retry.ofDefaults("retry"); Function<String, String> decorated = Retry.decorateFunction(retry, cache::get); String value = decorated.apply("key"); ``` 以上就是使用 Resilience4j-cache 的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值