Spring Boot 使用 AOP來防止重複提交

思路

  1. 自定義註解 @NoRepeatSubmit 標記全部Controller中的提交請求java

  2. 經過AOP 對全部標記了 @NoRepeatSubmit 的方法攔截redis

  3. 在業務方法執行前,獲取當前用戶的 token(或者JSessionId)+ 當前請求地址,做爲一個惟一 KEY,去獲取 Redis 分佈式鎖(若是此時併發獲取,只有一個線程會成功獲取鎖)安全

  4. 業務方法執行後,釋放鎖多線程

關於Redis 分佈式鎖併發

  • 不瞭解的同窗戳這裏 ==> Redis分佈式鎖的正確實現方式負載均衡

  • 使用Redis 是爲了在負載均衡部署,若是是單機的部署的項目能夠使用一個線程安全的本地Cache 替代 Redisdom

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(); } ​ }

 

多線程測試

測試代碼以下,模擬十個請求併發同時提交ide

 @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); } ​ }

 

成功防止重複提交,控制檯日誌以下,能夠看到十個線程的啓動時間幾乎同時發起,只有一個請求提交成功了post

img

結論

你們有什麼要說的,歡迎在評論區留言

對了,小編爲你們準備了一套2020最新的Java資料,須要點擊下方連接獲取方式

一、點贊+評論(勾選「同時轉發」)

學習java,你掌握這些。二三線也能輕鬆拿8K以上

相關文章
相關標籤/搜索