使用AOP注解处理接口幂等性,默认禁止同一用户在上次提交未果后10秒内又重复提交
在原先的sameUrlData的注解上进行了copy新建优化,使用redis去setnx的参数视项目使用点而调整,不一定是每个项目都适合这种取参形式。
源码如下:
package com.rs.srm.annotation;/** * 接口防短时间并发处理
*/
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/** * 一个用户 相同url 同时提交 相同数据 验证 **/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatData {/*** 设置请求锁定时间 单位秒*/int lockTime() default 10;
}
package com.rs.srm.annotation;import com.alibaba.fastjson.JSON;
import com.rs.common.exception.LockFailException;
import com.rs.common.meta.context.UserContext;
import com.rs.common.meta.service.lock.impl.RedisDistributedLockHandler;
import com.rs.common.vo.ResultDto;
import com.rs.system.shiro.util.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;@Aspect
@Component
@Slf4j
public class NoRepeatDataImpl {@AutowiredRedisDistributedLockHandler redisDistributedLockHandler;/*** 配置切入点*/@Pointcut("@annotation(com.rs.srm.annotation.NoRepeatData)")public void noRepeatDataCut() {}@Around("@annotation(noRepeatData)")public Object around(ProceedingJoinPoint point,NoRepeatData noRepeatData) throws Throwable {int lockExpire = noRepeatData.lockTime();//加锁过期时间//开始时间long start = System.currentTimeMillis();String args = JSON.toJSONString(point.getArgs());ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = requestAttributes.getRequest();String url = request.getRequestURL().toString();UserContext u = LoginUtil.getUserContext();MethodSignature signature = (MethodSignature) point.getSignature();//请求的方法名String className = point.getTarget().getClass().getName();String methodName = signature.getName();String paramSign = DigestUtils.md5Hex(url + u.getUserId() + args);log.warn("======request url=>{},param=>{},uid=>{},md5-=>{}",url,args,u.getUserId(),paramSign);try {log.info("【接口加锁参数:{}】",paramSign);redisDistributedLockHandler.lock(paramSign, 1, lockExpire);//过期时间由注解接收Object result = null;try {result = point.proceed();long end = System.currentTimeMillis();log.info("【接口正常执行完成】接口名:{}.{},执行时间:{}毫秒", className, methodName, (end - start));return result;}catch (Exception e){log.error("业务代码执行异常",e);return ResultDto.fail("系统异常");}finally {redisDistributedLockHandler.unlock(paramSign,1);}}catch (LockFailException e){long end = System.currentTimeMillis();log.info("【重复提交已拦截】接口名:{}.{},执行时间:{}毫秒", className, methodName, (end - start));return ResultDto.fail("请勿重复提交数据,请耐心等待!");}}
}