網站首頁 編程語言 正文
分布式鎖
為了保證一個方法在高并發情況下的同一時間只能被同一個線程執行,在傳統單體應用單機部署的情況下,可以使用Java并發處理相關的API(如ReentrantLcok或synchronized)進行互斥控制。但是,隨著業務發展的需要,原單體單機部署的系統被演化成分布式系統后,由于分布式系統多線程、多進程并且分布在不同機器上,這將使原單機部署情況下的并發控制鎖策略失效,為了解決這個問題就需要一種跨JVM的互斥機制來控制共享資源的訪問,這就是分布式鎖要解決的問題。
應用場景
1、處理效率提升:應用分布式鎖,可以減少重復任務的執行,避免資源處理效率的浪費;
2、數據準確性保障:使用分布式鎖可以放在數據資源的并發訪問,避免數據不一致情況,甚至數據損失等。
例如:
分布式任務調度平臺保證任務的冪等性。
分布式全局id的生成
使用Redis 實現分布式鎖
思路:Redis實現分布式鎖基于SetNx命令,因為在Redis中key是保證是唯一的。所以當多個線程同時的創建setNx時,只要誰能夠創建成功誰就能夠獲取到鎖。
Set 命令: 每次 set 時,可以修改原來舊值;
SetNx命令:每次SetNx檢查該 key是否已經存在,如果已經存在的話不會執行任何操作。返回為0 如果已經不存在的話直接新增該key。
1:新增key成功, 0:失敗
獲取鎖的時候:當多個線程同時創建SetNx k,只要誰能夠創建成功誰就能夠獲取到鎖。
釋放鎖:可以對該key設置一個有效期可以避免死鎖的現象。
單機版Redis實現分布式鎖
使用原生Jedis實現
1、增加maven依賴
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2、編寫Jedis連接Redis工具類
public class RedisClientUtil {
//protected static Logger logger = Logger.getLogger(RedisUtil.class);
private static String IP = "www.kaicostudy.com";
//Redis的端口號
private static int PORT = 6379;
//可用連接實例的最大數目,默認值為8;
//如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。
private static int MAX_ACTIVE = 100;
//控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例,默認值也是8。
private static int MAX_IDLE = 20;
//等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException;
private static int MAX_WAIT = 3000;
private static int TIMEOUT = 3000;
//在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的;
private static boolean TEST_ON_BORROW = true;
//在return給pool時,是否提前進行validate操作;
private static boolean TEST_ON_RETURN = true;
private static JedisPool jedisPool = null;
/**
* redis過期時間,以秒為單位
*/
public final static int EXRP_HOUR = 60 * 60; //一小時
public final static int EXRP_DAY = 60 * 60 * 24; //一天
public final static int EXRP_MONTH = 60 * 60 * 24 * 30; //一個月
/**
* 初始化Redis連接池
*/
private static void initialPool() {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, IP, PORT, TIMEOUT, "123456");
} catch (Exception e) {
//logger.error("First create JedisPool error : "+e);
e.getMessage();
}
}
/**
* 在多線程環境同步初始化
*/
private static synchronized void poolInit() {
if (jedisPool == null) {
initialPool();
}
}
/**
* 同步獲取Jedis實例
*
* @return Jedis
*/
public synchronized static Jedis getJedis() {
if (jedisPool == null) {
poolInit();
}
Jedis jedis = null;
try {
if (jedisPool != null) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
e.getMessage();
// logger.error("Get jedis error : "+e);
}
return jedis;
}
/**
* 釋放jedis資源
*
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null && jedisPool != null) {
jedisPool.returnResource(jedis);
}
}
public static Long sadd(String key, String... members) {
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.sadd(key, members);
} catch (Exception e) {
//logger.error("sadd error : "+e);
e.getMessage();
}
return res;
}
}
3、編寫Redis鎖的工具類
public class RedisLock {
private static final int setnxSuccss = 1;
/**
* 獲取鎖
*
* @param lockKey 定義鎖的key
* @param notLockTimeOut 沒有獲取鎖的超時時間
* @param lockTimeOut 使用鎖的超時時間
* @return
*/
public String getLock(String lockKey, int notLockTimeOut, int lockTimeOut) {
// 獲取Redis連接
Jedis jedis = RedisClientUtil.getJedis();
// 定義沒有獲取鎖的超時時間
Long endTimeOut = System.currentTimeMillis() + notLockTimeOut;
while (System.currentTimeMillis() < endTimeOut) {
String lockValue = UUID.randomUUID().toString();
// 如果在多線程情況下誰能夠setnx 成功返回0 誰就獲取到鎖
if (jedis.setnx(lockKey, lockValue) == setnxSuccss) {
jedis.expire(lockKey, lockTimeOut / 1000);
return lockValue;
}
// 否則情況下 在超時時間內繼續循環
}
try {
if (jedis != null) {
jedis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 釋放鎖 其實就是將該key刪除
*
* @return
*/
public Boolean unLock(String lockKey, String lockValue) {
Jedis jedis = RedisClientUtil.getJedis();
// 確定是對應的鎖 ,才刪除
if (lockValue.equals(jedis.get(lockKey))) {
return jedis.del(lockKey) > 0 ? true : false;
}
return false;
}
}
4、測試方法
private RedisLock redisLock = new RedisLock();
private String lockKey = "kaico_lock";
/**
* 測試Jedis實現分布式鎖
* @return
*/
@GetMapping("/restLock1")
public String restLock1(){
// 1.獲取鎖
String lockValue = redisLock.getLock(lockKey, 5000, 5000);
if (StringUtils.isEmpty(lockValue)) {
System.out.println(Thread.currentThread().getName() + ",獲取鎖失敗!");
return "獲取鎖失敗";
}
// 2.獲取鎖成功執行業務邏輯
System.out.println(Thread.currentThread().getName() + ",獲取成功,lockValue:" + lockValue);
// 3.釋放lock鎖
redisLock.unLock(lockKey, lockValue);
return "";
}
使用Springboot實現
依賴于之前的項目
1、編寫鎖的工具類方法
@Component
public class SpringbootRedisLockUtil {
@Autowired
public RedisTemplate redisTemplate;
// 解鎖原子性操作腳本
public static final String unlockScript="if redis.call(\"get\",KEYS[1]) == ARGV[1]\n"
+ "then\n"
+ " return redis.call(\"del\",KEYS[1])\n"
+ "else\n"
+ " return 0\n"
+ "end";
/**
* 加鎖,有阻塞
* @param name
* @param expire
* @param timeout
* @return
*/
public String lock(String name, long expire, long timeout) throws UnsupportedEncodingException {
long startTime=System.currentTimeMillis();
String token;
do{
token=tryLock(name,expire);
if(token==null){
//設置等待時間,若等待時間過長則獲取鎖失敗
if((System.currentTimeMillis()-startTime)>(timeout-50)){
break;
}
try {
Thread.sleep(50);//try it again per 50
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}while (token==null);
return token;
}
/**
* 解鎖
* @param name
* @param token
* @return
*/
public Boolean unlock(String name, String token) throws UnsupportedEncodingException {
byte[][] keyArgs=new byte[2][];
keyArgs[0]= name.getBytes(Charset.forName("UTF-8"));
keyArgs[1]= token.getBytes(Charset.forName("UTF-8"));
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
RedisConnection connection = connectionFactory.getConnection();
try{
Long result = connection.scriptingCommands().eval(unlockScript.getBytes(Charset.forName("UTF-8")), ReturnType.INTEGER, 1, keyArgs);
if(result!=null&&result>0){
return true;
}
}finally {
RedisConnectionUtils.releaseConnection(connection,connectionFactory);
}
return false;
}
/**
* 加鎖,無阻塞
* @param name
* @param expire
* @return
*/
public String tryLock(String name, long expire) throws UnsupportedEncodingException {
String token= UUID.randomUUID().toString();
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
RedisConnection connection = connectionFactory.getConnection();
try{
Boolean result = connection.set(name.getBytes(Charset.forName("UTF-8")), token.getBytes(Charset.forName("UTF-8")),
Expiration.from(expire, TimeUnit.MILLISECONDS), RedisStringCommands.SetOption.SET_IF_ABSENT);
if(result!=null&&result){
return token;
}
}
finally {
RedisConnectionUtils.releaseConnection(connection,connectionFactory);
}
return null;
}
}
2、測試類
@Autowired
private SpringbootRedisLockUtil springbootRedisLockUtil;
@PostMapping("/restLock1")
public void restLock2() throws UnsupportedEncodingException {
String token;
token=springbootRedisLockUtil.lock(Thread.currentThread().getName(),1000,11000);
if(token!=null){
System.out.println("我拿到鎖了哦!");
}
else{
System.out.println("我沒有拿到鎖!");
}
springbootRedisLockUtil.unlock(Thread.currentThread().getName(),token);
}
原文鏈接:https://blog.csdn.net/weixin_44044929/article/details/125380445
相關推薦
- 2022-08-08 redis如何實現保存對象_Redis
- 2023-07-30 el-selete改變值后選中的內容不變
- 2021-11-08 Android如何實現時間線效果_Android
- 2023-01-20 基于?Dubbo?Admin?動態調整服務超時時間的操作步驟_其它綜合
- 2022-11-29 Mybatis傳入參數類型不匹配導致的報錯:
- 2022-04-18 Python?變量類型實例詳解_python
- 2023-02-04 python協程之yield和yield?from實例詳解_python
- 2022-08-31 C語言數據的存儲專項分析_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支