在做项目的时候,点击注册到跳转有1s的间隙,可以多次点击注册按钮,且都能注册成功,所以我们要优化,前后端,都要防重复提交,前端方案大体就是点击后置灰或不可用或隐藏,后端可以在数据库层加锁,因为性能等一些原因效果不理想。所以我们的项目采用注解+AOP+分布式锁实现防重复提交,下面是伪代码:
package com.xxx.xxx.resolver;
import com.xxx.xxx.common.exception.SbcRuntimeException;
import com.xxx.xxx.common.util.CommonErrorCode;
import com.xxx.xxx.redis.RedisService;
import com.xxx.xxx.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 使用redis分布式锁(setnx)解决
* hash值 对象实例相同,默认的hash值相等(配合lombok的@Data对hashcode的重写),通过hash值作为锁值校验入参(不要随便重写request hash()方法)
* hash值超时时间(默认 5 s)
*/
@Aspect
@Component
@Slf4j