Google Guava Retry 优雅的重试方案

在这里插入图片描述


前言

使用场景

当需要两个或者多个组件协同工作而又无法保证调用返回的顺序时,我们可能会得到一个意料之外结果,可是下一时刻重新调用方法这个结果可能就正确了。当在这个场景下我们希望第一次得到意外结果时再调用一次方法以得到期望的结果。

我们调用外部接口时候获取数据的时候,因为一些外在因素可能会出现网络抖动,连接超时等导致接口调用失败,这个时候我们应该具备一个重试的机制。

总的来说为了使业务程序更加健壮,后续的重试操作会帮助我们最终达到成功的结果。

什么场景不适合重试

当一个方法过程是会更新或者写数据的时候这个方法和包含这个方法的过程要在保证幂等性的基础上使用重试机制。

将重试机制应用于只读方法过程则很少会出现问题。


提示:以下是本篇文章正文内容,下面案例可供参考

了解幂等性

针对同一个方法用同样的参数进行一次请求和重复的多次请求对系统造成的影响和所产生的数据是一致的

在使用重试器的时候幂等性是我们必须要考虑的一个问题!

一、Guava Retry是什么?

Guava Retrying是Google Guava库的一个小扩展,可以为任意函数调用创建可配置的重试策略,例如,与正常运行时间不稳定的远程服务进行通信的对象。

guava retry 模块提供了一种通用方法,用于重试具有特定停止,重试和异常处理功能的任意Java代码,而Guava的谓词匹配增强了该功能。

与Spring retry比较

Spring retry 重试判定时针对异常类的,不能对返回数据做判断。
Spring retry 的可配置参数也比Guava Retry 少。

Guava Retry 相比较 Spring retry 功能更丰富,使用起来也更灵活,推荐使用。

二、使用步骤

1.引入库

代码如下(示例):

gradle

implementation group: 'com.github.rholder', name: 'guava-retrying', version: '2.0.0'

maven

<dependency>
    <groupId>com.github.rholder</groupId>
    <artifactId>guava-retrying</artifactId>
    <version>2.0.0</version>
</dependency>

2.码代码

import com.github.rholder.retry.*;
import com.google.common.base.Predicates;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
        (classes = TestClassSecond.class)
public class TestClassSecond {

    public static final Logger log = LoggerFactory.getLogger(TestClassSecond.class);

    /**
     * 测试过程
     */
    @Test
    public void testProc() {
//      定义重试器并配置
        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()   // 模板类型为 要重试函数的返回类型
                .retryIfExceptionOfType(Exception.class) // 重试条件 根据指定异常类型重试
                .retryIfResult(Predicates.equalTo(false)) // 重试条件 根据返回值判断重试     异常类型判断 和 返回值判断  可以2选1 或者 两个条件同时设定
                .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
//                .withWaitStrategy(WaitStrategies.exponentialWait(100, 5, TimeUnit.MINUTES))  // 重试频率 每次叠加5分钟
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))  // 最多重试3次
//                .withStopStrategy(StopStrategies.neverStop())    // 判断重试条件 一直重试下去
                .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(2, TimeUnit.SECONDS)) // 每次 重试条件为真 重试频率固定2秒后重试
                .build();

//        定义目标对象
        TestClassSecond testClassSecond = new TestClassSecond();

//        定义参数
//        String params = "https://blog.csdn.net/wangxudongx";
        String params = "https://www.baidu.com";

        try {
//            重试器调用
            retryer.call(()-> testClassSecond.func(params));
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 重试的目标函数
     * @param url
     * @return
     */
    private Boolean func(String url) {
        RestTemplate restTemplate = new RestTemplate();

        log.info("#INFO 事件调用 begin");

        String result = restTemplate.getForObject(url, String.class);
        log.info(""+result);
        log.info("#INFO 事件调用 end");

        if (result.length() > 10000) {
            return false;
        }

        return true;
    }

}

可以在判断重试条件里写Lambda表达式

        //      定义重试器并配置
        Retryer<BusinessDTO> retryer = RetryerBuilder.<BusinessDTO>newBuilder()   // 模板类型为 要重试函数的返回类型
//                .retryIfExceptionOfType(Exception.class) // 重试条件 根据指定异常类型重试
                .retryIfResult(e->{ return e.getCode() == 1 ? true : false;}) // 重试条件 根据返回值判断重试     异常类型判断 和 返回值判断  可以2选1 或者 两个条件同时设定 ,还可使用Lambda
                .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
//                .withWaitStrategy(WaitStrategies.exponentialWait(100, 5, TimeUnit.MINUTES))  // 重试频率 每次叠加5分钟
                .withStopStrategy(StopStrategies.stopAfterAttempt(2))  // 最多重试3次
//                .withStopStrategy(StopStrategies.neverStop())    // 判断重试条件 一直重试下去
                .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(750, TimeUnit.MILLISECONDS)) // 每次 重试条件为真 重试频率固定750毫秒秒后重试
                .build();

需要拿到最终运行的返回时

//            重试器调用
            BusinessDTO dto = retryer.call(()->this.isAboutMeOfDialog(dialogRecordId,baseUserInfo.getUserId()));

需要考虑的问题。

  1. 在什么条件下重试?
  2. 我们应该重试几次?
  3. 每次重试的间隔设置为多少合适?
  4. 如果所有重试机会都用完了还是不成功怎么办?

总结

在合适的场景下使用Guava retry可以帮忙我们快速实现重试机制。而善用重试机制可以方便解决一些业务场景问题。


导读

Google guava 事件总线 EventBus 进程内消息队列 观察者模式

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Guava RetryGuava库中提供的一种重试机制。它允许你在方法执行失败时自动进行重试,以增加方法的可靠性和稳定性。 使用Guava Retry,你可以定义重试策略,包括重试的最大次数、重试间隔等。当方法执行失败时,Retry会根据定义的策略自动进行重试,直到方法成功执行或达到最大重试次数。 下面是一个使用Guava Retry的示例代码: ```java import com.google.common.base.Predicates; import com.google.common.base.Throwables; import import com.google.common.util.concurrent.RateLimiter; import import com.google.common.util.concurrent.Retryer; import import com.google.common.util.concurrent.SimpleTimeLimiter; import import com.google.common.util.concurrent.TimeLimiter; import import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; public class GuavaRetryExample { public static void main(String[] args) { // 创建一个重试 Retryer<Boolean> retryer = Retryer.newBuilder() .retryIfResult(Predicates.isNull()) .retryIfException() .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build(); // 定义需要重试的方法 Callable<Boolean> callable = new Callable<Boolean>() { public Boolean call() throws Exception { // 这里可以放置需要重试的逻辑 // 如果方法执行失败,抛出异常 throw new Exception("Method execution failed"); } }; try { // 使用重试来执行方法 Boolean result = retryer.call(callable); System.out.println("Method execution result: " + result); } catch (ExecutionException e) { // 打印重试失败的异常信息 System.out.println("Retry failed: " + Throwables.getRootCause(e)); } catch (RetryException e) { // 打印重试异常信息 System.out.println("Retry failed: " + e.getMessage()); } } } ``` 在上述示例中,我们创建了一个重试,并定义了重试的条件和策略。然后,我们通过`retryer.call(callable)`来执行需要重试的方法。如果方法执行成功,将会返回执行结果;如果方法执行失败,将会抛出重试异常。 需要注意的是,Guava Retry并不会自动处理所有类型的异常。如果你希望在特定的异常情况下进行重试,可以使用`retryIfException`方法,并指定需要重试的异常类型。 这就是Guava Retry的简单介绍和示例代码,希望对你有帮助!如果还有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值