注:本功能不适用于分布式,后面会有文章介绍分布式
本次功能使用的是springboot,依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
</dependencies>
Lock注解
创建一个 LocalLock
注解,简单点就一个 key
可以了,由于暂时未用到 redis
所以 expire
是摆设…
package com.gysoft.springboothello.com.gysoft.annotation;
import java.lang.annotation.*;
/**
* Create by wws on 2019/5/28
* 锁的注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LocalLock {
/**
* @author fly
*/
String key() default "";
/**
* 过期时间 TODO 由于用的 guava 暂时就忽略这属性吧 集成 redis 需要用到
*
* @author fly
*/
int expire() default 5;
}
首先通过 CacheBuilder.newBuilder()
构建出缓存对象,设置好过期时间;
其目的就是为了防止因程序崩溃锁得不到释放(当然如果单机这种方式程序都炸了,锁早没了;但这不妨碍我们写好点)
在具体的 interceptor()
方法上采用的是 Around(环绕增强)
,所有带 LocalLock
注解的都将被切面处理;
Lock拦截器
package com.gysoft.springboothello.com.gysoft.interceptor;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.gysoft.springboothello.com.gysoft.annotation.LocalLock;
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.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
/**
* @Description
* @Author DJZ-WWS
* @Date 2019/5/28 16:17
*/
@Aspect
@Configuration
public class LockMethodInterceptor {
private static final Cache<String,Object> CACHES= CacheBuilder.newBuilder()
//最大缓存1000个
.maximumSize(1000)
//设置缓存5秒后过期
.expireAfterWrite(5, TimeUnit.SECONDS).build();
@Around("execution(* com.gysoft.springboothello.controlller..*(..))")
public Object interceptor(ProceedingJoinPoint pjp) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
LocalLock localLock = method.getAnnotation(LocalLock.class);
String key = getKey(localLock.key(), pjp.getArgs());
if (!StringUtils.isEmpty(key)) {
if (CACHES.getIfPresent(key) != null) {
throw new RuntimeException("请勿重复请求");
}
// 如果是第一次请求,就将 key 当前对象压入缓存中
CACHES.put(key, key);
}
try {
return pjp.proceed();
} catch (Throwable throwable) {
throw new RuntimeException("服务器异常");
} finally {
// TODO 为了演示效果,这里就不调用 CACHES.invalidate(key); 代码了
}
}
/**
* key 的生成策略,如果想灵活可以写成接口与实现类的方式(TODO 后续讲解)
*
* @param keyExpress 表达式
* @param args 参数
* @return 生成的key
*/
private String getKey(String keyExpress, Object[] args) {
for (int i = 0; i < args.length; i++) {
keyExpress = keyExpress.replace("arg[" + i + "]", args[i].toString());
}
return keyExpress;
}
}
控制层
@LocalLock(key = "user:arg[0]")
@GetMapping("query")
public String query(@RequestParam String token) {
return "success - " + token;
}
启动服务进行测试,当我们第一次访问的时候
扫描二维码关注公众号,回复:
6427125 查看本文章
![](/qrcode.jpg)
在短时间内再次访问:
从而实现了我们的表单防重复提交,这样的业务在做订单这样的业务的时候肯定会涉及。
转自:https://blog.battcn.com/2018/06/12/springboot/v2-cache-locallock/