Redis+IDEA實(shí)現(xiàn)單機(jī)鎖和分布式鎖的過程
單機(jī)下:

只適用于單機(jī)環(huán)境下(單個JVM),多個客戶端訪問同一個服務(wù)器
1.synchronized
package com.cloud.SR.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestConrtoller1 {
@Value("${server.port}")
private String serverPort;
@Resource
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/buy1")
public String shopping(){
synchronized (this){
String result = stringRedisTemplate.opsForValue().get("goods:001");
int total = result == null? 0 :Integer.parseInt(s);
if(total > 0){
int realTotal = total - 1;
stringRedisTemplate.opsForValue().set("goods:001",String.valueOf(realCount ));
System.out.println("剩余商品為:"+realCount +",提供服務(wù)的端口號:"+serverPort);
return "剩余商品為:"+realTotal +",提供服務(wù)的端口號:"+serverPort;
}else{
System.out.println("購買商品失??!");
}
return "購買商品失??!";
}
}
}
2.ReentrantLock
@RestController
public class TestConrtoller2 {
@Value("${server.port}")
private String serverPort;
// 使用ReentrantLock鎖解決單體應(yīng)用的并發(fā)問題
Lock lock = new ReentrantLock();
@Autowired
StringRedisTemplate stringRedisTemplate;
@RequestMapping("/buy2")
public String index() {
lock.lock();
try {
String result = stringRedisTemplate.opsForValue().get("goods:001");
int total = result == null ? 0 : Integer.parseInt(result);
if (total > 0) {
int realTotal = total - 1;
stringRedisTemplate.opsForValue().set("goods:001", String.valueOf(realTotal));
System.out.println("購買商品成功,庫存還剩:" + realTotal + ",服務(wù)端口為:"+serverPort);
return "購買商品成功,庫存還剩:" + realTotal + ",服務(wù)端口為:"+serverPort;
} else {
System.out.println("購買商品失??!");
}
} catch (Exception e) {
lock.unlock();
} finally {
lock.unlock();
}
return "購買商品失敗!";
}
}分布式下:
而在服務(wù)器分布式集群下,,單個服務(wù)器的synchronized和ReentrantLock

1.SETNX
SET key value [EX seconds] [PX milliseconds] [NX|XX]
EXseconds – 設(shè)置鍵key的過期時間,單位時秒PXmilliseconds – 設(shè)置鍵key的過期時間,單位時毫秒NX– 只有鍵key不存在的時候才會設(shè)置key的值XX– 只有鍵key存在的時候才會設(shè)置key的值
例子:
1.set lock01 01 NX :意思就是說只要誰把key為lock01的值設(shè)置為01且key不存在的時候就能拿到鎖
2. set lock01 01 NX EX 30 :在例1的基礎(chǔ)上把鎖設(shè)置的時間設(shè)置為30秒后過期。避免有服務(wù)掛了而沒有釋放鎖的情況、或者業(yè)務(wù)處理完但一直拿著鎖不釋放導(dǎo)致死鎖。
項(xiàng)目中使用SETNX:
template.opsForValue().setIfAbsent()
測試的話就得本機(jī)模擬集群,當(dāng)然有虛擬機(jī)的也可以用兩臺虛擬機(jī),但此處用兩臺JVM即可完成簡易集群本機(jī)實(shí)現(xiàn)集群的可以看這篇文章:http://t.csdn.cn/jvZFx
先讓集群跑起來,然后啟動Nginx,再通過Jmeter實(shí)現(xiàn)高并發(fā)的秒殺環(huán)節(jié)

