feat: 2.3.0

新增注解在接口的情况
This commit is contained in:
byteblogs168 2023-09-07 09:33:23 +08:00
parent f5c52e225c
commit a5e745ad28
13 changed files with 335 additions and 244 deletions

View File

@ -38,7 +38,7 @@
<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.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,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 = "指定基础参数",

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

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

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,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("此处发生了参数异常");
}
}

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(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,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("此处发生了参数异常");
}
}

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