Redisson分布式锁实战-1:构建分布式锁
生活随笔
收集整理的這篇文章主要介紹了
Redisson分布式锁实战-1:构建分布式锁
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
我們現(xiàn)在來(lái)到Task類當(dāng)中,這個(gè)方法就是V4了/*** Redisson分布式鎖實(shí)現(xiàn)* @throws InterruptedException*/
// @Scheduled(cron="0 */1 * * * ?")//每1分鐘(每個(gè)1分鐘的整數(shù)倍)public void closeOrderTaskV4() throws InterruptedException {RLock lock = redissonManager.getRedisson().getLock(Const.REDIS_LOCK.CLOSE_ORDER_TASK_LOCK);boolean getLock = false;try {if(getLock = lock.tryLock(2,50, TimeUnit.SECONDS)){//trylock增加鎖log.info("===獲取{},ThreadName:{}",Const.REDIS_LOCK.CLOSE_ORDER_TASK_LOCK,Thread.currentThread().getName());int hour = Integer.parseInt(PropertiesUtil.getProperty("close.order.task.time.hour","2"));iOrderService.closeOrder(hour);}else{log.info("===沒(méi)有獲得分布式鎖:{}",Const.REDIS_LOCK.CLOSE_ORDER_TASK_LOCK);}}finally {if(!getLock){return;}log.info("===釋放分布式鎖:{}",Const.REDIS_LOCK.CLOSE_ORDER_TASK_LOCK);lock.unlock();}}V4我們就要用Redisson來(lái)搞定分布式鎖,其實(shí)我們?cè)鷮?shí)現(xiàn)的單點(diǎn)登陸,使用Spring Session實(shí)現(xiàn)單點(diǎn)登陸,原先我們實(shí)現(xiàn)的分布式鎖,然后使用Redisson分布式鎖都是這么一個(gè)過(guò)程,先原生實(shí)現(xiàn),然后調(diào)用框架實(shí)現(xiàn),本身我們?cè)鷮?shí)現(xiàn)完之后,基礎(chǔ)會(huì)更扎實(shí),然后再學(xué)習(xí)一些簡(jiǎn)單的框架呢,也會(huì)更快,例如Spring Session侵入性會(huì)比較小,那在改造的時(shí)候呢,在單位推動(dòng)這個(gè)框架也更容易一些,使用Redisson來(lái)實(shí)現(xiàn)這個(gè)分布式鎖,首先要把之前聲明的RedissonManager注入到我們這個(gè)類當(dāng)中,@Autowiredprivate RedissonManager redissonManager;然后直接使用他,首先聲明一個(gè)RLock,Redisson的一個(gè)lock,我們先獲取到Redisson這個(gè)實(shí)例,我們點(diǎn)tryLock,tryLock是嘗試獲取鎖,也就是我們?cè)瓉?lái)V3講的版本,這里面嘗試獲取鎖,把這些都封裝好了,第一個(gè)有參數(shù)可以添加等待時(shí)間,鎖的自動(dòng)解鎖時(shí)間,還有時(shí)間的一個(gè)單位,首先waittime是什么,嘗試獲取鎖的時(shí)候,我最多等待兩秒,那多久釋放呢,5秒釋放,也就是說(shuō)這個(gè)鎖最多放5秒,然后最后寫(xiě)上單位,tryLock的返回值是一個(gè)布爾/*** Returns <code>true</code> as soon as the lock is acquired.* If the lock is currently held by another thread in this or any* other process in the distributed system this method keeps trying* to acquire the lock for up to <code>waitTime</code> before* giving up and returning <code>false</code>. If the lock is acquired,* it is held until <code>unlock</code> is invoked, or until <code>leaseTime</code>* have passed since the lock was granted - whichever comes first.** @param waitTime the maximum time to aquire the lock* @param leaseTime lease time* @param unit time unit* @return <code>true</code> if lock has been successfully acquired* @throws InterruptedException - if the thread is interrupted before or during this method.*/boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;那返回值代表是否獲取鎖成功,我們?cè)谏厦娼o一個(gè)默認(rèn)值,getLock是否獲取到鎖,默認(rèn)是false,然后這里面要做一個(gè)判斷,那座判斷的時(shí)候還要給他更新,獲取鎖并把返回值賦給getLock,如果為true的話就會(huì)進(jìn)入到if里邊,我們打印一個(gè)日志,Redisson獲取分布式鎖,線程的名字,鎖的名字,然后還要獲取我們的小時(shí),這里面我們就不調(diào)用closeOrder了,我們把分布式鎖都交給Redisson來(lái)管理了,所以我們這里封裝的,包括時(shí)間的控制,我們都不需要操作了,我們只把這個(gè)時(shí)間拿過(guò)來(lái),然后可以直接調(diào)用iOrderService.closeOrder(hour),那這個(gè)Catch是一個(gè)打斷的Exception,那這個(gè)Catch我們也要打一個(gè)日志,Redisson鎖獲取異常,這里面需要注意一下,我們要把finally加上,提高一下我們的代碼健壯性,如果getLock沒(méi)有拿到的話,直接返回,所以我們?cè)趂inally里面調(diào)用unlock來(lái)釋放這個(gè)鎖,也就是說(shuō),鎖用完之后,finally是肯定要執(zhí)行的,然后在這里面要釋放這個(gè)鎖了,然后打印一個(gè)日志,Redisson分布式鎖釋放鎖,finally一定不要忘記加,提高了我們代碼的一個(gè)健壯性,我在tryLock的時(shí)候只等一秒,沒(méi)有的時(shí)候就走人了
?
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的Redisson分布式锁实战-1:构建分布式锁的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Redisson初始化
- 下一篇: Redisson分布式锁实战-2:解决w