用template.opsForValue().setIfAbsent()命令進(jìn)行加鎖。加上了過期時間后就解決了key無法刪除的問題,但如果key設(shè)置的時間太短,當(dāng)業(yè)務(wù)處理的時間長于key設(shè)置的時間,key過期后其他請求就可以設(shè)置這個key而當(dāng)這個線程再回來處理這個程序的時候就會把人家設(shè)置的key給刪除了,因此我們規(guī)定誰設(shè)置的鎖只能由誰刪除。
finally {
// 誰加的鎖,誰才能刪除
if(template.opsForValue().get(REDIS_LOCK).equals(value)){
template.delete(REDIS_LOCK);
}而新的問題就是finally塊的判斷和del刪除操作不是原子操作,并發(fā)的時候也會出問題。因此采用lua(原子性)來進(jìn)行刪除
finally {
// 誰加的鎖,誰才能刪除,使用Lua腳本,進(jìn)行鎖的刪除
Jedis jedis = null;
try{
jedis = RedisUtils.getJedis();
String script = "if redis.call('get',KEYS[1]) == ARGV[1] " +
"then " +
"return redis.call('del',KEYS[1]) " +
"else " +
" return 0 " +
"end";
Object eval = jedis.eval(script, Collections.singletonList(REDIS_LOCK), Collections.singletonList(value));
if("1".equals(eval.toString())){
System.out.println("-----del redis lock ok....");
}else{
System.out.println("-----del redis lock error ....");
}
}catch (Exception e){
}finally {
if(null != jedis){
jedis.close();
}
}總的代碼:
@RestController
public class TestConrtoller3 {
@Value("${server.port}")
private String serverPort;
public static final String REDIS_LOCK = "good_lock";
@Autowired
StringRedisTemplate stringtemplate;
@RequestMapping("/buy3")
public String shopping(){
// 每個人進(jìn)來先要進(jìn)行加鎖,key值為"good_lock",且用UUID保證每個人的鎖不同
String value = UUID.randomUUID().toString().replace("-","");
try{
// 為key加一個過期時間
Boolean flag = stringtemplate.opsForValue().setIfAbsent(REDIS_LOCK, value,10L,TimeUnit.SECONDS);
// 加鎖失敗
if(!flag){
return "搶鎖失敗!";
}
System.out.println( value+ " 搶鎖成功");
String result = stringtemplate.opsForValue().get("goods:001");
int total = result == null ? 0 : Integer.parseInt(result);
if (total > 0) {
// 如果在此處需要調(diào)用其他微服務(wù),處理時間較長。。。
int realTotal = total - 1;
stringtemplate.opsForValue().set("goods:001", String.valueOf(realTotal));
System.out.println("購買商品成功,庫存還剩:" + realTotal + ",服務(wù)端口為"+serverPort);
return "購買商品成功,庫存還剩:" + realTotal + "服務(wù)端口為"+serverPort;
} else {
System.out.println("購買商品失敗");
}
return "購買商品失?。?;
}finally {
// 誰加的鎖,誰才能刪除,使用Lua腳本,進(jìn)行鎖的刪除
Jedis jedis = null;
try{
jedis = RedisUtils.getJedis();
String script = "if redis.call('get',KEYS[1]) == ARGV[1] " +
"then " +
"return redis.call('del',KEYS[1]) " +
"else " +
" return 0 " +
"end";
Object eval = jedis.eval(script, Collections.singletonList(REDIS_LOCK), Collections.singletonList(value));
if("1".equals(eval.toString())){
System.out.println("-----del redis lock ok....");
}else{
System.out.println("-----del redis lock error ....");
}
}catch (Exception e){
}finally {
if(null != jedis){
jedis.close();
}
}
}
}
}2.Redisson(推薦)
考慮緩存續(xù)命,以及Redis集群部署下,異步復(fù)制造成的鎖丟失:主節(jié)點(diǎn)沒來得及把剛剛set進(jìn)來這條數(shù)據(jù)給從節(jié)點(diǎn),就掛了。所以直接上RedLock的Redisson落地實(shí)現(xiàn)
@RestController
public class TestConrtoller4 {
@Value("${server.port}")
private String serverPort;
public static final String REDIS_LOCK = "good_lock";
@Autowired
StringRedisTemplate stringtemplate;
@Autowired
Redisson redisson;
@RequestMapping("/buy4")
public String shopping(){
RLock lock = redisson.getLock(REDIS_LOCK);
lock.lock();
// 每個人進(jìn)來先要進(jìn)行加鎖,key值為"good_lock"
String value = UUID.randomUUID().toString().replace("-","");
try{
String result = stringtemplate.opsForValue().get("goods:001");
int total = result == null ? 0 : Integer.parseInt(result);
if (total > 0) {
// 如果在此處需要調(diào)用其他微服務(wù),處理時間較長
int realTotal = total - 1;
stringtemplate.opsForValue().set("goods:001", String.valueOf(realTotal));
System.out.println("購買商品成功,庫存還剩:" + realTotal + "件, 服務(wù)端口為"+serverPort);
return "購買商品成功,庫存還剩:" + realTotal + "件, 服務(wù)端口為"+serverPort;
} else {
System.out.println("購買商品失敗");
}
return "購買商品失敗";
}finally {
if(lock.isLocked() && lock.isHeldByCurrentThread()){
lock.unlock();
}
}
}
}Redis工具類
import com.myfutech.common.util.constant.RedisPrefix;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
/**
* 基于redis分布式鎖
*/
@Slf4j
public class RedisLockUtils {
/**
* 默認(rèn)輪休獲取鎖間隔時間, 單位:毫秒
*/
private static final int DEFAULT_ACQUIRE_RESOLUTION_MILLIS = 100;
private static final String UNLOCK_LUA;
static {
StringBuilder sb = new StringBuilder();
sb.append("if redis.call(\"get\",KEYS[1]) == ARGV[1] ");
sb.append("then ");
sb.append(" return redis.call(\"del\",KEYS[1]) ");
sb.append("else ");
sb.append(" return 0 ");
sb.append("end ");
UNLOCK_LUA = sb.toString();
}
/**
* 獲取鎖,沒有獲取到則一直等待,異常情況則返回null
*
* @param redisTemplate redis連接
* @param key redis key
* @param expire 鎖過期時間, 單位 秒
* @return 當(dāng)前鎖唯一id,如果沒有獲取到,返回 null
*/
public static String lock(RedisTemplate redisTemplate, final String key, long expire){
return lock(redisTemplate, key, expire, -1);
}
/**
* 獲取鎖,acquireTimeout時間內(nèi)沒有獲取到,則返回null,異常情況返回null
*
* @param redisTemplate redis連接
* @param key redis key
* @param expire 鎖過期時間, 單位 秒
* @param acquireTimeout 獲取鎖超時時間, -1代表永不超時, 單位 秒
* @return 當(dāng)前鎖唯一id,如果沒有獲取到,返回 null
*/
public static String lock(RedisTemplate redisTemplate, final String key, long expire, long acquireTimeout){
try {
return acquireLock(redisTemplate, key, expire, acquireTimeout);
} catch (Exception e) {
log.error("acquire lock exception", e);
}
return null;
}
/**
* 獲取鎖,沒有獲取到則一直等待,沒有獲取到則拋出異常
*
* @param redisTemplate redis連接
* @param key redis key
* @param expire 鎖過期時間, 單位 秒
* @return 當(dāng)前鎖唯一id,如果沒有獲取到,返回 null
*/
public static String lockFailThrowException(RedisTemplate redisTemplate, final String key, long expire){
return lockFailThrowException(redisTemplate, key, expire, -1);
}
/**
* 獲取鎖,到達(dá)超時時間時沒有獲取到,則拋出異常
*
* @param redisTemplate redis連接
* @param key redis key
* @param expire 鎖過期時間, 單位 秒
* @param acquireTimeout 獲取鎖超時時間, -1代表永不超時, 單位 秒
* @return 當(dāng)前鎖唯一id,如果沒有獲取到,返回 null
*/
public static String lockFailThrowException(RedisTemplate redisTemplate, final String key, long expire, long acquireTimeout){
try {
String lockId = acquireLock(redisTemplate, key, expire, acquireTimeout);
if (lockId != null) {
return lockId;
}
throw new RuntimeException("acquire lock fail");
} catch (Exception e) {
throw new RuntimeException("acquire lock exception", e);
}
}
private static String acquireLock(RedisTemplate redisTemplate, String key, long expire, long acquireTimeout) throws InterruptedException {
long acquireTime = -1;
if (acquireTimeout != -1) {
acquireTime = acquireTimeout * 1000 + System.currentTimeMillis();
}
synchronized (key) {
String lockId = UUID.randomUUID().toString();
while (true) {
if (acquireTime != -1 && acquireTime < System.currentTimeMillis()) {
break;
}
//調(diào)用tryLock
boolean hasLock = tryLock(redisTemplate, key, expire, lockId);
//獲取鎖成功
if (hasLock) {
return lockId;
}
Thread.sleep(DEFAULT_ACQUIRE_RESOLUTION_MILLIS);
}
}
return null;
}
/**
* 釋放鎖
*
* @param redisTemplate redis連接
* @param key redis key
* @param lockId 當(dāng)前鎖唯一id
*/
public static void unlock(RedisTemplate redisTemplate, String key, String lockId) {
try {
RedisCallback<Boolean> callback = (connection) ->
connection.eval(UNLOCK_LUA.getBytes(StandardCharsets.UTF_8),ReturnType.BOOLEAN, 1,
(RedisPrefix.LOCK_REDIS_PREFIX + key).getBytes(StandardCharsets.UTF_8), lockId.getBytes(StandardCharsets.UTF_8));
redisTemplate.execute(callback);
} catch (Exception e) {
log.error("release lock exception", e);
}
}
/**
* 獲取當(dāng)前鎖的id
*
* @param key redis key
* @return 當(dāng)前鎖唯一id
*/
public static String get(RedisTemplate redisTemplate, String key) {
try {
RedisCallback<String> callback = (connection) -> {
byte[] bytes = connection.get((RedisPrefix.LOCK_REDIS_PREFIX + key).getBytes(StandardCharsets.UTF_8));
if (bytes != null){
return new String(bytes, StandardCharsets.UTF_8);
}
return null;
};
return (String)redisTemplate.execute(callback);
} catch (Exception e) {
log.error("get lock id exception", e);
}
return null;
}
private static boolean tryLock(RedisTemplate redisTemplate, String key, long expire, String lockId) {
RedisCallback<Boolean> callback = (connection) ->
connection.set((RedisPrefix.LOCK_REDIS_PREFIX + key).getBytes(StandardCharsets.UTF_8),
lockId.getBytes(StandardCharsets.UTF_8), Expiration.seconds(expire), RedisStringCommands.SetOption.SET_IF_ABSENT);
return (Boolean)redisTemplate.execute(callback);
}
}到此這篇關(guān)于Redis+IDEA極速了解和實(shí)現(xiàn)單機(jī)鎖和分布式鎖的文章就介紹到這了,更多相關(guān)Redis單機(jī)鎖和分布式鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于redis Key淘汰策略的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄P(guān)于redis Key淘汰策略的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
Redis實(shí)現(xiàn)用戶關(guān)注的項(xiàng)目實(shí)踐
本文主要介紹了Redis實(shí)現(xiàn)用戶關(guān)注的項(xiàng)目實(shí)踐,通過使用Redis的set數(shù)據(jù)結(jié)構(gòu)來存儲關(guān)注對象,方便高效地進(jìn)行添加和取消關(guān)注操作,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Redis 分片集群搭建與故障轉(zhuǎn)移實(shí)戰(zhàn)指南(推薦)
本文介紹了如何搭建一個最小的Redis分片集群,并詳細(xì)描述了集群的啟動、創(chuàng)建、測試、伸縮和故障轉(zhuǎn)移過程,同時,還介紹了如何使用RedisTemplate訪問分片集群,感興趣的朋友跟隨小編一起看看吧2025-12-12
RedisDesktopManager遠(yuǎn)程連接redis的實(shí)現(xiàn)
本文主要介紹了RedisDesktopManager遠(yuǎn)程連接redis的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

