Merge branch '2.x'

This commit is contained in:
byteblogs168 2023-09-16 10:27:27 +08:00
commit eca8414ddc
21 changed files with 403 additions and 273 deletions

View File

@ -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`)
);
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`)
);

12
pom.xml
View File

@ -38,7 +38,17 @@
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>easy-retry-client-starter</artifactId>
<version>2.2.0</version>
<version>2.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>

View File

@ -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);
}
}

View File

@ -1,13 +1,12 @@
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.LocalRetryService;
import com.example.easy.retry.service.impl.LocalRetryServiceImpl;
@RestController
@RequestMapping("/local")
@ -27,6 +26,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 = "指定基础参数",
@ -83,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);
}
}

View File

@ -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);
}
}

View File

@ -8,15 +8,10 @@ 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.RemoteRetryService;
import com.example.easy.retry.service.impl.RemoteRetryServiceImpl;
@RestController
@RequestMapping("/remote")
@ -24,7 +19,7 @@ import com.example.easy.retry.service.RemoteRetryService;
public class RemoteRetryController {
@Autowired
private RemoteRetryService remoteRetryService;
private RemoteRetryServiceImpl remoteRetryServiceImpl;
/**
* 一个最简单的远程调用案例
@ -37,7 +32,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 +46,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 +62,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 +81,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 +99,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 +122,7 @@ public class RemoteRetryController {
)
@PostMapping("/retryWithRetryMethod")
public void remoteRetryWithRetryMethod(@RequestBody OrderVo orderVo) {
remoteRetryService.remoteRetryWithRetryMethod(orderVo);
remoteRetryServiceImpl.remoteRetryWithRetryMethod(orderVo);
}
/**
@ -142,9 +137,10 @@ public class RemoteRetryController {
+
"📢查看任务列表: http://preview.easyretry.com/#/retry-task/list"
)
@PostMapping("/retryWithCallback")
public void remoteRetryWithCallback(@RequestBody OrderVo orderVo) {
remoteRetryService.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);
}
/**
@ -158,7 +154,7 @@ public class RemoteRetryController {
)
@PostMapping("/remoteRetryWithBizNo")
public void remoteRetryWithBizNo(@RequestBody OrderVo orderVo) {
remoteRetryService.remoteRetryWithBizNo(orderVo);
remoteRetryServiceImpl.remoteRetryWithBizNo(orderVo);
}

View File

@ -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<FailOrderPo>()
.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()

View File

@ -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 {
}

View File

@ -1,85 +1,28 @@
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;
import com.example.easy.retry.vo.OrderVo;
/**
* 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);
if ("ParamException".equals(type)) {
throw new ParamException("此处发生了指定异常Param Exception");
} else if ("ArithmeticException".equals(type)) {
throw new ParamException("此处发生了指定异常Arithme Exception");
} else {
throw new UnsupportedOperationException("未知异常");
}
}
void localRetryExcludeException(String type);
void localRetryIsThrowException(String params);
boolean localRetryWithRetryMethod(OrderVo orderVo);
@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("此处发生了参数异常");
}
}

View File

@ -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 {
}

View File

@ -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(String scene, OrderVo orderVo);
boolean remoteRetryWithBizNo(OrderVo orderVo);
}

View File

@ -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;
}
}

View File

@ -0,0 +1,105 @@
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;
import com.example.easy.retry.exception.ParamException;
/**
* easy-retry中的本地重试demo
*/
@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("此处发生了参数异常");
}
@Override
@Retryable(scene = "localRetryWithRetryMethod", retryStrategy = RetryType.ONLY_LOCAL,
retryMethod = OrderRetryMethod.class)
public boolean localRetryWithRetryMethod(OrderVo orderVo) {
throw new NullPointerException();
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,117 @@
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;
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();
}
/**
* 自定义BizNo
*/
@Retryable(scene = "remoteRetryWithBizNo", retryStrategy = RetryType.ONLY_REMOTE, bizNo = "#orderVo.orderId")
public boolean remoteRetryWithBizNo(OrderVo orderVo) {
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;
}
}

View File

@ -0,0 +1,2 @@
com.aizuda.easy.retry.client.core.expression.QLExpressEngine
#com.aizuda.easy.retry.client.core.expression.AviatorExpressionEngine

View File

@ -1,2 +1 @@
#com.aizuda.easy.retry.client.core.serializer.HessianSerializer
#com.aizuda.easy.retry.client.core.serializer.JacksonSerializer

View File

@ -0,0 +1 @@
#com.example.easy.retry.spi.TTLRetrySiteSnapshotContext

View File

@ -0,0 +1 @@
#com.example.easy.retry.spi.MyEasyRetryListener

View File

@ -1 +0,0 @@
#com.maluxinyu.easyretry.spi.TTLRetrySiteSnapshotContext

View File

@ -1 +0,0 @@
#com.maluxinyu.easyretry.spi.MyEasyRetryListener