springboot结合redis实现防重复提交注解机制

springboot结合redis实现防重复提交注解

总所周知,有一些用户点击量较大的项目,经常会出现接口被频繁调用导致数据库崩溃,或者说因为网络卡顿的原因,造成脏数据的产生从而影响项目的正常运行。

为了解决该问题,也为了实现简单并且全局实现,今天就将我自己自定义防重复注解分享,希望大佬多多指教给予更合理的方式。

首先创建一个注解类,自定义我们需要用的注解名称(直接如下图所示)

在这里插入图片描述

其次,我们需要用到切面来实现该注解(已将代码示例写入下方)
package com.peas.center.config.interceptor;
import com.peas.center.basedata.redis.RedisClient;
import com.peas.center.common.constans.RedisContants;
import com.peas.center.common.enums.SubmitEnum;
import com.peas.center.common.result.CodeEnum;
import com.peas.center.config.annotation.RepeatAnnotation;
import com.peas.center.utils.JsonUtils;
import com.peas.center.utils.MD5Utils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.RequestWrapper;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @Description :防重复提交注解AOP
 */
@Aspect
@Component
@Slf4j
public class NoRepeatSubmitAspect extends HandlerInterceptorAdapter {
    @Autowired
    private RedisClient redisClient;

    /**
     * 切点
     */
    @Pointcut("@annotation(com.peas.center.config.annotation.RepeatAnnotation)")
    public void pointcut() {}

    /**
     * 环绕通知
     *
     * @return
     */
    @Around("pointcut() && @annotation(repeatAnnotation)")
    public Object around(ProceedingJoinPoint joinPoint, RepeatAnnotation repeatAnnotation) {
        Object obj = null;
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
//                .getRequestAttributes();
        // 获取request请求
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        // 获取访问的url
        StringBuffer requestURL = request.getRequestURL();
        //获取请求参数
        StringBuilder params = new StringBuilder();
        for (Object arg : joinPoint.getArgs()) {
            params.append(arg);
        }
        String userid = request.getParameter("userid");
        //使用访问的url加上params作为key并加密
        String key = RedisContants.REPEAT_LOCK+MD5Utils.encode(userid+params.toString());
        log.info("redisKey:"+key);
        // 进行操作
        if(redisClient.get(key) == null) {
            try {
                obj = joinPoint.proceed();
                log.info("操作已提交成功!");
                // 将该操作记录到redis
                redisClient.set(key,0, repeatAnnotation.time());
            } catch (Throwable e) {
                e.printStackTrace();
            }
        } else {
            //如果该操作在该key未失效的情况下请求,则为重复提交请求抛出异常
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", CodeEnum.DATA_EXIST.getCode());
            map.put("success",false);
            map.put("message",CodeEnum.DATA_EXIST.getMessage());
            obj = map;
            log.error("请勿重复提交!");
        }
        return obj;
    }
}
实现完自己的注解以后,就可以将自己定义的注解使用在需要的接口上,如图所示

在这里插入图片描述

当然写完别忘记自测一下看是否满足自己防重复提交的要求
如图所示在规定时间内重复访问该接口便会提示重复提交

在这里插入图片描述
之所以会这样,是因为在这个切面里面使用环绕通知去获取到接口具有唯一性标识性的参数。将该参数和访问的url结合加密作为放进redis的一个key,并给该key设置一个过期时间,当每次访问这个接口时会去判断当前key是否还存在,如果还存在并且没有过期的话,那就是说明该接口在此时间内为重复提交操作,如果判断到该key不存在则会将该key写入redis之后再做之后的步骤。
以上是我自己通过查询资料写出来的适合自己的一种防重复提交注解,希望能带给有疑惑的朋友思路。若有不足之处请大佬多多指教。学习使我快乐

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值