Spring AOP 切面利用@Around注解实现幂等性

3 篇文章 0 订阅
1 篇文章 0 订阅

@Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务。

比如我们想在执行controller中方法前打印出请求参数,并在方法执行结束后来打印出响应值,这个时候,我们就可以借助于@Around注解来实现;

再比如我们想在执行方法时动态修改参数值等

类似功能的注解还有@Before等等,用到了Spring AOP切面思想,Spring AOP常用于拦截器、事务、日志、权限验证等方面。

完整演示代码如下:

需要说明的是,在以下例子中,我们即可以只用@Around注解,并设置条件,见方法run1();也可以用@Pointcut和@Around联合注解,见方法pointCut2()和run2(),这2种用法是等价的。如果我们还想利用其进行参数的修改,则调用时必须用joinPoint.proceed(Object[] args)方法,将修改后的参数进行回传。如果用joinPoint.proceed()方法,则修改后的参数并不会真正被使用。

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.persistence.EntityManager;
 
/**
 * 控制器切面
 *
 * @author lichuang
 */
 
@Component
@Aspect
public class ControllerAspect {
 
    private static final Logger logger = LoggerFactory.getLogger(ControllerAspect.class);
 
    @Autowired
    private EntityManager entityManager;
 
    /**
     * 调用controller包下的任意类的任意方法时均会调用此方法
     */
    @Around("execution(* com.company.controller.*.*(..))")
    public Object run1(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取方法参数值数组
        Object[] args = joinPoint.getArgs();
        //得到其方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取方法参数类型数组
        Class[] paramTypeArray = methodSignature.getParameterTypes();
        if (EntityManager.class.isAssignableFrom(paramTypeArray[paramTypeArray.length - 1])) {
            //如果方法的参数列表最后一个参数是entityManager类型,则给其赋值
            args[args.length - 1] = entityManager;
        }
        logger.info("请求参数为{}",args);
        //动态修改其参数
        //注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args)
        Object result = joinPoint.proceed(args);
        logger.info("响应结果为{}",result);
        //如果这里不返回result,则目标对象实际返回值会被置为null
        return result;
    }
 
    @Pointcut("execution(* com.company.controller.*.*(..))")
    public void pointCut2() {}
 
    @Around("pointCut2()")
    public Object run2(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取方法参数值数组
        Object[] args = joinPoint.getArgs();
        //得到其方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取方法参数类型数组
        Class[] paramTypeArray = methodSignature.getParameterTypes();
        if (EntityManager.class.isAssignableFrom(paramTypeArray[paramTypeArray.length - 1])) {
            //如果方法的参数列表最后一个参数是entityManager类型,则给其赋值
            args[args.length - 1] = entityManager;
        }
        logger.info("请求参数为{}",args);
        //动态修改其参数
        //注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args)
        Object result = joinPoint.proceed(args);
        logger.info("响应结果为{}",result);
        //如果这里不返回result,则目标对象实际返回值会被置为null
        return result;
    }
}

什么是幂等性

幂等性这个名词过于专业化,但用通俗的语言来讲就是同一时间因网络或其他因素导致用户对同一接口重复调用,并且接口请求(请求参数等)都是一样的,导致系统可能会出现数据的异常。举个常见的例子:在用户支付时,由于用户网络问题,因此第一次点击支付并付款时,发现网站无响应(实际上数据已经提交至服务器中),于是用户以为支付未成功又重新换起支付并进行付款。这样就会导致重复扣费。

有一些系统上在虽然幂等性要求不高,但有些对数据严格要求的系统则需要严格控制幂等性。

解决办法

实际上幂等性的解决办法网上随便一搜便有很多,常见的如:

1、业务表使用唯一索引:这种仅适用于单独的插入操作,如果在插入前需要更新数据后在插入时可能需要配合事务使用

2、悲观锁:由于需要只有一个线程获取锁,对并发要求高的接口不大适用

3、乐观锁:操作时检查下数据是否已经存在或更新,这种实际上还是会执行接口所有操作,如果接口在处理时间较长的情况下可能会导致无效的请求占用服务器资源

4、token机制:要求每次操作都需要对服务器申请token,数据提交时提交token,如果token有效才进行操作,同时删除token

5、分布式锁:通过第三方缓存(redis等)来实现分布式锁,控制同一时间仅有一个线程处理请求

实际上还有很多其他方法,而 在这里仅列出几个较为经典的方式

结合分析

(由于该工具类是我在开发博客时编写的,所以以下情况都是基于服务器带宽小【1M】、资源少的情况下考虑的)

在上面的办法中常用的有token机制、分布式锁及乐观锁,结合我自身服务器的情况,分析如下

token机制:需要预先获取token后才可以使用,如果服务器资源较少的情况下,token生成过程也是有一定可能会损耗带宽,而导致其他接口可能请求等待时间较长

分布式锁:没有足够资源让第三方缓存来支持

乐观锁:重复的操作可能会占用服务器处理请求的资源

解决方法

由于token在服务生成会占用资源,那么就将其交给前端进行生成,同时使用Java本地缓存保存前端生成的token,然后在接口处理完成时将token释放

代码实现

@Aspect
@Order(199)
public class ApiIdempotentAop {
    /**
     * 幂等性token缓存
     */
    private final ConcurrentHashMap<String, Object> tokenCache;
    /**
     * 是否是基于方法进行幂等性拦截
     */
    private final Boolean byMethod = false;
 
    private Object flag = new Object();
 
    public ApiIdempotentAop() {
        tokenCache = new ConcurrentHashMap<>();
    }
 
    /** 切入被PostMapping、DeleteMapping、PutMapping修饰的方法 */
    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.PutMapping)")
    public void api() {}
 
    @Around("api()")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获得request对象
        ServletRequestAttributes attributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
        HttpServletRequest request = attributes == null ? null : attributes.getRequest();
        if (request == null) { // 如果不是有效的api接口请求则不处理
            return joinPoint.proceed(joinPoint.getArgs());
        }
 
        // 如果存在HttpServletRequest对象,也就是有效的请求
        String idt = request.getHeader("Idempotent-Token");
        if (idt == null || idt.isEmpty()) {
            throw new ApiIdempotentException("Idempotent-Token不能为空");
        }
 
        // 如果基于方法则加上方法
        String token = (byMethod ? "" : (request.getMethod() + ":")) + idt;
        // 保存幂等性令牌,校验是否已存在token,如果存在则立即抛出异常终止执行
        if (tokenCache.putIfAbsent(token, this.flag) != null) {
            throw new ApiIdempotentException("服务器正在处理中,请稍后再试");
        } else {
            try {
                return joinPoint.proceed(joinPoint.getArgs());
            } finally {
                tokenCache.remove(token);
            }
        }
    }
}

其中异常类也仅是继承RuntimeException而已,并无特殊处理

public class ApiIdempotentException extends RuntimeException {
    public ApiIdempotentException() {
        super();
    }
 
    public ApiIdempotentException(String message) {
        super(message);
    }
 
    public ApiIdempotentException(String message, Throwable cause) {
        super(message, cause);
    }
 
    public ApiIdempotentException(Throwable cause) {
        super(cause);
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值