首页 > 其他分享 >重复消费解决方案

重复消费解决方案

时间:2023-03-28 23:23:29浏览次数:38  
标签:消费 重复 解决方案 request length ip import null ipAddress

1、Http重复请求问题

在系统中,Http 请求和远程调用是很常见的数据交互手段,例如订单服务调用商品服务扣减库存,如果因为订单服务的网络问题,导致调用过程重试,但是商品服务在短时间内接收到相同的请求,那么会做相同的数据库库存扣减,导致了数据不一致的问题,也是属于重复问题那么我们再接口设计上面,需要保证幂等性

解决办法:使用 redis 实现接口的幂等性

可以自定义一个幂等注解,然后配合AOP 进行方法拦截,对拦截的请求信息(包括 ip+方法名+参数名+参数值)根据固定的规则去生成一个 key,然后调用 redis 的 setnx 方法,如果返回 ok,则正常调用方法,否则就是重复调用了。这样可以保证重复请求接口在一定时间内只会被成功处理一次.至于锁的有效时长要根据业务情况而定的

代码实现

package com.ly.utils;

import org.springframework.http.HttpHeaders;
import org.springframework.http.server.ServerHttpRequest;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 获取IP地址
 *
 * @author ly (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023-03-21  22:20
 * @tags 喜欢就去努力的争取
 */
public class IpUtil {

    public static String getIpAddress(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        // ipAddress = this.getRequest().getRemoteAddr();

        return ipAddress;
    }

    public static String getGatewayIpAddress(ServerHttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        String ip = headers.getFirst("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddress().getAddress().getHostAddress();
        }
        return ip;
    }
}
package com.ly.revisit;

import java.lang.annotation.*;

/**
 * @author ly (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023-03-28  21:01
 * @tags 喜欢就去努力的争取
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Idempotence {
}

package com.ly.revisit;

import com.ly.utils.IpUtil;
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.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
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.time.Duration;
import java.util.StringJoiner;

/**
 * @author ly (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023-03-28  21:02
 * @tags 喜欢就去努力的争取
 */
@Slf4j
@Component
@Aspect
public class IdempotenceAspect {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 前置处理器
     *
     * @return
     */
    @Around(value = "@annotation(com.ly.revisit.Idempotence)")
    public Integer idempotence(ProceedingJoinPoint joinPoint) {
        Integer proceed = null;
        // 获取HttpServletRequest
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        // 获取IP
        String ipAddress = IpUtil.getIpAddress(request);
        // 拿到类名1
        Class<? extends ProceedingJoinPoint> aClass = joinPoint.getClass();
        log.info("aClass => {}", aClass);
        // 获取请求的方法名称
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        String methodName = method.getName();
        // 拿到类名2
        Class<?> declaringClass = method.getDeclaringClass();
        String typeName = declaringClass.getTypeName();

        // 获取请求参数
        Object[] args = joinPoint.getArgs();
        // 获取当前用户的ID 正常情况从TOKEN中获取
        Integer userId = 1;
        // 组装成唯一key
        StringJoiner joiner = new StringJoiner("-");
        String key = joiner.add(ipAddress)
                .add(typeName)
                .add(methodName)
                .add(userId.toString()).toString();

        Boolean flag = redisTemplate.opsForValue().setIfAbsent(key, "ly", Duration.ofSeconds(2));
        if (flag) {
            // 执行目标方法
            try {
                proceed = (Integer) joinPoint.proceed(args);
                log.info("目标方法执行成功");
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        } else {
            // 抛出异常
            log.info("重复请求");
        }
        return proceed;
    }

}

2、消息中间件重复消费问题

使用Redission的布隆过滤器实现

标签:消费,重复,解决方案,request,length,ip,import,null,ipAddress
From: https://www.cnblogs.com/qbbit/p/17267166.html

相关文章