背景介绍
用户开发票功能,数据链路较长,并且是异步返回开票最终结果的;为了防止用户重复提交开票申请,需要做防止重复提交的业务逻辑处理。
我们的服务是分布式微服务架构,开票服务有多个节点。当时已有的实现是通过redis key 有效期来实现的。为了将做成一个公共的工具,我试图把重复提交判断的逻辑与业务代码解耦,希望达到的效果是:在需要验证重复提交的接口上添加自定义注解@NoRepeatSubmit(expire = 60, handkey = NoRepeatSubmitHandleFactory实现类.class),
注解使用者需要通过实现NoRepeatSubmitHandleFactory接口,并重写handleKey方法去处理自己特殊的重复提交判断。
关键类代码
1、注解 NoRepeatSubmit
/**
* @Description 防止重复提交请求注解
* @Date 2022/8/30 14:50
* @Created by liuxianglin
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
//key的过期时间 单位s
int expire();
//处理key的方法
Class<? extends NoRepeatSubmitHandleFactory> handleKey();
}
2、切面拦截器 NoRepeatSubmitAspect
/**
* @Description 切面拦截器处理NoRepeatSubmit注解
* @Date 2022/8/30 15:07
* @Created by liuxianglin
*/
@Aspect
@Component
public class NoRepeatSubmitAspect {
private static final Logger logger = LoggerFactory.getLogger(NoRepeatSubmitAspect.class);
@Pointcut("@annotation(com.xxxx.config.repeatsubmit.NoRepeatSubmit)")
public void point() {}
@Around("point()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
//获取NoRepeatSubmit注解的key处理类
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
NoRepeatSubmit annotation = method.getAnnotation(NoRepeatSubmit.class);
Class<? extends NoRepeatSubmitHandleFactory> aClass1 = annotation.handleKey();
NoRepeatSubmitHandleFactory handleFactory = SpringUtil.getBean(aClass1);
//调用处理key的方法
List<String> keys;
try {
keys = handleFactory.handleKey(pjp);
} catch (Exception e) {
return Results.failure(null, e.getMessage());
}
//执行请求
Results result = (Results) pjp.proceed();
return result;
}
}
3、处理重复提交具体业务逻辑的工厂类 NoRepeatSubmitHandleFactory
/**
* @Description
* @Date 2022/8/31 16:53
* @Created by liuxianglin
*/
public interface NoRepeatSubmitHandleFactory {
List<String> handleKey(ProceedingJoinPoint pjp) throws Exception;
}
4、我的重复提交具体业务逻辑的实现类 MyNoRepeatSubmitHandleFactory
import org.springframework.data.redis.core.RedisTemplate;
/**
* @Description
* @Date 2022/8/31 16:55
* @Created by liuxianglin
*/
@Component
public class MyNoRepeatSubmitHandle implements NoRepeatSubmitHandleFactory {
@Autowired
private RedisTemplate redisRepository;
@Override
public List<String> handleKey(ProceedingJoinPoint pjp) throws Exception {
//获取request
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
//获取response
HttpServletResponse response = requestAttributes.getResponse();
//获取请求的body参数
Object[] args = pjp.getArgs();
MyCommand cmd = (MyCommand) args[0];
String key = cmd.toString();
//获取注解NoRepeatSubmit注解
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
NoRepeatSubmit annotation = method.getAnnotation(NoRepeatSubmit.class);
Object value = redisRepository.get(key);
if (value != null) {
throw new Exception("请勿重复提交");
}
redisRepository.opsForValue().set(key, key, annotation.expire(), TimeUnit.SECONDS);
}
}
5、使用注解 NoRepeatSubmit的示例
@NoRepeatSubmit(expire = 60, handleKey = MyNoRepeatSubmitHandleFactory.class)
@GetMapping("/testNoRepeatSubmit")
public Results testNoRepeatSubmit(MyCommand cmd) {
return null;
}