SpringBoot使用Redis實現分布式鎖
前言
在單機應用時代,我們對一個共享的對象進行多線程訪問的時候,使用java的synchronized關鍵字或者ReentrantLock類對操作的對象加鎖就可以解決對象的線程安全問題。
分布式應用時代這個方法卻行不通了,我們的應用可能被部署到多臺機器上,運行在不同的JVM里,一個對象可能同時存在多臺機器的內存中,怎樣使共享對象同時只被一個線程處理就成了一個問題。
在分布式系統中為了保證一個對象在高并發的情況下只能被一個線程使用,我們需要一種跨JVM的互斥機制來控制共享資源的訪問,此時就需要用到我們的分布式鎖了。
分布式鎖一般有三種實現方式:1.通過數據庫實現分布式鎖;2.通過緩存(Redis等)實現分布式鎖;3.通過Zookeeper實現分布式鎖。本篇文章主要介紹第二種通過Redis實現分布式鎖的方式。
分布式鎖的需要具備的條件
為了保證分布式鎖的可用性,需要具備一下五點條件:
1、在同一時間保證只有一臺機器的一個線程可以持有鎖。2、不能發生死鎖,無論何時持有鎖的機器崩潰掛掉了都要能自動釋放鎖。3、高效的獲取和釋放鎖。4、具備非阻塞性,一旦獲取不到鎖就立刻返回加鎖失敗。5、獨占性,即自己加的鎖只有自己才能釋放。
代碼實現
組件依賴
首先在pom.xml文件中添加依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
加鎖代碼
代碼如下:
/** * 獲取鎖 * @param lockKey 鎖 * @param identity 身份標識(保證鎖不會被其他人釋放) * @param expireTime 鎖的過期時間(單位:秒) * @return */public boolean lock(String lockKey, String identity, long expireTime){ boolean lockResult = redisTemplate.opsForValue().setIfAbsent(lockKey, identity, expireTime, TimeUnit.SECONDS); return opsForValue;}
加鎖的方法只需要三個參數:lockKey、identity、expireTime。
第一個參數lockKey為key,一個資源對應一個唯一的key。 第二個參數identity為身份標識,作為此key對應的value存儲,為了判斷在釋放鎖時是不是和加鎖的身份相同,防止別人釋放鎖。 第三個參數expireTime為過期時間,此參數保證程序加鎖后崩潰導致不能主動釋放鎖的時候自動釋放鎖,防止出現死鎖。為什么使用setIfAbsent方法呢?這個方法的好處就是,如果redis中已經存在這個key了,就會返回失敗,并且不改變redis中的數據,這樣就不會把別的線程的加的鎖給覆蓋掉。
解鎖代碼
代碼如下:
/** * 釋放鎖 * @param lockKey 鎖 * @param identity 身份標識(保證鎖不會被其他人釋放) * @return */public boolean releaseLock(String lockKey, String identity){ String luaScript = 'if ' + ' redis.call(’get’, KEYS[1]) == ARGV[1] ' + 'then ' + ' return redis.call(’del’, KEYS[1]) ' + 'else ' + ' return 0 ' + 'end'; DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(); redisScript.setResultType(Boolean.class); redisScript.setScriptText(luaScript); List<String> keys = new ArrayList<>(); keys.add(lockKey); boolean result = redisTemplate.execute(redisScript, keys, identity); return result;}
解鎖的方法只需兩個參數:lockKey、identity。
第一個參數lockKey為key,一個資源對應一個唯一的key。 第二個參數identity為身份標識,作為此key對應的value存儲,為了判斷在釋放鎖時是不是和加鎖的身份相同,防止別人釋放鎖。此處使用Lua腳本來判斷身份,身份相同就刪除,身份不同就不對數據做操作并返回失敗。為什么要使用Lua腳本呢?這是為了要保證操作的原子性,redis在執行Lua腳本的時候是把腳本當作一個命令來執行的,我們都知道redis的命令是都是原子操作,這樣就保證了操作的原子性。
測試代碼
package com.qixi.lock.demo.lockdemo.controller;import com.qixi.lock.demo.lockdemo.util.RedisLock;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * 測試分布式鎖 * @author ZhengNC * @date 2020/5/13 17:27 */@RestController@RequestMapping('test')public class TestRedisLockController { private final String lockKeyName = 'testKey'; @Autowired private RedisLock redisLock; /** * 測試加鎖 * @param id 加鎖的資源id * @param identity 身份標識 * @return */ @GetMapping('lock') public String lock(@RequestParam('id') String id, @RequestParam('identity') String identity){ String lockKey = lockKeyName+':'+id; boolean lockSuccess = redisLock.lock(lockKey, identity, 60); String result = 'lock failed'; if (lockSuccess){ result = 'lock success'; } return result; } /** * 測試釋放鎖 * @param id 釋放鎖的資源id * @param identity 身份標識 * @return */ @GetMapping('release') public String release(@RequestParam('id') String id, @RequestParam('identity') String identity){ String lockKey = lockKeyName+':'+id; boolean releaseSuccess = redisLock.releaseLock(lockKey, identity); String result = 'release failed'; if (releaseSuccess){ result = 'release success'; } return result; }}
package com.qixi.lock.demo.lockdemo.util;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.script.DefaultRedisScript;import org.springframework.stereotype.Component;import java.util.ArrayList;import java.util.List;import java.util.concurrent.TimeUnit;/** * 分布式鎖Redis工具類 * @author ZhengNC * @date 2020/5/13 17:27 */@Componentpublic class RedisLock { @Autowired private RedisTemplate<String, String> redisTemplate; /** * 獲取鎖 * @param lockKey 鎖 * @param identity 身份標識(保證鎖不會被其他人釋放) * @param expireTime 鎖的過期時間(單位:秒) * @return */ public boolean lock(String lockKey, String identity, long expireTime){ boolean lockResult = redisTemplate.opsForValue().setIfAbsent(lockKey, identity, expireTime, TimeUnit.SECONDS); return lockResult; } /** * 釋放鎖 * @param lockKey 鎖 * @param identity 身份標識(保證鎖不會被其他人釋放) * @return */ public boolean releaseLock(String lockKey, String identity){ String luaScript ='if ' +' redis.call(’get’, KEYS[1]) == ARGV[1] ' +'then ' +' return redis.call(’del’, KEYS[1]) ' +'else ' +' return 0 ' +'end'; DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(); redisScript.setResultType(Boolean.class); redisScript.setScriptText(luaScript); List<String> keys = new ArrayList<>(); keys.add(lockKey); boolean result = redisTemplate.execute(redisScript, keys, identity); return result; }}
結語
感謝大家閱讀我的文章,更歡迎大家指出我的問題,希望能在這里通過討論取得共同的進步。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
