From a5e745ad2835feec85ac836eb4ab92b9cf622a40 Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Thu, 7 Sep 2023 09:33:23 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=202.3.0=20=E6=96=B0=E5=A2=9E=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E5=9C=A8=E6=8E=A5=E5=8F=A3=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../LocalAndRemoteRetryController.java | 11 +- .../controller/LocalRetryController.java | 11 +- .../ManualRetryExecutorController.java | 6 +- .../controller/RemoteRetryController.java | 20 ++-- .../retry/service/LocalRemoteService.java | 33 +----- .../easy/retry/service/LocalRetryService.java | 85 +++----------- .../ManualRetryExecutorMethodService.java | 30 +---- .../retry/service/RemoteRetryService.java | 105 ++--------------- .../service/impl/LocalRemoteServiceImpl.java | 37 ++++++ .../service/impl/LocalRetryServiceImpl.java | 96 +++++++++++++++ .../ManualRetryExecutorMethodServiceImpl.java | 34 ++++++ .../service/impl/RemoteRetryServiceImpl.java | 109 ++++++++++++++++++ 13 files changed, 335 insertions(+), 244 deletions(-) create mode 100644 src/main/java/com/example/easy/retry/service/impl/LocalRemoteServiceImpl.java create mode 100644 src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java create mode 100644 src/main/java/com/example/easy/retry/service/impl/ManualRetryExecutorMethodServiceImpl.java create mode 100644 src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java diff --git a/pom.xml b/pom.xml index b854008..06749dc 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,7 @@ com.aizuda easy-retry-client-starter - 2.2.0 + 2.3.0-SNAPSHOT com.baomidou diff --git a/src/main/java/com/example/easy/retry/controller/LocalAndRemoteRetryController.java b/src/main/java/com/example/easy/retry/controller/LocalAndRemoteRetryController.java index 13e2bf9..cb19b65 100644 --- a/src/main/java/com/example/easy/retry/controller/LocalAndRemoteRetryController.java +++ b/src/main/java/com/example/easy/retry/controller/LocalAndRemoteRetryController.java @@ -1,7 +1,6 @@ package com.example.easy.retry.controller; -import com.example.easy.retry.service.LocalRemoteService; -import com.example.easy.retry.service.LocalRetryService; +import com.example.easy.retry.service.impl.LocalRemoteServiceImpl; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -11,20 +10,18 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.util.UUID; - @RestController @RequestMapping("/local-remote") @Api(value = "模拟先本地再远程重试案例", tags = "先本地再远程重试案例【RetryType.LOCAL_REMOTE】") public class LocalAndRemoteRetryController { @Autowired - private LocalRemoteService localRemoteService; + private LocalRemoteServiceImpl localRemoteServiceImpl; @GetMapping("/retry") @ApiOperation(value = "一个简单的入门案例") public void localRemote() { - localRemoteService.localRemote(); + localRemoteServiceImpl.localRemote(); } @GetMapping("/retryWithLocalRemote") @@ -37,6 +34,6 @@ public class LocalAndRemoteRetryController { ) public void remoteRetryWithLocalRemote(@ApiParam(name = "params", value = "测试参数", defaultValue = "test") @RequestParam("params") String params) { - localRemoteService.remoteRetryWithLocalRemote(params); + localRemoteServiceImpl.remoteRetryWithLocalRemote(params); } } diff --git a/src/main/java/com/example/easy/retry/controller/LocalRetryController.java b/src/main/java/com/example/easy/retry/controller/LocalRetryController.java index 2f0768d..116bba9 100644 --- a/src/main/java/com/example/easy/retry/controller/LocalRetryController.java +++ b/src/main/java/com/example/easy/retry/controller/LocalRetryController.java @@ -1,5 +1,6 @@ package com.example.easy.retry.controller; +import com.example.easy.retry.service.LocalRetryService; import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -7,7 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.example.easy.retry.service.LocalRetryService; +import com.example.easy.retry.service.impl.LocalRetryServiceImpl; @RestController @RequestMapping("/local") @@ -27,6 +28,14 @@ public class LocalRetryController { localRetryService.localRetry(params); } + @GetMapping("/localRetryWithAnnoOnInterface") + @ApiOperation( + value = "@Retryable在接口上执行重试" + ) + public void localRetryWithAnnoOnInterface(@ApiParam(name = "params", value = "测试参数", defaultValue = "test") @RequestParam("params") String params){ + localRetryService.localRetryWithAnnoOnInterface(params); + } + @GetMapping("/withBasicParams") @ApiOperation( value = "指定基础参数", diff --git a/src/main/java/com/example/easy/retry/controller/ManualRetryExecutorController.java b/src/main/java/com/example/easy/retry/controller/ManualRetryExecutorController.java index 73cd2d4..eb44a51 100644 --- a/src/main/java/com/example/easy/retry/controller/ManualRetryExecutorController.java +++ b/src/main/java/com/example/easy/retry/controller/ManualRetryExecutorController.java @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.example.easy.retry.service.ManualRetryExecutorMethodService; +import com.example.easy.retry.service.impl.ManualRetryExecutorMethodServiceImpl; @RestController @RequestMapping("/manual") @@ -17,7 +17,7 @@ import com.example.easy.retry.service.ManualRetryExecutorMethodService; public class ManualRetryExecutorController { @Autowired - private ManualRetryExecutorMethodService manualRetryExecutorMethodService; + private ManualRetryExecutorMethodServiceImpl manualRetryExecutorMethodServiceImpl; @ApiOperation( value = "手动重试", notes = "❤️如果不知道这个手动重试的使用场景可以参考: https://www.easyretry.com/pages/406a68/#%E5%8F%91%E9%80%81mq%E5%9C%BA%E6%99%AF \n" @@ -25,6 +25,6 @@ public class ManualRetryExecutorController { ) @GetMapping("/retry") public void remoteRetryWithCallback(@ApiParam(name = "params", value = "测试参数", defaultValue = "test") @RequestParam("params") String params){ - manualRetryExecutorMethodService.myExecutorMethod(params); + manualRetryExecutorMethodServiceImpl.myExecutorMethod(params); } } diff --git a/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java b/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java index cf3cb5e..838ed9b 100644 --- a/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java +++ b/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java @@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.easy.retry.vo.OrderVo; -import com.example.easy.retry.service.RemoteRetryService; +import com.example.easy.retry.service.impl.RemoteRetryServiceImpl; @RestController @RequestMapping("/remote") @@ -24,7 +24,7 @@ import com.example.easy.retry.service.RemoteRetryService; public class RemoteRetryController { @Autowired - private RemoteRetryService remoteRetryService; + private RemoteRetryServiceImpl remoteRetryServiceImpl; /** * 一个最简单的远程调用案例 @@ -37,7 +37,7 @@ public class RemoteRetryController { ) public void remote(@ApiParam(name = "params", value = "测试参数", defaultValue = "test") @RequestParam("params") String params) { - remoteRetryService.remoteRetry(params); + remoteRetryServiceImpl.remoteRetry(params); } /** @@ -51,7 +51,7 @@ public class RemoteRetryController { ) public void remoteSync(@ApiParam(name = "params", value = "测试参数", defaultValue = "test") @RequestParam("params") String params) { - remoteRetryService.remoteSync(params); + remoteRetryServiceImpl.remoteSync(params); } /** @@ -67,7 +67,7 @@ public class RemoteRetryController { "📢查看任务列表: http://preview.easyretry.com/#/retry-task/list" ) public void remoteRetryWithIdempotentId(@RequestBody OrderVo orderVo) { - remoteRetryService.remoteRetryWithIdempotentId(orderVo); + remoteRetryServiceImpl.remoteRetryWithIdempotentId(orderVo); } /** @@ -86,7 +86,7 @@ public class RemoteRetryController { public void retryWithSingleParamIdempotentGenerate( @ApiParam(name = "params", value = "测试参数", defaultValue = "test") @RequestParam("params") String params) { - remoteRetryService.retryWithSingleParamIdempotentGenerate(params); + remoteRetryServiceImpl.retryWithSingleParamIdempotentGenerate(params); } /** @@ -104,7 +104,7 @@ public class RemoteRetryController { ) public void retryWithMulParamIdempotentGenerate(@RequestBody OrderVo orderVo) { Random random = new Random(); - remoteRetryService.retryWithMulParamIdempotentGenerate( + remoteRetryServiceImpl.retryWithMulParamIdempotentGenerate( String.valueOf(UUID.randomUUID()), random.nextInt(), random.nextDouble(), @@ -127,7 +127,7 @@ public class RemoteRetryController { ) @PostMapping("/retryWithRetryMethod") public void remoteRetryWithRetryMethod(@RequestBody OrderVo orderVo) { - remoteRetryService.remoteRetryWithRetryMethod(orderVo); + remoteRetryServiceImpl.remoteRetryWithRetryMethod(orderVo); } /** @@ -144,7 +144,7 @@ public class RemoteRetryController { ) @PostMapping("/retryWithCallback") public void remoteRetryWithCallback(@RequestBody OrderVo orderVo) { - remoteRetryService.remoteRetryWithCompleteCallback(orderVo); + remoteRetryServiceImpl.remoteRetryWithCompleteCallback(orderVo); } /** @@ -158,7 +158,7 @@ public class RemoteRetryController { ) @PostMapping("/remoteRetryWithBizNo") public void remoteRetryWithBizNo(@RequestBody OrderVo orderVo) { - remoteRetryService.remoteRetryWithBizNo(orderVo); + remoteRetryServiceImpl.remoteRetryWithBizNo(orderVo); } diff --git a/src/main/java/com/example/easy/retry/service/LocalRemoteService.java b/src/main/java/com/example/easy/retry/service/LocalRemoteService.java index f4fe7fa..833d52b 100644 --- a/src/main/java/com/example/easy/retry/service/LocalRemoteService.java +++ b/src/main/java/com/example/easy/retry/service/LocalRemoteService.java @@ -1,36 +1,9 @@ package com.example.easy.retry.service; -import com.aizuda.easy.retry.client.core.annotation.Retryable; -import com.aizuda.easy.retry.client.core.retryer.RetryType; -import org.springframework.stereotype.Service; - -import java.util.concurrent.TimeUnit; - /** - * 模拟先本地再远程重试案例 - * - * @author www.byteblogs.com - * @date 2023-07-18 22:19:30 - * @since 2.1.0 + * @author: www.byteblogs.com + * @date : 2023-09-06 09:02 */ -@Service -public class LocalRemoteService { - - @Retryable(scene = "localRemote", retryStrategy = RetryType.LOCAL_REMOTE) - public void localRemote() { - System.out.println("local retry 方法开始执行"); - double i = 1 / 0; - } - - /** - * 使用先本地再远程的策略同步上传重试请求 retryStrategy = LOCAL_REMOTE 代表本地重试3次后再执行远程上报 async = false 代表使用同步上传的方式 timeout = 1 代表超时时间为1 - * unit = MINUTES 代表超时时间的单位是分钟 - */ - @Retryable(scene = "remoteRetryWithSync", retryStrategy = RetryType.LOCAL_REMOTE, - async = false, timeout = 1, unit = TimeUnit.MINUTES) - public String remoteRetryWithLocalRemote(String requestId) { - double i = 1 / 0; - return requestId; - } +public interface LocalRemoteService { } diff --git a/src/main/java/com/example/easy/retry/service/LocalRetryService.java b/src/main/java/com/example/easy/retry/service/LocalRetryService.java index 8a5bd61..62804da 100644 --- a/src/main/java/com/example/easy/retry/service/LocalRetryService.java +++ b/src/main/java/com/example/easy/retry/service/LocalRetryService.java @@ -1,85 +1,26 @@ package com.example.easy.retry.service; -import com.aizuda.easy.retry.client.core.retryer.RetryType; -import org.springframework.stereotype.Component; - import com.aizuda.easy.retry.client.core.annotation.Retryable; -import com.example.easy.retry.exception.ParamException; +import com.aizuda.easy.retry.client.core.retryer.RetryType; /** - * easy-retry中的本地重试demo - * 测试类入口见 {@link com.example.easy.retry.local.RetryableTest} + * @author: www.byteblogs.com + * @date : 2023-09-06 09:03 */ +public interface LocalRetryService { -@Component -public class LocalRetryService { - /** - * 入门案例 - * 我们仅仅需要指定场景值scene就可以给方法赋予重试逻辑 - * 其他的参数均可以省略或者使用默认值 - * [参数释义] - * 场景值scene对应我们后台的场景参数,用于后续的分组 - * 在微服务中建议大家直接使用集群的服务名称即可 - */ - @Retryable(scene = "localRetry", retryStrategy = RetryType.ONLY_LOCAL) - public void localRetry(String params) { - System.out.println("local retry 方法开始执行"); - double i = 1 / 0; - } + void localRetry(String params); - /** - * 指定基础参数 - * localTimes 本地重试次数 - * localInterval 本地重试间隔时间(默认单位为秒) - * unit 超时时间单位 - * 以下案例指的是本地重试4次,每次重试之间间隔10s - */ - @Retryable(scene = "localRetryWithBasicParams", localTimes = 4, localInterval = 10, retryStrategy = RetryType.ONLY_LOCAL) - public void localRetryWithBasicParams(String params) { - System.out.println("local retry with basic params 方法开始执行"); - double i = 1 / 0; - } + @Retryable(scene = "localRetryWithAnnoOnInterface", retryStrategy = RetryType.ONLY_LOCAL) + void localRetryWithAnnoOnInterface(String params); - /** - * 指定异常参数 - * include参数指的是当我们遭遇到指定异常时进行重试 - * 在这个案例中我们处理两个场景: - * 抛出指定异常,例如抛出自定义的ParamException异常,观察是否会重试 - * 抛出非指定异常,例如我们在这里产生一个异常,观察是否会重试 - * 注意:如果此时我们在include 中指定参数为BusinessException(ParamException的父类),同样也会进行重试逻辑 - * 更多用法:如果我们需要在include中指定多个参数,可以使用 include = {ParamException.class,NullPointerException.class} - */ - @Retryable(scene = "localRetryIncludeException", include = ParamException.class, retryStrategy = RetryType.ONLY_LOCAL) - public void localRetryIncludeException(String params) { - System.out.println("local retry include exception 方法开始执行"); - if ("NullPointerException".equals(params)) { - throw new NullPointerException(); - } else { - throw new ParamException("此处发生了指定异常Param Exception"); - } - } + void localRetryWithBasicParams(String params); - /** - * 这个参数的作用和include是相反的 - * exclude参数指的是当我们遇到指定异常时则不会进行重试 - * 比如在下述案例中我们指定了遇到ParamException和ArithmeticException后不进行重试 - */ - @Retryable(scene = "localRetryExcludeException", exclude = {ParamException.class, ArithmeticException.class}, retryStrategy = RetryType.ONLY_LOCAL) - public void localRetryExcludeException(String type) { - System.out.println("local retry exclude exception 方法开始执行"); + void localRetryIncludeException(String params); + + void localRetryExcludeException(String type); + + void localRetryIsThrowException(String params); - if ("ParamException".equals(type)) { - throw new ParamException("此处发生了指定异常Param Exception"); - } else if ("ArithmeticException".equals(type)) { - throw new ParamException("此处发生了指定异常Arithme Exception"); - } else { - throw new UnsupportedOperationException("未知异常"); - } - } - @Retryable(scene = "localRetryIsThrowException", isThrowException = false, retryStrategy = RetryType.ONLY_LOCAL) - public void localRetryIsThrowException(String params) { - System.out.println("local retry is throw exception 方法开始执行"); - throw new ParamException("此处发生了参数异常"); - } } diff --git a/src/main/java/com/example/easy/retry/service/ManualRetryExecutorMethodService.java b/src/main/java/com/example/easy/retry/service/ManualRetryExecutorMethodService.java index e669170..ae61ee3 100644 --- a/src/main/java/com/example/easy/retry/service/ManualRetryExecutorMethodService.java +++ b/src/main/java/com/example/easy/retry/service/ManualRetryExecutorMethodService.java @@ -1,33 +1,9 @@ package com.example.easy.retry.service; -import org.springframework.stereotype.Component; - -import com.aizuda.easy.retry.client.core.retryer.EasyRetryTemplate; -import com.aizuda.easy.retry.client.core.retryer.RetryTaskTemplateBuilder; -import com.example.easy.retry.executor.ManualRetryExecutorTask; -import com.example.easy.retry.vo.OrderVo; - /** - * easy-retry中的手动重试 + * @author: www.byteblogs.com + * @date : 2023-09-06 09:04 */ -@Component -public class ManualRetryExecutorMethodService { - - public void myExecutorMethod(String params) { - OrderVo orderVo = OrderVo.builder() - .orderId(params) - .source(1) - .build(); - EasyRetryTemplate easyRetryTemplate = RetryTaskTemplateBuilder.newBuilder() - // 手动指定场景名称 - .withScene(ManualRetryExecutorTask.SCENE) - // 指定要执行的任务 - .withExecutorMethod(ManualRetryExecutorTask.class) - // 指定参数 - .withParam(orderVo) - .build(); - // 执行模板 - easyRetryTemplate.executeRetry(); - } +public interface ManualRetryExecutorMethodService { } diff --git a/src/main/java/com/example/easy/retry/service/RemoteRetryService.java b/src/main/java/com/example/easy/retry/service/RemoteRetryService.java index e12af86..8f97aef 100644 --- a/src/main/java/com/example/easy/retry/service/RemoteRetryService.java +++ b/src/main/java/com/example/easy/retry/service/RemoteRetryService.java @@ -1,108 +1,27 @@ package com.example.easy.retry.service; -import java.util.Random; -import java.util.concurrent.TimeUnit; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import com.aizuda.easy.retry.client.core.annotation.Retryable; -import com.aizuda.easy.retry.client.core.retryer.RetryType; -import com.example.easy.retry.customized.MultiParamIdempotentGenerate; -import com.example.easy.retry.customized.OrderIdempotentIdGenerate; -import com.example.easy.retry.customized.OrderCompleteCallback; -import com.example.easy.retry.customized.OrderRetryMethod; -import com.example.easy.retry.customized.SingleParamIdempotentGenerate; -import com.example.easy.retry.dao.FailOrderBaseMapper; -import com.example.easy.retry.po.FailOrderPo; import com.example.easy.retry.vo.OrderVo; -import cn.hutool.db.sql.Order; -import cn.hutool.json.JSONUtil; - /** - * easy-retry中的远程重试 + * @author: www.byteblogs.com + * @date : 2023-09-06 09:04 */ -@Component -public class RemoteRetryService { +public interface RemoteRetryService { - /** - * 定义一个最基本的远程重试方法 - */ - @Retryable(scene = "remoteRetry", retryStrategy = RetryType.ONLY_REMOTE) - public void remoteRetry(String params) { - System.out.println("远程重试方法启动"); - double i = 1 / 0; - } + void remoteRetry(String params); - @Retryable(scene = "remoteRetrySync", retryStrategy = RetryType.ONLY_REMOTE, async = false, timeout = 500, unit = TimeUnit.MILLISECONDS) - public void remoteSync(String params) { - System.out.println("同步远程重试方法启动"); - double i = 1 / 0; - } + void remoteSync(String params); - /** - * 使用自定义的幂等Id生成类 OrderIdempotentIdGenerate - */ - @Retryable(scene = "remoteRetryWithIdempotentId", retryStrategy = RetryType.ONLY_REMOTE, - idempotentId = OrderIdempotentIdGenerate.class) - public boolean remoteRetryWithIdempotentId(OrderVo orderVo) { - double i = 1 / 0; - return true; - } + boolean remoteRetryWithIdempotentId(OrderVo orderVo); - /** - * 使用自定义的幂等Id生成类 SingleParamIdempotentGenerate 测试单参数场景下 - */ - @Retryable(scene = "retryWithSingleParamIdempotentGenerate", retryStrategy = RetryType.ONLY_REMOTE, - idempotentId = SingleParamIdempotentGenerate.class) - public boolean retryWithSingleParamIdempotentGenerate(String params) { - throw new NullPointerException(); - } + boolean retryWithSingleParamIdempotentGenerate(String params); - /** - * 使用自定义的幂等Id生成类 MultiParamIdempotentGenerate 测试多个参数场景下的解析 - */ - @Retryable(scene = "retryWithMulParamIdempotentGenerate", retryStrategy = RetryType.ONLY_REMOTE, - idempotentId = MultiParamIdempotentGenerate.class) - public boolean retryWithMulParamIdempotentGenerate(String uuid, Integer intVal, Double doubleVal, - Character character, OrderVo orderVo) { - throw new NullPointerException(); - } + boolean retryWithMulParamIdempotentGenerate(String uuid, Integer intVal, Double doubleVal, + Character character, OrderVo orderVo); - /** - * 使用自定义的异常处理类 OrderRetryMethod - */ - @Retryable(scene = "remoteRetryWithRetryMethod", retryStrategy = RetryType.ONLY_REMOTE, - retryMethod = OrderRetryMethod.class) - public boolean remoteRetryWithRetryMethod(OrderVo orderVo) { - throw new NullPointerException(); - } - - /** - * 使用自定义的retryCompleteCallback类 OrderCompleteCallback - */ - @Retryable(scene = "remoteRetryWithCompleteCallback", retryStrategy = RetryType.LOCAL_REMOTE, - retryCompleteCallback = OrderCompleteCallback.class) - public boolean remoteRetryWithCompleteCallback(OrderVo orderVo) { - Random random = new Random(); - // 生成一个随机数,范围为0到1之间 - double probability = random.nextDouble(); - // 判断随机数是否小于等于0.5,即50%的概率 - if (probability <= 0.5) { - // 生成的结果在50%的概率下执行这里的逻辑 - throw new NullPointerException(); - } - return true; - } - - /** - * 自定义BizNo - */ - @Retryable(scene = "remoteRetryWithBizNo", retryStrategy = RetryType.ONLY_REMOTE, bizNo = "#orderVo.orderId") - public boolean remoteRetryWithBizNo(OrderVo orderVo) { - throw new NullPointerException(); - } + boolean remoteRetryWithRetryMethod(OrderVo orderVo); + boolean remoteRetryWithCompleteCallback(OrderVo orderVo); + boolean remoteRetryWithBizNo(OrderVo orderVo); } diff --git a/src/main/java/com/example/easy/retry/service/impl/LocalRemoteServiceImpl.java b/src/main/java/com/example/easy/retry/service/impl/LocalRemoteServiceImpl.java new file mode 100644 index 0000000..c5b9dd4 --- /dev/null +++ b/src/main/java/com/example/easy/retry/service/impl/LocalRemoteServiceImpl.java @@ -0,0 +1,37 @@ +package com.example.easy.retry.service.impl; + +import com.aizuda.easy.retry.client.core.annotation.Retryable; +import com.aizuda.easy.retry.client.core.retryer.RetryType; +import com.example.easy.retry.service.LocalRemoteService; +import org.springframework.stereotype.Service; + +import java.util.concurrent.TimeUnit; + +/** + * 模拟先本地再远程重试案例 + * + * @author www.byteblogs.com + * @date 2023-07-18 22:19:30 + * @since 2.1.0 + */ +@Service +public class LocalRemoteServiceImpl implements LocalRemoteService { + + @Retryable(scene = "localRemote", retryStrategy = RetryType.LOCAL_REMOTE) + public void localRemote() { + System.out.println("local retry 方法开始执行"); + double i = 1 / 0; + } + + /** + * 使用先本地再远程的策略同步上传重试请求 retryStrategy = LOCAL_REMOTE 代表本地重试3次后再执行远程上报 async = false 代表使用同步上传的方式 timeout = 1 代表超时时间为1 + * unit = MINUTES 代表超时时间的单位是分钟 + */ + @Retryable(scene = "remoteRetryWithSync", retryStrategy = RetryType.LOCAL_REMOTE, + async = false, timeout = 1, unit = TimeUnit.MINUTES) + public String remoteRetryWithLocalRemote(String requestId) { + double i = 1 / 0; + return requestId; + } + +} diff --git a/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java b/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java new file mode 100644 index 0000000..0f66e66 --- /dev/null +++ b/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java @@ -0,0 +1,96 @@ +package com.example.easy.retry.service.impl; + +import com.aizuda.easy.retry.client.core.retryer.RetryType; +import com.example.easy.retry.service.LocalRetryService; +import org.springframework.stereotype.Component; + +import com.aizuda.easy.retry.client.core.annotation.Retryable; +import com.example.easy.retry.exception.ParamException; + +/** + * easy-retry中的本地重试demo + * 测试类入口见 {@link com.example.easy.retry.local.RetryableTest} + */ + +@Component +public class LocalRetryServiceImpl implements LocalRetryService { + /** + * 入门案例 + * 我们仅仅需要指定场景值scene就可以给方法赋予重试逻辑 + * 其他的参数均可以省略或者使用默认值 + * [参数释义] + * 场景值scene对应我们后台的场景参数,用于后续的分组 + * 在微服务中建议大家直接使用集群的服务名称即可 + */ + @Override + @Retryable(scene = "localRetry", retryStrategy = RetryType.ONLY_LOCAL) + public void localRetry(String params) { + System.out.println("local retry 方法开始执行"); + double i = 1 / 0; + } + + @Override + public void localRetryWithAnnoOnInterface(final String params) { + double i = 1 / 0; + } + + /** + * 指定基础参数 + * localTimes 本地重试次数 + * localInterval 本地重试间隔时间(默认单位为秒) + * unit 超时时间单位 + * 以下案例指的是本地重试4次,每次重试之间间隔10s + */ + @Override + @Retryable(scene = "localRetryWithBasicParams", localTimes = 4, localInterval = 10, retryStrategy = RetryType.ONLY_LOCAL) + public void localRetryWithBasicParams(String params) { + System.out.println("local retry with basic params 方法开始执行"); + double i = 1 / 0; + } + + /** + * 指定异常参数 + * include参数指的是当我们遭遇到指定异常时进行重试 + * 在这个案例中我们处理两个场景: + * 抛出指定异常,例如抛出自定义的ParamException异常,观察是否会重试 + * 抛出非指定异常,例如我们在这里产生一个异常,观察是否会重试 + * 注意:如果此时我们在include 中指定参数为BusinessException(ParamException的父类),同样也会进行重试逻辑 + * 更多用法:如果我们需要在include中指定多个参数,可以使用 include = {ParamException.class,NullPointerException.class} + */ + @Override + @Retryable(scene = "localRetryIncludeException", include = ParamException.class, retryStrategy = RetryType.ONLY_LOCAL) + public void localRetryIncludeException(String params) { + System.out.println("local retry include exception 方法开始执行"); + if ("NullPointerException".equals(params)) { + throw new NullPointerException(); + } else { + throw new ParamException("此处发生了指定异常Param Exception"); + } + } + + /** + * 这个参数的作用和include是相反的 + * exclude参数指的是当我们遇到指定异常时则不会进行重试 + * 比如在下述案例中我们指定了遇到ParamException和ArithmeticException后不进行重试 + */ + @Override + @Retryable(scene = "localRetryExcludeException", exclude = {ParamException.class, ArithmeticException.class}, retryStrategy = RetryType.ONLY_LOCAL) + public void localRetryExcludeException(String type) { + System.out.println("local retry exclude exception 方法开始执行"); + + if ("ParamException".equals(type)) { + throw new ParamException("此处发生了指定异常Param Exception"); + } else if ("ArithmeticException".equals(type)) { + throw new ParamException("此处发生了指定异常Arithme Exception"); + } else { + throw new UnsupportedOperationException("未知异常"); + } + } + + @Override + @Retryable(scene = "localRetryIsThrowException", isThrowException = false, retryStrategy = RetryType.ONLY_LOCAL) + public void localRetryIsThrowException(String params) { + System.out.println("local retry is throw exception 方法开始执行"); + throw new ParamException("此处发生了参数异常"); + } +} diff --git a/src/main/java/com/example/easy/retry/service/impl/ManualRetryExecutorMethodServiceImpl.java b/src/main/java/com/example/easy/retry/service/impl/ManualRetryExecutorMethodServiceImpl.java new file mode 100644 index 0000000..31707ec --- /dev/null +++ b/src/main/java/com/example/easy/retry/service/impl/ManualRetryExecutorMethodServiceImpl.java @@ -0,0 +1,34 @@ +package com.example.easy.retry.service.impl; + +import com.example.easy.retry.service.ManualRetryExecutorMethodService; +import org.springframework.stereotype.Component; + +import com.aizuda.easy.retry.client.core.retryer.EasyRetryTemplate; +import com.aizuda.easy.retry.client.core.retryer.RetryTaskTemplateBuilder; +import com.example.easy.retry.executor.ManualRetryExecutorTask; +import com.example.easy.retry.vo.OrderVo; + +/** + * easy-retry中的手动重试 + */ +@Component +public class ManualRetryExecutorMethodServiceImpl implements ManualRetryExecutorMethodService { + + public void myExecutorMethod(String params) { + OrderVo orderVo = OrderVo.builder() + .orderId(params) + .source(1) + .build(); + EasyRetryTemplate easyRetryTemplate = RetryTaskTemplateBuilder.newBuilder() + // 手动指定场景名称 + .withScene(ManualRetryExecutorTask.SCENE) + // 指定要执行的任务 + .withExecutorMethod(ManualRetryExecutorTask.class) + // 指定参数 + .withParam(orderVo) + .build(); + // 执行模板 + easyRetryTemplate.executeRetry(); + } + +} diff --git a/src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java b/src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java new file mode 100644 index 0000000..a19beb3 --- /dev/null +++ b/src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java @@ -0,0 +1,109 @@ +package com.example.easy.retry.service.impl; + +import java.util.Random; +import java.util.concurrent.TimeUnit; + +import com.example.easy.retry.service.RemoteRetryService; +import org.springframework.stereotype.Component; + +import com.aizuda.easy.retry.client.core.annotation.Retryable; +import com.aizuda.easy.retry.client.core.retryer.RetryType; +import com.example.easy.retry.customized.MultiParamIdempotentGenerate; +import com.example.easy.retry.customized.OrderIdempotentIdGenerate; +import com.example.easy.retry.customized.OrderCompleteCallback; +import com.example.easy.retry.customized.OrderRetryMethod; +import com.example.easy.retry.customized.SingleParamIdempotentGenerate; +import com.example.easy.retry.vo.OrderVo; + +/** + * easy-retry中的远程重试 + */ +@Component +public class RemoteRetryServiceImpl implements RemoteRetryService { + + /** + * 定义一个最基本的远程重试方法 + */ + @Override + @Retryable(scene = "remoteRetry", retryStrategy = RetryType.ONLY_REMOTE) + public void remoteRetry(String params) { + System.out.println("远程重试方法启动"); + double i = 1 / 0; + } + + @Override + @Retryable(scene = "remoteRetrySync", retryStrategy = RetryType.ONLY_REMOTE, async = false, timeout = 500, unit = TimeUnit.MILLISECONDS) + public void remoteSync(String params) { + System.out.println("同步远程重试方法启动"); + double i = 1 / 0; + } + + /** + * 使用自定义的幂等Id生成类 OrderIdempotentIdGenerate + */ + @Override + @Retryable(scene = "remoteRetryWithIdempotentId", retryStrategy = RetryType.ONLY_REMOTE, + idempotentId = OrderIdempotentIdGenerate.class) + public boolean remoteRetryWithIdempotentId(OrderVo orderVo) { + double i = 1 / 0; + return true; + } + + /** + * 使用自定义的幂等Id生成类 SingleParamIdempotentGenerate 测试单参数场景下 + */ + @Override + @Retryable(scene = "retryWithSingleParamIdempotentGenerate", retryStrategy = RetryType.ONLY_REMOTE, + idempotentId = SingleParamIdempotentGenerate.class) + public boolean retryWithSingleParamIdempotentGenerate(String params) { + throw new NullPointerException(); + } + + /** + * 使用自定义的幂等Id生成类 MultiParamIdempotentGenerate 测试多个参数场景下的解析 + */ + @Override + @Retryable(scene = "retryWithMulParamIdempotentGenerate", retryStrategy = RetryType.ONLY_REMOTE, + idempotentId = MultiParamIdempotentGenerate.class) + public boolean retryWithMulParamIdempotentGenerate(String uuid, Integer intVal, Double doubleVal, + Character character, OrderVo orderVo) { + throw new NullPointerException(); + } + + /** + * 使用自定义的异常处理类 OrderRetryMethod + */ + @Override + @Retryable(scene = "remoteRetryWithRetryMethod", retryStrategy = RetryType.ONLY_REMOTE, + retryMethod = OrderRetryMethod.class) + public boolean remoteRetryWithRetryMethod(OrderVo orderVo) { + throw new NullPointerException(); + } + + /** + * 使用自定义的retryCompleteCallback类 OrderCompleteCallback + */ + @Retryable(scene = "remoteRetryWithCompleteCallback", retryStrategy = RetryType.LOCAL_REMOTE, + retryCompleteCallback = OrderCompleteCallback.class) + public boolean remoteRetryWithCompleteCallback(OrderVo orderVo) { + Random random = new Random(); + // 生成一个随机数,范围为0到1之间 + double probability = random.nextDouble(); + // 判断随机数是否小于等于0.5,即50%的概率 + if (probability <= 0.5) { + // 生成的结果在50%的概率下执行这里的逻辑 + throw new NullPointerException(); + } + return true; + } + + /** + * 自定义BizNo + */ + @Retryable(scene = "remoteRetryWithBizNo", retryStrategy = RetryType.ONLY_REMOTE, bizNo = "#orderVo.orderId") + public boolean remoteRetryWithBizNo(OrderVo orderVo) { + throw new NullPointerException(); + } + + +} From 4a3644e7c77d5e6af5ab6a6112cfc6bae60d62cf Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Sun, 10 Sep 2023 10:04:49 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E8=87=AA=E5=AE=9A=E4=B9=89=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../retry/controller/LocalRetryController.java | 18 ++++++++++++++---- .../easy/retry/service/LocalRetryService.java | 2 ++ .../service/impl/LocalRetryServiceImpl.java | 11 ++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/easy/retry/controller/LocalRetryController.java b/src/main/java/com/example/easy/retry/controller/LocalRetryController.java index 116bba9..8f91140 100644 --- a/src/main/java/com/example/easy/retry/controller/LocalRetryController.java +++ b/src/main/java/com/example/easy/retry/controller/LocalRetryController.java @@ -1,12 +1,10 @@ package com.example.easy.retry.controller; import com.example.easy.retry.service.LocalRetryService; +import com.example.easy.retry.vo.OrderVo; import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.example.easy.retry.service.impl.LocalRetryServiceImpl; @@ -92,4 +90,16 @@ public class LocalRetryController { public void localRetryIsThrowException(@RequestParam("params") String params){ localRetryService.localRetryIsThrowException(params); } + + @PostMapping("/localRetryWithRetryMethod") + /** + * 使用自定义的异常处理类 OrderRetryMethod + */ + @ApiOperation( + value = "指定自定义的异常处理类", + notes ="🥇什么是自定义的异常处理类: https://www.easyretry.com/pages/540554/#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C%E5%99%A8" + ) + public boolean localRetryWithRetryMethod(@RequestBody OrderVo orderVo){ + return localRetryService.localRetryWithRetryMethod(orderVo); + } } diff --git a/src/main/java/com/example/easy/retry/service/LocalRetryService.java b/src/main/java/com/example/easy/retry/service/LocalRetryService.java index 62804da..d36c166 100644 --- a/src/main/java/com/example/easy/retry/service/LocalRetryService.java +++ b/src/main/java/com/example/easy/retry/service/LocalRetryService.java @@ -2,6 +2,7 @@ package com.example.easy.retry.service; import com.aizuda.easy.retry.client.core.annotation.Retryable; import com.aizuda.easy.retry.client.core.retryer.RetryType; +import com.example.easy.retry.vo.OrderVo; /** * @author: www.byteblogs.com @@ -22,5 +23,6 @@ public interface LocalRetryService { void localRetryIsThrowException(String params); + boolean localRetryWithRetryMethod(OrderVo orderVo); } diff --git a/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java b/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java index 0f66e66..87b88e8 100644 --- a/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java +++ b/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java @@ -1,7 +1,9 @@ package com.example.easy.retry.service.impl; import com.aizuda.easy.retry.client.core.retryer.RetryType; +import com.example.easy.retry.customized.OrderRetryMethod; import com.example.easy.retry.service.LocalRetryService; +import com.example.easy.retry.vo.OrderVo; import org.springframework.stereotype.Component; import com.aizuda.easy.retry.client.core.annotation.Retryable; @@ -9,7 +11,6 @@ import com.example.easy.retry.exception.ParamException; /** * easy-retry中的本地重试demo - * 测试类入口见 {@link com.example.easy.retry.local.RetryableTest} */ @Component @@ -93,4 +94,12 @@ public class LocalRetryServiceImpl implements LocalRetryService { System.out.println("local retry is throw exception 方法开始执行"); throw new ParamException("此处发生了参数异常"); } + + @Override + @Retryable(scene = "localRetryWithRetryMethod", retryStrategy = RetryType.ONLY_LOCAL, + retryMethod = OrderRetryMethod.class) + public boolean localRetryWithRetryMethod(OrderVo orderVo) { + throw new NullPointerException(); + } + } From b7112e3dc54bc3537ff4e143746ca544e6f8ee7c Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Sun, 10 Sep 2023 10:57:19 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dspi=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com.aizuda.easy.retry.client.core.RetryArgSerializer | 1 - .../com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext | 1 + .../com.aizuda.easy.retry.client.core.event.EasyRetryListener | 1 + .../com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext | 1 - .../com.aizuda.easy.retry.client.core.event.EasyRetryListener | 1 - 5 files changed, 2 insertions(+), 3 deletions(-) rename src/main/resources/{ => META-INF}/services/com.aizuda.easy.retry.client.core.RetryArgSerializer (50%) create mode 100644 src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext create mode 100644 src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener delete mode 100644 src/main/resources/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext delete mode 100644 src/main/resources/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener diff --git a/src/main/resources/services/com.aizuda.easy.retry.client.core.RetryArgSerializer b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.RetryArgSerializer similarity index 50% rename from src/main/resources/services/com.aizuda.easy.retry.client.core.RetryArgSerializer rename to src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.RetryArgSerializer index 30c4ca4..44d9a74 100644 --- a/src/main/resources/services/com.aizuda.easy.retry.client.core.RetryArgSerializer +++ b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.RetryArgSerializer @@ -1,2 +1 @@ #com.aizuda.easy.retry.client.core.serializer.HessianSerializer -#com.aizuda.easy.retry.client.core.serializer.JacksonSerializer diff --git a/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext new file mode 100644 index 0000000..64d16af --- /dev/null +++ b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext @@ -0,0 +1 @@ +#com.example.easy.retry.spi.TTLRetrySiteSnapshotContext diff --git a/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener new file mode 100644 index 0000000..240eada --- /dev/null +++ b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener @@ -0,0 +1 @@ +#com.example.easy.retry.spi.MyEasyRetryListener diff --git a/src/main/resources/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext b/src/main/resources/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext deleted file mode 100644 index f8d0216..0000000 --- a/src/main/resources/services/com.aizuda.easy.retry.client.core.RetrySiteSnapshotContext +++ /dev/null @@ -1 +0,0 @@ -#com.maluxinyu.easyretry.spi.TTLRetrySiteSnapshotContext diff --git a/src/main/resources/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener b/src/main/resources/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener deleted file mode 100644 index 6837c1d..0000000 --- a/src/main/resources/services/com.aizuda.easy.retry.client.core.event.EasyRetryListener +++ /dev/null @@ -1 +0,0 @@ -#com.maluxinyu.easyretry.spi.MyEasyRetryListener From 203ec502d85bb7660975ad887ca64b1ea9304fe7 Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Sun, 10 Sep 2023 23:13:33 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=96=B0=E5=A2=9EbizNo=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=E7=9A=84Spi=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 ++++++++++ .../com.aizuda.easy.retry.client.core.ExpressionEngine | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine diff --git a/pom.xml b/pom.xml index 06749dc..561984c 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,16 @@ easy-retry-client-starter 2.3.0-SNAPSHOT + + com.googlecode.aviator + aviator + 5.3.3 + + + com.alibaba + QLExpress + 3.3.1 + com.baomidou mybatis-plus-boot-starter diff --git a/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine new file mode 100644 index 0000000..5740424 --- /dev/null +++ b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine @@ -0,0 +1,2 @@ +#com.aizuda.easy.retry.client.core.expression.QLExpressEngine +#com.aizuda.easy.retry.client.core.expression.AviatorExpressionEngine \ No newline at end of file From 258971f199f9d82559c46041e2eeaa99827cfc20 Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Wed, 13 Sep 2023 23:42:23 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E6=AC=A1=E6=95=B0=E5=9B=9E=E8=B0=83=20demo?= =?UTF-8?q?=E7=9A=84sql=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/demo.sql | 23 +++++----- .../controller/RemoteRetryController.java | 14 +++---- .../customized/OrderCompleteCallback.java | 8 ++-- .../retry/service/RemoteRetryService.java | 2 +- .../service/impl/RemoteRetryServiceImpl.java | 42 +++++++++++-------- 5 files changed, 48 insertions(+), 41 deletions(-) diff --git a/docs/demo.sql b/docs/demo.sql index 7aef6ef..5d678f0 100644 --- a/docs/demo.sql +++ b/docs/demo.sql @@ -2,14 +2,15 @@ DROP DATABASE IF EXISTS demo; CREATE DATABASE demo; USE demo; -CREATE TABLE `demo`.`fail_order` ( - `id` bigint NOT NULL COMMENT '自增主键Id', - `orderId` bigint NOT NULL COMMENT '订单Id', - `sourceId` int NOT NULL COMMENT '来源Id', - `sceneName` varchar(255) NOT NULL COMMENT '场景名称', - `executorName` varchar(255) NOT NULL COMMENT '执行器名称', - `args` varchar(255) NULL COMMENT '参数信息', - `create_dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', - `update_dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', - PRIMARY KEY (`id`) - ); \ No newline at end of file +CREATE TABLE fail_order +( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', + `order_id` varchar(255) NOT NULL COMMENT '订单Id', + `source_id` int NOT NULL COMMENT '来源Id', + `scene_name` varchar(255) NOT NULL COMMENT '场景名称', +`executor_name` varchar(255) NOT NULL COMMENT '执行器名称', +`args` varchar(255) NULL COMMENT '参数信息', +`create_dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', +`update_dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', +PRIMARY KEY (`id`) +); \ No newline at end of file diff --git a/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java b/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java index 838ed9b..d2ab539 100644 --- a/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java +++ b/src/main/java/com/example/easy/retry/controller/RemoteRetryController.java @@ -8,12 +8,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.example.easy.retry.vo.OrderVo; import com.example.easy.retry.service.impl.RemoteRetryServiceImpl; @@ -142,9 +137,10 @@ public class RemoteRetryController { + "📢查看任务列表: http://preview.easyretry.com/#/retry-task/list" ) - @PostMapping("/retryWithCallback") - public void remoteRetryWithCallback(@RequestBody OrderVo orderVo) { - remoteRetryServiceImpl.remoteRetryWithCompleteCallback(orderVo); + @PostMapping("/retryWithCallback/{scene}") + public void remoteRetryWithCallback(@ApiParam(name = "scene", value = "场景 FINISH/MAX_COUNT", defaultValue = "FINISH") + @PathVariable("scene") String scene, @RequestBody OrderVo orderVo) { + remoteRetryServiceImpl.remoteRetryWithCompleteCallback(scene, orderVo); } /** diff --git a/src/main/java/com/example/easy/retry/customized/OrderCompleteCallback.java b/src/main/java/com/example/easy/retry/customized/OrderCompleteCallback.java index cbf6367..36620b9 100644 --- a/src/main/java/com/example/easy/retry/customized/OrderCompleteCallback.java +++ b/src/main/java/com/example/easy/retry/customized/OrderCompleteCallback.java @@ -2,6 +2,8 @@ package com.example.easy.retry.customized; import cn.hutool.json.JSONUtil; import com.aizuda.easy.retry.client.core.callback.RetryCompleteCallback; +import com.aizuda.easy.retry.common.core.util.JsonUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.example.easy.retry.dao.FailOrderBaseMapper; import com.example.easy.retry.po.FailOrderPo; @@ -26,10 +28,10 @@ public class OrderCompleteCallback implements RetryCompleteCallback { @Override public void doSuccessCallback(String sceneName, String executorName, Object[] objects) { // 重试成功后删除失败表中的数据 - OrderVo orderVo = (OrderVo) objects[0]; + OrderVo orderVo = JsonUtil.parseObject(JsonUtil.toJsonString(objects[1]), OrderVo.class); log.info("远程重试成功,场景{},执行器{},参数信息",sceneName,executorName, JSONUtil.toJsonStr(objects)); failOrderBaseMapper.delete( - new LambdaQueryChainWrapper<>(failOrderBaseMapper) + new LambdaQueryWrapper() .eq(FailOrderPo::getOrderId,orderVo.getOrderId()) ); } @@ -42,7 +44,7 @@ public class OrderCompleteCallback implements RetryCompleteCallback { */ @Override public void doMaxRetryCallback(String sceneName, String executorName, Object[] objects) { - OrderVo orderVo = (OrderVo) objects[0]; + OrderVo orderVo = JsonUtil.parseObject(JsonUtil.toJsonString(objects[1]), OrderVo.class); log.info("远程重试达到最大限度,场景{},执行器{},参数信息",sceneName,executorName, JSONUtil.toJsonStr(objects)); // 重试失败后插入订单失败信息 failOrderBaseMapper.insert(FailOrderPo.builder() diff --git a/src/main/java/com/example/easy/retry/service/RemoteRetryService.java b/src/main/java/com/example/easy/retry/service/RemoteRetryService.java index 8f97aef..bb0ec63 100644 --- a/src/main/java/com/example/easy/retry/service/RemoteRetryService.java +++ b/src/main/java/com/example/easy/retry/service/RemoteRetryService.java @@ -21,7 +21,7 @@ public interface RemoteRetryService { boolean remoteRetryWithRetryMethod(OrderVo orderVo); - boolean remoteRetryWithCompleteCallback(OrderVo orderVo); + boolean remoteRetryWithCompleteCallback(String scene, OrderVo orderVo); boolean remoteRetryWithBizNo(OrderVo orderVo); } diff --git a/src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java b/src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java index a19beb3..7e50574 100644 --- a/src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java +++ b/src/main/java/com/example/easy/retry/service/impl/RemoteRetryServiceImpl.java @@ -3,6 +3,9 @@ package com.example.easy.retry.service.impl; import java.util.Random; import java.util.concurrent.TimeUnit; +import cn.hutool.core.lang.Assert; +import com.aizuda.easy.retry.client.core.intercepter.RetrySiteSnapshot; +import com.aizuda.easy.retry.common.core.enums.RetryStatusEnum; import com.example.easy.retry.service.RemoteRetryService; import org.springframework.stereotype.Component; @@ -80,23 +83,6 @@ public class RemoteRetryServiceImpl implements RemoteRetryService { throw new NullPointerException(); } - /** - * 使用自定义的retryCompleteCallback类 OrderCompleteCallback - */ - @Retryable(scene = "remoteRetryWithCompleteCallback", retryStrategy = RetryType.LOCAL_REMOTE, - retryCompleteCallback = OrderCompleteCallback.class) - public boolean remoteRetryWithCompleteCallback(OrderVo orderVo) { - Random random = new Random(); - // 生成一个随机数,范围为0到1之间 - double probability = random.nextDouble(); - // 判断随机数是否小于等于0.5,即50%的概率 - if (probability <= 0.5) { - // 生成的结果在50%的概率下执行这里的逻辑 - throw new NullPointerException(); - } - return true; - } - /** * 自定义BizNo */ @@ -105,5 +91,27 @@ public class RemoteRetryServiceImpl implements RemoteRetryService { throw new NullPointerException(); } + /** + * 使用自定义的retryCompleteCallback类 OrderCompleteCallback + */ + @Retryable(scene = "remoteRetryWithCompleteCallback", retryStrategy = RetryType.ONLY_REMOTE, + retryCompleteCallback = OrderCompleteCallback.class) + public boolean remoteRetryWithCompleteCallback(String scene, OrderVo orderVo) { + + Assert.notNull(RetrySiteSnapshot.getStage(), ()->new IllegalArgumentException("测试回调")); + + // 本地重试阶段,执行失败,远程的执行成功 + if (RetrySiteSnapshot.getStage().equals(RetrySiteSnapshot.EnumStage.LOCAL.getStage())) { + // 生成的结果在50%的概率下执行这里的逻辑 + throw new NullPointerException(); + } + + if (scene.equals(RetryStatusEnum.MAX_COUNT.name())) { + throw new NullPointerException(); + } + + return true; + } + } From 70337c1277a2cdccb3afa44a8c1b8753cb7a7fe3 Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Sat, 16 Sep 2023 10:27:08 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=96=B0=E5=A2=9EBizNo=20=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/com.aizuda.easy.retry.client.core.ExpressionEngine | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine index 5740424..b4e8fd4 100644 --- a/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine +++ b/src/main/resources/META-INF/services/com.aizuda.easy.retry.client.core.ExpressionEngine @@ -1,2 +1,2 @@ -#com.aizuda.easy.retry.client.core.expression.QLExpressEngine +com.aizuda.easy.retry.client.core.expression.QLExpressEngine #com.aizuda.easy.retry.client.core.expression.AviatorExpressionEngine \ No newline at end of file