不了解的同学戳这里 ==> Redis分布式锁的正确实现方式
使用Redis 是为了在负载均衡部署,如果是单机的部署的项目可以使用一个线程安全的本地Cache 替代 Redis
这里只贴出 AOP 类和测试类,完整代码:
https://github.com/TavenYin/taven-springboot-learning/tree/master/repeat-submit-intercept
@Aspect @Component public class RepeatSubmitAspect { private final static Logger LOGGER = LoggerFactory.getLogger(RepeatSubmitAspect.class); @Autowired private RedisLock redisLock; @Pointcut("@annotation(noRepeatSubmit)") public void pointCut(NoRepeatSubmit noRepeatSubmit) { } @Around("pointCut(noRepeatSubmit)") public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) throws Throwable { int lockSeconds = noRepeatSubmit.lockTime(); HttpServletRequest request = RequestUtils.getRequest(); Assert.notNull(request, "request can not null"); // 此处可以用token或者JSessionId String token = request.getHeader("Authorization"); String path = request.getServletPath(); String key = getKey(token, path); String clientId = getClientId(); boolean isSuccess = redisLock.tryLock(key, clientId, lockSeconds); if (isSuccess) { LOGGER.info("tryLock success, key = [{}], clientId = [{}]", key, clientId); // 获取锁成功, 执行进程 Object result; try { result = pjp.proceed(); } finally { // 解锁 redisLock.releaseLock(key, clientId); LOGGER.info("releaseLock success, key = [{}], clientId = [{}]", key, clientId); } return result; } else { // 获取锁失败,认为是重复提交的请求 LOGGER.info("tryLock fail, key = [{}]", key); return new ResultBean(ResultBean.FAIL, "重复请求,请稍后再试", null); } } private String getKey(String token, String path) { return token + path; } private String getClientId() { return UUID.randomUUID().toString(); } }
测试代码如下,模拟十个请求并发同时提交
@Component public class RunTest implements ApplicationRunner { private static final Logger LOGGER = LoggerFactory.getLogger(RunTest.class); @Autowired private RestTemplate restTemplate; @Override public void run(ApplicationArguments args) throws Exception { System.out.println("执行多线程测试"); String url="http://localhost:8000/submit"; CountDownLatch countDownLatch = new CountDownLatch(1); ExecutorService executorService = Executors.newFixedThreadPool(10); for(int i=0; i<10; i++){ String userId = "userId" + i; HttpEntity request = buildRequest(userId); executorService.submit(() -> { try { countDownLatch.await(); System.out.println("Thread:"+Thread.currentThread().getName()+", time:"+System.currentTimeMillis()); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); System.out.println("Thread:"+Thread.currentThread().getName() + "," + response.getBody()); } catch (InterruptedException e) { e.printStackTrace(); } }); } countDownLatch.countDown(); } private HttpEntity buildRequest(String userId) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set("Authorization", "yourToken"); Map<String, Object> body = new HashMap<>(); body.put("userId", userId); return new HttpEntity<>(body, headers); } }
成功防止重复提交,控制台日志如下,可以看到十个线程的启动时间几乎同时发起,只有一个请求提交成功了
https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/repeat-submit-intercept
build项目之后,启动本地redis,运行项目自动执行测试方法
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
打印的文档 总被同事误拿,涉密文件定怕被别人看到,手把手教你如何使用密码打印功能。适用奔图机型:M7300FDW、CP2506DNPLUS、CM7105DN等具有密码打印功能的奔图打印机。
在银行、酒店、机场等行业工作需要大在量的票据复印,一般的复印机仅仅满足普通文本复印发票上的“数字”“单位”等关键内容复印不清晰,几乎难以辩认失动汇了存档、交流的价值,四步教你一键票据清晰复印。主要适用于奔图M6800FDW、M6870FDW、M7200FDN等多功能打印机。
如何使用奔图打印机自动双面扫描、自动双面复印功能?适用于M7300FDW\M7300FDN\CM71050D等型号
如何使用奔图打印机的精细打印功能?从事建筑、装修等行业,经常需要打印综合预留、水电暖施工等专业图纸,为了保障设计施工标准化、效率化,必须将所有信息清晰呈现,但是,普通打印很难实现这一点,看奔图打印机如何交出满意答卷,适用于P3010\P3300\M6700\M7100\M6800\M7200\M7300系列打印机
卡纸的位置千奇百怪,卡纸的原因五花八门,看着一张张平坦的白纸,硬是卡成了“扇子”,好想拿个锤子让其灰飞烟灭,教你如何使用奔图打印机的卡纸分离设计功能,适用于P2206/P2500/P2506/M6202/M6500/M6506/M6500/M6556/M6600/M6606等系列打印机
电脑、文件夹、日历、水杯、手纸再加一台打印机和各种线,天啊!我的办公桌承受了它本不该承受的,把打印机放在其位置,距离又太远,线不够长,试试奔图的无线打印功能。适用于M7170DW、P3010DW等型号中带“W”的奔图黑白激光打印机。
软件正版化工作纲领性的文件,由国务院办公厅于2013年下发,掌握这个文件的内容基本上就清楚整个软件正版化工作。
WPS使用技巧:https://www.wps.cn/learning/question/index
使用金仓数据库V8R2的人群
prtyaa 收益393.62元
zlj141319 收益218元
1843880570 收益214.2元
IT-feng 收益209.03元
风晓 收益208.24元
777 收益172.71元
Fhawking 收益106.6元
信创来了 收益105.84元
克里斯蒂亚诺诺 收益91.08元
技术-小陈 收益79.5元
扫一扫关注公众号
扫一扫加入交流群
加入交流群
请使用微信扫一扫!