0
点赞
收藏
分享

微信扫一扫

利用spring AOP /Redis 实现各种情况下 防止接口重复提交问题

奋斗De奶爸 2021-09-19 阅读 61

上篇 并发情况下幂等性(&多次提交问题)中分析了幂等性出现的原因,以及解决方案,这篇就来实战一下,如何在分布式环境中利用redis 防止重复提交的问题。一起干饭!

  • 表单录入如何防止重复提交?

  • 微服务架构中,客户端重试如何防止重复提交?


1.自定义注解

package com.ptdot.portal.aop;

import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NoRepeatSubmit {

/**
* 默认1s钟以内算重复提交
* @return
*/

long timeout() default 1;
}

2.利用AOP+redis锁 实现对同一时间段重复提交问题的隔绝

package com.ptdot.portal.aop;

import cn.hutool.core.lang.Assert;
import com.ptdot.common.service.RedisService;
import com.ptdot.utils.IPUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
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;
import java.lang.reflect.Method;
import java.util.List;
import java.util.UUID;

@Aspect
@Component
public class NoRepeatSubmitAop {

@Autowired
private RedisService redisService;

/**
* 定义切入点
*/

@Pointcut("@annotation(NoRepeatSubmit)")
public void noRepeat() {}

/**
* 前置通知:在连接点之前执行的通知
* @param point
* @throws Throwable
*/

@Before("noRepeat()")
public void before(JoinPoint point) throws Exception{
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Assert.notNull(request, "request can not null");

// 此处可以用token或者JSessionId
String token = IPUtils.getIpAddr(request);
String path = request.getServletPath();
String key = getKey(token, path);
String clientId = getClientId();
List<Object> lGet = redisService.lGet(key, 0, -1);
// 获取注解
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
NoRepeatSubmit annotation = method.getAnnotation(NoRepeatSubmit.class);
long timeout = annotation.timeout();
boolean isSuccess = false;
if (lGet.size()==0 || lGet == null) {
isSuccess = redisService.lPushAll(key, timeout, clientId)>0;
}
if (!isSuccess) {
// 获取锁失败,认为是重复提交的请求
redisService.lPushAll(key, timeout, clientId);
throw new Exception("不可以重复提交");
}

}

private String getKey(String token, String path) {
return token + path;
}

private String getClientId() {
return UUID.randomUUID().toString();
}
}

3.IPutils 获取真实ip地址 作为key的一部分使用

package com.ptdot.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
* IP地址
*/

public class IPUtils {
private static Logger logger = LoggerFactory.getLogger(IPUtils.class);

/**
* 获取IP地址
* <p>
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
*/

public static String
getIpAddr(HttpServletRequest request) {
String ip = null;
try {
ip = request.getHeader("x-forwarded-for");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
} catch (Exception e) {
logger.error("IPUtils ERROR ", e);
}

// //使用代理,则获取第一个IP地址
if(StringUtils.isEmpty(ip) && ip.length() > 15) {
if(ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}

return ip;
}

}

4. 使用到的redisTemplate 方法

    @Override
public Long lPushAll(String key, Long time, Object... values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
expire(key, time);
return count;
}

这样,防止接口重复提交的问题就解决啦,真实可用,欢迎小伙伴们留言讨论


不要以为每天把功能完成了就行了,这种思想是要不得的,互勉~!

举报

相关推荐

0 条评论