分布式鎖實例教程之防止重復提交
拋出一個問題
需求:現(xiàn)在有一個常見的場景——用戶注冊,但是如果出現(xiàn)重復提交的情況,則會出現(xiàn)多條注冊數(shù)據,因此這里如何做好防止重復提交這是我們需要解決的問題。
正常的代碼邏輯
1、注冊controller
/** * 用戶注冊請求 * @param userDto * @param bindingResult * @return */ @RequestMapping(value=prefix+"/db/register",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) public BaseResponse register(@RequestBody @Validated UserDto userDto, BindingResult bindingResult){ BaseResponse response=new BaseResponse(StatusCode.Success); try { log.debug("注冊信息: {} ",userDto); //注冊之前,我們先判斷是否已經注冊了。(正常邏輯) User user=userService.selectByUserName(userDto.getUserName()); if (user!=null){ return new BaseResponse(StatusCode.UserNameExist); } userService.register(userDto); }catch (Exception e){ e.printStackTrace(); response=new BaseResponse(StatusCode.Fail); } return response; }
在controller中判斷用戶是否已經注冊,如果沒有注冊,則調用注冊邏輯。
2、注冊service
/** * 用戶注冊——最普通的操作,沒有任何加鎖,沒有任何防止重復提交 * * @param userDto * @return * @throws Exception */ public int register(UserDto userDto) throws Exception { int result = 0; User user = new User(); BeanUtils.copyProperties(userDto, user); result = userMapper.insertSelective(user); return result; }
簡單的增加一個用戶信息。
問題也很明顯,這樣畢竟會出現(xiàn)問題,并發(fā)的問題,也會出現(xiàn)重復注冊的情況。測試結果也很明顯
一堆重復注冊的,但是加入分布式鎖就好了么?
3、加入分布式鎖,問題依舊
分布式鎖的實現(xiàn)方式
/** * 用戶注冊,基于redisson的分布式鎖 * * @param userDto * @return */ public int registerLockRedisson(UserDto userDto) { int result = 0; RLock rLock = redissonLockComponent.acquireLock(userDto.getUserName()); try { if (rLock != null) { User user = new User(); BeanUtils.copyProperties(userDto, user); user.setCreateTime(new Date()); userMapper.insertSelective(user); } } catch (Exception e) { log.error("獲取redisson分布式鎖異常"); } finally { if (rLock != null) { redissonLockComponent.releaseLock(rLock); } } return result; }
加入分布式鎖之后,再進行測試。
不好意思,依舊出現(xiàn)了重復注冊的情況。何解?
問題分析,為了遵循單一職責,這里的讀取數(shù)據(判斷是否注冊)與寫入數(shù)據(用戶注冊)操作是分開的,分布式鎖為了進一步細化,只是加在了寫入數(shù)據階段,并沒有加在整個業(yè)務階段,因此會出現(xiàn)數(shù)據重復提交的問題,解決方法有很多,最暴力的方法無非就是給數(shù)據庫user表中的用戶名字段加入唯一約束。但是這樣隨著業(yè)務規(guī)模擴大,數(shù)據庫壓力會越來越大。
解決方法
解決方法有幾種,前面提到的給數(shù)據庫增加唯一索引也是一種方法。但是為了減輕數(shù)據庫的壓力,這種操作可以直接在應用層處理。
分布式鎖+防重操作
在分布式鎖的基礎上,加入redis存儲key值,作為防重提交的判斷。不想過多解釋了,直接上代碼吧。
/** * 用戶注冊,redisson分布式鎖,redis防止重復提交 * * @param userDto * @return */ public int registerLockAvoidDupPost(UserDto userDto) { int result = 0; RLock rLock = redissonLockComponent.acquireLock(userDto.getUserName()); try { //redis中根據用戶名存儲作為key值 String key = lockKeyPrefix+userDto.getUserName(); if (!stringRedisTemplate.hasKey(key)) {//如果不存在key則進入注冊階段 stringRedisTemplate.opsForValue().set(key,UUID.randomUUID().toString(),10L,TimeUnit.SECONDS); User user = new User(); BeanUtils.copyProperties(userDto, user); user.setCreateTime(new Date()); userMapper.insertSelective(user); log.info("{},注冊成功",userDto.getUserName()); }else{//如果存在,則提示不可重復提交 log.error("10秒內,請勿重復提交注冊信息"); } } catch (Exception e) { log.error("獲取redisson分布式鎖異常"); } finally { if (rLock != null) { redissonLockComponent.releaseLock(rLock); } } return result; }
分布式鎖的實現(xiàn)方式有多重,redis/redisson/zookeeper等,只需要在已經實現(xiàn)分布式鎖的基礎上引入防重提交的機制即可。
因此還有其他方式的實現(xiàn),如下所示為zookeeper分布式鎖+redis防重的方式
/** * 用戶注冊,redisson分布式鎖,redis防止重復提交 * * @param userDto * @return */ public int registerLockAvoidDupPost(UserDto userDto) { int result = 0; InterProcessMutex mutex=new InterProcessMutex(client,zkPrefix+userDto.getUserName()+"-lock"); try { if (mutex.acquire(10L, TimeUnit.SECONDS)){ final String realKey=zkRedisKeyPrefix+userDto.getUserName(); if (!stringRedisTemplate.hasKey(realKey)){ stringRedisTemplate.opsForValue().set(realKey, UUID.randomUUID().toString()); User user=new User(); BeanUtils.copyProperties(userDto,user); user.setCreateTime(new Date()); userMapper.insertSelective(user); log.info("{},注冊成功",userDto.getUserName()); }else{ log.error("10秒內,請勿重復提交注冊信息"); } }else{ throw new RuntimeException("獲取zk分布式鎖失敗!"); } }catch (Exception e){ e.printStackTrace(); throw e; }finally { mutex.release(); } return result; }
測試結果:
并不會出現(xiàn)重復注冊情況了。
總結
防重提交不能全部交給數(shù)據庫
到此這篇關于分布式鎖實例教程之防止重復提交的文章就介紹到這了,更多相關分布式鎖防止重復提交內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springcloud+nacos實現(xiàn)灰度發(fā)布示例詳解
這篇文章主要介紹了springcloud+nacos實現(xiàn)灰度發(fā)布,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08SparkSQL使用IDEA快速入門DataFrame與DataSet的完美教程
本文給大家介紹使用idea開發(fā)Spark SQL 的詳細過程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-08-08