SpringBoot中使用Redis作為全局鎖示例過程
微服務(wù)的項目中,一個服務(wù)我們啟動多份,在不同的進程中。這些服務(wù)是無狀態(tài)的,而由數(shù)據(jù)存儲容器(mysql/redis/es)進行狀態(tài)數(shù)據(jù)的持久化。這就會導(dǎo)致資源競爭,出現(xiàn)多線程的問題。
一、模擬沒有鎖情況下的資源競爭
public class CommonConsumerService {
//庫存?zhèn)€數(shù)
static int goodsCount = 900;
//賣出個數(shù)
static int saleCount = 0;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000; i++) {
new Thread(() -> {
try {Thread.sleep(2);} catch (InterruptedException e) {}
if (goodsCount > 0) {
goodsCount--;
System.out.println("剩余庫存:" + goodsCount + " 賣出個數(shù)" + ++saleCount);
}
}).start();
}
Thread.sleep(3000);
}
}運行一次,最后幾行的輸出結(jié)果如下,很明顯出錯了,剩余0個商品卻只賣出了899個商品,很明顯有商品被某個線程私吞了。
...
剩余庫存:5 賣出個數(shù)893
剩余庫存:5 賣出個數(shù)894
剩余庫存:4 賣出個數(shù)895
剩余庫存:2 賣出個數(shù)896
剩余庫存:2 賣出個數(shù)897
剩余庫存:1 賣出個數(shù)898
剩余庫存:0 賣出個數(shù)899
二、使用redis加鎖
redis是單線程的,串行執(zhí)行,那么接下來使用redis為資源進行加鎖。
1.首先引入依賴
compile "org.springframework.boot:spring-boot-starter-data-redis"
2.引入redis加鎖工具類
package com.kingboy.common.utils;
import redis.clients.jedis.Jedis;
import java.util.Collections;
/**
* @author kingboy--KingBoyWorld@163.com
* @date 2017/12/29 下午1:57
* @desc Redis工具.
*/
public class RedisTool {
private static final String LOCK_SUCCESS = "OK";
private static final String SET_IF_NOT_EXIST = "NX";
private static final String SET_WITH_EXPIRE_TIME = "PX";
private static final Long RELEASE_SUCCESS = 1L;
/**
* 嘗試獲取分布式鎖
* @param jedis Redis客戶端
* @param lockKey 鎖
* @param requestId 請求標識
* @param expireTime 超期時間
* @return 是否獲取成功
*/
public static boolean tryGetDistributedLock(Jedis jedis, String lockKey, String requestId, int expireTime) {
String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
if (LOCK_SUCCESS.equals(result)) {
return true;
}
return false;
}
/**
* 釋放分布式鎖
* @param jedis Redis客戶端
* @param lockKey 鎖
* @param requestId 請求標識
* @return 是否釋放成功
*/
public static boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) {
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
if (RELEASE_SUCCESS.equals(result)) {
return true;
}
return false;
}
}3.將上面沒有鎖的示例代碼改編如下:
public class RedisLockConsumerService {
//庫存?zhèn)€數(shù)
static int goodsCount = 900;
//賣出個數(shù)
static int saleCount = 0;
@SneakyThrows
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "192.168.0.130", 6379, 1000);
for (int i = 0; i < 1000; i++) {
new Thread(() -> {
try {Thread.sleep(2);} catch (InterruptedException e) {}
Jedis jedis = jedisPool.getResource();
boolean lock = false;
while (!lock) {
lock = RedisTool.tryGetDistributedLock(jedis, "goodsCount", Thread.currentThread().getName(), 10);
}
if (lock) {
if (goodsCount > 0) {
goodsCount--;
System.out.println("剩余庫存:" + goodsCount + " 賣出個數(shù)" + ++saleCount);
}
}
RedisTool.releaseDistributedLock(jedis, "goodsCount", Thread.currentThread().getName());
jedis.close();
}).start();
}
Thread.sleep(3000);
jedisPool.close();
}
}執(zhí)行幾次程序輸出結(jié)果如下,可以看到結(jié)果是有序,并且正確的。
...
剩余庫存:6 賣出個數(shù)894
剩余庫存:5 賣出個數(shù)895
剩余庫存:4 賣出個數(shù)896
剩余庫存:3 賣出個數(shù)897
剩余庫存:2 賣出個數(shù)898
剩余庫存:1 賣出個數(shù)899
剩余庫存:0 賣出個數(shù)900
以上就是SpringBoot中使用Redis作為全局鎖示例過程的詳細內(nèi)容,更多關(guān)于SpringBoot Redis全局鎖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringCloud zookeeper作為注冊中心使用介紹
ZooKeeper由雅虎研究院開發(fā),是Google Chubby的開源實現(xiàn),后來托管到Apache,于2010年11月正式成為Apache的頂級項目。ZooKeeper是一個經(jīng)典的分布式數(shù)據(jù)一致性解決方案,致力于為分布式應(yīng)用提供一個高性能、高可用,且具有嚴格順序訪問控制能力的分布式協(xié)調(diào)服務(wù)2022-11-11
解決spring @ControllerAdvice處理異常無法正確匹配自定義異常
這篇文章主要介紹了解決spring @ControllerAdvice處理異常無法正確匹配自定義異常的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java中String判斷值為null或空及地址是否相等的問題
這篇文章主要介紹了Java中String判斷值為null或空及地址是否相等的問題,文中舉了簡單的例子對字符串類型的值和地址問題進行講解,需要的朋友可以參考下2016-01-01

