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

只適用于單機(jī)環(huán)境下(單個(gè)JVM),多個(gè)客戶端訪問同一個(gè)服務(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ù)的端口號(hào):"+serverPort);
return "剩余商品為:"+realTotal +",提供服務(wù)的端口號(hào):"+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ù)器分布式集群下,,單個(gè)服務(wù)器的synchronized和ReentrantLock

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

用template.opsForValue().setIfAbsent()命令進(jìn)行加鎖。加上了過期時(shí)間后就解決了key無法刪除的問題,但如果key設(shè)置的時(shí)間太短,當(dāng)業(yè)務(wù)處理的時(shí)間長于key設(shè)置的時(shí)間,key過期后其他請(qǐng)求就可以設(shè)置這個(gè)key而當(dāng)這個(gè)線程再回來處理這個(gè)程序的時(shí)候就會(huì)把人家設(shè)置的key給刪除了,因此我們規(guī)定誰設(shè)置的鎖只能由誰刪除。
finally {
// 誰加的鎖,誰才能刪除
if(template.opsForValue().get(REDIS_LOCK).equals(value)){
template.delete(REDIS_LOCK);
}而新的問題就是finally塊的判斷和del刪除操作不是原子操作,并發(fā)的時(shí)候也會(huì)出問題。因此采用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(){
// 每個(gè)人進(jìn)來先要進(jìn)行加鎖,key值為"good_lock",且用UUID保證每個(gè)人的鎖不同
String value = UUID.randomUUID().toString().replace("-","");
try{
// 為key加一個(gè)過期時(shí)間
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ù),處理時(shí)間較長。。。
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();
// 每個(gè)人進(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ù),處理時(shí)間較長
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)輪休獲取鎖間隔時(shí)間, 單位:毫秒
*/
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 鎖過期時(shí)間, 單位 秒
* @return 當(dāng)前鎖唯一id,如果沒有獲取到,返回 null
*/
public static String lock(RedisTemplate redisTemplate, final String key, long expire){
return lock(redisTemplate, key, expire, -1);
}
/**
* 獲取鎖,acquireTimeout時(shí)間內(nèi)沒有獲取到,則返回null,異常情況返回null
*
* @param redisTemplate redis連接
* @param key redis key
* @param expire 鎖過期時(shí)間, 單位 秒
* @param acquireTimeout 獲取鎖超時(shí)時(shí)間, -1代表永不超時(shí), 單位 秒
* @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 鎖過期時(shí)間, 單位 秒
* @return 當(dāng)前鎖唯一id,如果沒有獲取到,返回 null
*/
public static String lockFailThrowException(RedisTemplate redisTemplate, final String key, long expire){
return lockFailThrowException(redisTemplate, key, expire, -1);
}
/**
* 獲取鎖,到達(dá)超時(shí)時(shí)間時(shí)沒有獲取到,則拋出異常
*
* @param redisTemplate redis連接
* @param key redis key
* @param expire 鎖過期時(shí)間, 單位 秒
* @param acquireTimeout 獲取鎖超時(shí)時(shí)間, -1代表永不超時(shí), 單位 秒
* @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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RedisTemplate 實(shí)現(xiàn)基于Value 操作的簡易鎖機(jī)制(示例代碼)
本文將介紹如何使用 RedisTemplate 的 opsForValue().setIfAbsent() 方法來實(shí)現(xiàn)一種簡單的鎖機(jī)制,并提供一個(gè)示例代碼,展示如何在 Java 應(yīng)用中利用這一機(jī)制來保護(hù)共享資源的訪問,感興趣的朋友跟隨小編一起看看吧2024-05-05
Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializ
本文主要介紹了Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializer區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
CentOS7.5使用mysql_multi方式安裝MySQL5.7.28多實(shí)例(詳解)
這篇文章主要介紹了CentOS7.5使用mysql_multi方式安裝MySQL5.7.28多實(shí)例,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
React實(shí)現(xiàn)組件之間通信的幾種常用方法
在?React?中,組件之間的通信是構(gòu)建復(fù)雜應(yīng)用程序的核心部分,良好的組件間通信能夠提高代碼的可維護(hù)性和可讀性,同時(shí)能夠高效地管理應(yīng)用狀態(tài),在這篇博客中,我們將探討?React中幾種常用的組件通信方法,并提供示例代碼來幫助你理解,需要的朋友可以參考下2025-02-02
Redis bitmap 實(shí)現(xiàn)簽到案例(最新推薦)
這篇文章主要介紹了Redis bitmap 實(shí)現(xiàn)簽到案例,通過設(shè)計(jì)簽到功能對(duì)應(yīng)的數(shù)據(jù)庫表,結(jié)合sql語句給大家講解的非常詳細(xì),具體示例代碼跟隨小編一起看看吧2024-07-07

