码迷,mamicode.com
首页 > 其他好文 > 详细

使用 注解+AOP+redis 实现幂等接口

时间:2021-07-26 16:58:58      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ide   tde   asp   around   dem   getc   one   业务   ssi   

由于业务需要,会根据入参DTO中的某几个属性实现幂等接口,特此记录.
1.增加注解
ApiIdempotent

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
public @interface ApiIdempotent {
    //幂等
    /*
     * 单位 秒
     */
    long time() default 1;
    /*
     * 是否抛出异常
     */
    boolean throwEx() default true;
}

增加注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Documented
public @interface ApiIdempotentField {

}

AOP部分逻辑

@Aspect
@Component
@Slf4j
public class ApiIdempotentInterceptor {

    private RedisService redisService;

    @Pointcut("@annotation(com.sky.common.base.annotation.ApiIdempotent)")
    public void apiIdempotentPoint() {}

    @Around(value = "apiIdempotentPoint()  &&  @annotation(apiIdempotent)")
    public Object methodAround(ProceedingJoinPoint jp,ApiIdempotent apiIdempotent) throws Throwable,IllegalAccessException {

        Object[] pointArgs  = jp.getArgs();
        List<Object> logArgs = streamOf(pointArgs)
                .filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse) && !(arg instanceof MultipartFile)))
                .collect(Collectors.toList());

        String parameter = logArgs.stream().map(o -> {
            Class<?> declaringClass = o.getClass();
            Field[] declaredFields = declaringClass.getDeclaredFields();
            String reduce = Arrays.stream(declaredFields).filter(field -> field.isAnnotationPresent(ApiIdempotentField.class)).map(field -> {
                String value = "";
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                try {
                    value =  String.valueOf(field.get(o));
                } catch (Exception e) {
                    log.error("ApiIdempotentInterceptor.methodAround", e);
                }
                return field.getName() + " : " + value;
            }).collect(Collectors.joining(","));
            if (StringUtils.isBlank(reduce)) {
                return JSON.toJSONString(o);
            }
            return reduce;
        }).collect(Collectors.joining(","));

        String md5 = Md5Utils.getMD5(parameter);

        if (!this.redisService.setIfAbsent(md5, 1, apiIdempotent.time())) {
            if (apiIdempotent.throwEx()) {
                throw new SkyException("重复操作");
            }
            return ResultMessage.success();
        } else {
          return jp.proceed();
        }
    }

    public ApiIdempotentInterceptor(RedisService redisService) {
        this.redisService = redisService;
    }

    private static <T> Stream<T> streamOf(T[] array) {
        return ArrayUtils.isEmpty(array) ? Stream.empty() : Arrays.stream(array);
    }
}

使用 注解+AOP+redis 实现幂等接口

标签:ide   tde   asp   around   dem   getc   one   业务   ssi   

原文地址:https://www.cnblogs.com/exceptionalChild/p/15061030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!