springboot redis分布式鎖代碼實(shí)例
這篇文章主要介紹了springboot redis分布式鎖代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
隨著微服務(wù)等分布式架構(gòu)的快速發(fā)展及應(yīng)用,在很多情況下,我們都會遇到在并發(fā)情況下多個(gè)線程競爭資源的情況,比如我們耳熟能詳?shù)拿霘⒒顒?,多平臺多用戶對同一個(gè)資源進(jìn)行操作等場景等。分布式鎖的實(shí)現(xiàn)方式有很多種,比如基于數(shù)據(jù)庫、Zookeeper、Redis等,本文我們主要介紹Spring Boot整合Redis實(shí)現(xiàn)分布式鎖。
工具類如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Protocol;
import redis.clients.util.SafeEncoder;
import java.io.Serializable;
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
public RedisTemplate getRedisTemplate() {
return this.redisTemplate;
}
/**
* 設(shè)置redis分布式鎖
* @param key
* @param value
* @param expire 鎖過期時(shí)間
* @return
*/
public boolean tryLock(final String key, final Serializable value, final long expire){
boolean isSuccess = (boolean) redisTemplate.execute((RedisCallback) connection -> {
RedisSerializer valueSerializer = redisTemplate.getValueSerializer();
RedisSerializer keySerializer = redisTemplate.getKeySerializer();
Object object = connection.execute("set",keySerializer.serialize(key),valueSerializer.serialize(value), SafeEncoder.encode("NX"),SafeEncoder.encode("EX"), Protocol.toByteArray(expire));
return null != object;
});
return isSuccess;
}
//釋放鎖
public boolean releaseLock(String key){
return redisTemplate.delete(key);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot整合Redis正確的實(shí)現(xiàn)分布式鎖的示例代碼
- SpringBoot使用Redis實(shí)現(xiàn)分布式鎖
- SpringBoot中使用redis做分布式鎖的方法
- SpringBoot之使用Redis實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
- 基于springboot實(shí)現(xiàn)redis分布式鎖的方法
- 關(guān)于SpringBoot 使用 Redis 分布式鎖解決并發(fā)問題
- SpringBoot集成redis實(shí)現(xiàn)分布式鎖的示例代碼
- SpringBoot利用注解來實(shí)現(xiàn)Redis分布式鎖
- Springboot中如何使用Redisson實(shí)現(xiàn)分布式鎖淺析
- 基于SpringBoot+Redis實(shí)現(xiàn)分布式鎖
相關(guān)文章
Spring源碼之事件監(jiān)聽機(jī)制(實(shí)現(xiàn)EventListener接口方式)
這篇文章主要介紹了Spring源碼之事件監(jiān)聽機(jī)制(實(shí)現(xiàn)EventListener接口方式),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
專屬于程序員的浪漫-Java輸出動態(tài)閃圖iloveyou
這篇文章主要介紹了專屬于程序員的浪漫-Java輸出動態(tài)閃圖iloveyou,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Struts2.5版本struts.xml與web.xml配置的更改方法
這篇文章主要給大家介紹了關(guān)于Struts2.5版本中struts.xm與web.xml配置的更改方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
StringBuffer與StringBuilder底層擴(kuò)容機(jī)制與常用方法
這篇文章主要給大家介紹了StringBuffer、StringBuilder底層擴(kuò)容機(jī)制與常用方法,有感興趣的小伙伴跟著小編一起來學(xué)習(xí)吧2023-07-07
SpringBoot中使用@Async實(shí)現(xiàn)異步任務(wù)調(diào)用詳解
這篇文章主要介紹了SpringBoot中使用@Async實(shí)現(xiàn)異步任務(wù)調(diào)用詳解,一個(gè)可以無需等待被調(diào)用函數(shù)的返回值就讓操作繼續(xù)進(jìn)行的方法(來自百度百科),即程序在順序執(zhí)行時(shí),不等待異步調(diào)用的語句返回結(jié)果就執(zhí)行后面的程序,需要的朋友可以參考下2023-12-12

