Java 利用锁避免重复订票_SpringBoot--防止重复提交(锁机制---本地锁、分布式锁)...

防止重复提交,主要是使用锁的形式来处理,如果是单机部署,可以使用本地缓存锁(Guava)即可,如果是分布式部署,则需要使用分布式锁(可以使用zk分布式锁或者redis分布式锁),本文的分布式锁以redis分布式锁为例。

一、本地锁(Guava)

1、导入依赖

org.springframework.boot

spring-boot-starter-aop

com.google.guava

guava

21.0

2、自定义本地锁注解

packagecom.example.demo.utils;import java.lang.annotation.*;

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inheritedpublic @interfaceLocalLock {

String key()default "";//过期时间,使用本地缓存可以忽略,如果使用redis做缓存就需要

int expire() default 5;

}

3、本地锁注解实现

packagecom.example.demo.utils;importcom.google.common.cache.Cache;importcom.google.common.cache.CacheBuilder;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.Signature;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.context.annotation.Configuration;importorg.springframework.util.StringUtils;importjava.lang.reflect.Method;importjava.util.concurrent.TimeUnit;

@Aspect

@Configurationpublic classLockMethodInterceptor {//定义缓存,设置最大缓存数及过期日期

private static final Cache CACHE = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(20, TimeUnit.SECONDS).build();

@Around("execution(public * *(..)) && @annotation(com.example.demo.utils.LocalLock)")publicObject interceptor(ProceedingJoinPoint joinPoint){

MethodSignature signature=(MethodSignature) joinPoint.getSignature();

Method method=signature.getMethod();

LocalLock localLock= method.getAnnotation(LocalLock.class);

String key=getKey(localLock.key(),joinPoint.getArgs());if(!StringUtils.isEmpty(key)){if(CACHE.getIfPresent(key) != null){throw new RuntimeException("请勿重复请求!");

}

CACHE.put(key,key);

}try{returnjoinPoint.proceed();

}catch(Throwable throwable){throw new RuntimeException("服务器异常");

}finally{

}

}privateString getKey(String keyExpress, Object[] args){for (int i = 0; i < args.length; i++) {

keyExpress= keyExpress.replace("arg[" + i + "]", args[i].

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值