
1.前言
1.1简介
1.2项目地址
https://github.com/burukeYou/fast-retry
1.3 分布式重试服务平台 Easy-Retry
https://mp.weixin.qq.com/s/Rr1oR4ieoYSoU_jLjxybow
https://blog.csdn.net/qq_34905631/article/details/131187901?spm=1001.2014.3001.5501
https://www.easyretry.com/pages/db78e2/
https://snailjob.opensnail.com/
2.原理
3.使用
3.1引入依赖
<dependency>
<groupId>io.github.burukeyougroupId>
<artifactId>fast-retry-allartifactId>
<version>0.2.0version>
dependency>
3.2编程式
3.2.1使用重试队列
ExecutorService executorService = Executors.newFixedThreadPool(8);RetryQueue queue = new FastRetryQueue(executorService);RetryTasktask = new RetryTask () { int result = 0 ;public long waitRetryTime() {return 2000;}public boolean retry() {return ++result < 5;}public String getResult() {return result + "";}};CompletableFuturefuture = queue.submit(task); log.info("任务结束 结果:{}",future.get());
3.2.2使用FastRetryBuilder
RetryResultPolicy<String> resultPolicy = result -> result.equals("444");FastRetryer<String> retryer = FastRetryBuilder.<String>builder().attemptMaxTimes(3).waitRetryTime(3, TimeUnit.SECONDS).retryIfException(true).retryIfExceptionOfType(TimeoutException.class).exceptionRecover(true).resultPolicy(resultPolicy).build();CompletableFuture<String> future = retryer.submit(() -> {log.info("重试");//throw new Exception("test");//int i = 1/0;if (0 < 10){throw new TimeoutException("test");}return "444";});String o = future.get();log.info("结果{}", o);
3.3注解式
依赖Spring环境,所以需要在配置上加上@EnableFastRetry注解启用配置才生效
如果将结果类型使用CompletableFuture包装,自动进行异步轮询返回,否则同步阻塞等待重试结果。
下面定义等价于 FastRetryer.execute方法
(retryWait = (delay = 2))public String retryTask(){return "success";}
下面定义等价于 FastRetryer.submit方法,支持异步轮询
(retryWait = (delay = 2))public CompletableFuture<String> retryTask(){return CompletableFuture.completedFuture("success");}
4.FastRetry、Spring-Retry和Guava-Retry的性能测试对比
下图是三者的性能对比
测试线程池: 8个固定线程
单个任务逻辑: 轮询5次,隔2秒重试一次,总耗时10秒
未测预计公式:当我们使用线程池的时候, 一般线程池中 总任务处理耗时 = 任务数/并发度 x 单个任务重试耗时