Fast-Retry 高性能百万级任务重试框架介绍及使用 一、Fast-Retry在本专栏的前面文章中我们介绍了Spring家族的 重试框架本篇文章再给大家介绍一个高性能百万级任务重试框架Fast-Retry。它是一个高性能任务重试框架可以支持百万级别任务的并发重试处理。与Spring-Retry不同Fast-Retry支持异步任务的重试、超时等待、回调 等功能。Fast-Retry使用起来也非常简单可以基于FastRetryBuilder创建一个重试执行器。 也可以和Spring框架结合使用官方提供了FastRetry注解可以非常方便的集成到项目中。并且Fast-Retry也可以针对不同的异常或结果进行重试。对比Spring-Retry和Guava-RetryFast-Retry有着绝对的性能优势GitHub项目地址https://github.com/burukeYou/fast-retry二、Fast-Retry 实践添加项目依赖dependencygroupIdio.github.burukeyou/groupIdartifactIdfast-retry-all/artifactIdversion0.2.0/version/dependency2.1 基于 FastRetryBuilder 的重试Slf4jSpringBootTestpublicclassRetryTest{privateFastRetryerStringretryerFastRetryBuilder.builder().attemptMaxTimes(3)// 重试测试.waitRetryTime(2,TimeUnit.SECONDS)// 重试等待时间.retryIfException(true)// 启用异常时重试.retryIfExceptionOfType(TimeoutException.class,RuntimeException.class)// 异常的类型.exceptionRecover(true).build();Testpublicvoidtest1()throwsExecutionException,InterruptedException{CompletableFutureStringfutureretryer.submit(()-{log.info(开始执行: {},LocalDateTime.now().toString());inti1/0;returnsuccess;});log.info(执行结果{},future.get());}}运行后可以看到重试日志2.2 基于 FastRetry 注解的方式基于注解的方式和Spring-Retry一样首先需要在配置类或启动类上增加EnableFastRetry注解EnableFastRetrypublicclassRetryDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(RetryDemoApplication.class,args);}}创建测试Service使用FastRetry注解Slf4jServicepublicclassTestService{FastRetry(maxAttempts3,retryWaitRetryWait(delay2))publicvoidtest(){log.info(开始执行: {},LocalDateTime.now().toString());inti1/0;log.info(执行结果succes);}}测试调用Slf4jSpringBootTestpublicclassRetryTest{ResourceTestServicetestService;Testpublicvoidtest2(){testService.test();}}同样可以看到重试日志