思路 自定義註解 @NoRepeatSubmit 標記所有Controller中的提交請求 通過AOP 對所有標記了 @NoRepeatSubmit 的方法攔截 在業務方法執行前,獲取當前用戶的 token(或者JSessionId)+ 當前請求地址,作為一個唯一 KEY,去獲取 Redis 分散式 ...
-
自定義註解 @NoRepeatSubmit 標記所有Controller中的提交請求
-
通過AOP 對所有標記了 @NoRepeatSubmit 的方法攔截
-
在業務方法執行前,獲取當前用戶的 token(或者JSessionId)+ 當前請求地址,作為一個唯一 KEY,去獲取 Redis 分散式鎖(如果此時併發獲取,只有一個線程會成功獲取鎖)
-
業務方法執行後,釋放鎖
關於Redis 分散式鎖
-
不瞭解的同學戳這裡 ==> Redis分散式鎖的正確實現方式
-
使用Redis 是為了在負載均衡部署,如果是單機的部署的項目可以使用一個線程安全的本地Cache 替代 Redis
Code
這裡只貼出 AOP 類和測試類,完整代碼見 ==> Gitee
@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> div = new HashMap<>(); div.put("userId", userId); return new HttpEntity<>(div, headers); } }
成功防止重覆提交,控制台日誌如下,可以看到十個線程的啟動時間幾乎同時發起,只有一個請求提交成功了
結論
大家有什麼要說的,歡迎在評論區留言
對了,小編為大家準備了一套2020最新的Java資料,需要點擊下方鏈接獲取方式