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, () -> {
        //写一些会抛异常的代码
    });
//    ...
}

猜你喜欢

转载自blog.csdn.net/marlinlm/article/details/122725235