Java實(shí)現(xiàn)開箱即用的redis分布式鎖
項(xiàng)目簡介
lock 為 java 設(shè)計(jì)的分布式鎖,開箱即用,縱享絲滑。
開源地址:https://github.com/houbb/lock
目的
- 開箱即用,支持注解式和過程式調(diào)用
- 基于 redis 的分布式鎖
- 內(nèi)置支持多種 redis 的整合方式
- 漸進(jìn)式設(shè)計(jì),可獨(dú)立于 spring 使用
- 整合 spring
- 整合 spring-boot
快速開始
需要
jdk1.7+
maven 3.x+
maven 引入
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>lock-core</artifactId>
<version>1.3.0</version>
</dependency>
入門例子
基于本地 redis 的測(cè)試案例。
public void helloTest() {
ILock lock = LockBs.newInstance();
String key = "ddd";
try {
// 加鎖
lock.tryLock(key);
System.out.println("業(yè)務(wù)處理");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 釋放鎖
lock.unlock(key);
}
}
配置化
為了便于拓展,LockBs 的配置支持自定義:
LockBs.newInstance()
.id(Ids.uuid32()) //id 生成策略
.cache(JedisRedisServiceFactory.pooled("127.0.0.1", 6379)) //緩存策略
.lockSupport(new RedisLockSupport()) // 鎖實(shí)現(xiàn)策略
.lockKeyFormat(new LockKeyFormat()) // 針對(duì) key 的格式化處理策略
.lockReleaseFailHandler(new LockReleaseFailHandler()) //釋放鎖失敗處理
;
整合 spring
maven 引入
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>lock-spring</artifactId>
<version>1.3.0</version>
</dependency>
指定 bean 使用
啟用分布式鎖
@EnableLock 啟用分布式鎖。
@EnableRedisConfig 啟用 redis 的默認(rèn)配置。
@Configurable
@ComponentScan(basePackages = "com.github.houbb.lock.test.service")
@EnableLock
@EnableRedisConfig
public class SpringConfig {
}
EnableLock 注解說明,和引導(dǎo)類對(duì)應(yīng):
public @interface EnableLock {
/**
* 唯一標(biāo)識(shí)生成策略
* @return 結(jié)果
*/
String id() default "lockId";
/**
* 緩存實(shí)現(xiàn)策略 bean 名稱
*
* 默認(rèn)引入 redis-config 中的配置
*
* @return 實(shí)現(xiàn)
*/
String cache() default "springRedisService";
/**
* 加鎖 key 格式化策略
* @return 策略
*/
String lockKeyFormat() default "lockKeyFormat";
/**
* 鎖釋放失敗處理類
* @return 結(jié)果
*/
String lockReleaseFailHandler() default "lockReleaseFailHandler";
}
其中 springRedisService 使用的是 redis-config 中的實(shí)現(xiàn)。
對(duì)應(yīng)注解 @EnableRedisConfig,redis 的配置信息如下:
| 配置 | 說明 | 默認(rèn)值 |
|---|---|---|
| redis.address | redis 地址 | 127.0.0.1 |
| redis.port | redis 端口 | 6379 |
| redis.password | redis 密碼 |
使用 LockBs
我們可以直接 LockBs 的引導(dǎo)類,這種適合一些更加靈活的場(chǎng)景。
@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceRawTest {
@Autowired
private UserService userService;
@Autowired
private LockBs lockBs;
@Test
public void queryLogTest() {
final String key = "name";
try {
lockBs.tryLock(key);
final String value = userService.rawUserName(1L);
} catch (Exception exception) {
throw new RuntimeException(exception);
} finally {
lockBs.unlock(key);
}
}
}
aop 注解使用
指定方法注解
當(dāng)然,我們可以在方法上直接指定注解 @Lock,使用更加方便。
直接使用,AOP 切面生效即可。
@Service
public class UserService {
@Lock
public String queryUserName(Long userId) {
}
@Lock(value = "#user.name")
public void queryUserName2(User user) {
}
}
@Lock 屬性說明,value 用于指定 key,支持 SPEL 表達(dá)式。
其他屬性,和引導(dǎo)類的方法參數(shù)一一對(duì)應(yīng)。
public @interface Lock {
/**
* 緩存的 key 策略,支持 SpEL
* @return 結(jié)果
*/
String value() default "";
/**
* 時(shí)間單位
* @return 單位
*/
TimeUnit timeUnit() default TimeUnit.SECONDS;
/**
* 等待鎖時(shí)間
* @return 等待鎖時(shí)間
*/
long waitLockTime() default 10;
/**
* 業(yè)務(wù)加鎖時(shí)間
* @return 加鎖時(shí)間
*/
long lockTime() default 60;
}
spring boot 整合
maven 引入
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>lock-springboot-starter</artifactId>
<version>1.3.0</version>
</dependency>
使用
同 spring
后期 Road-MAP
支持鎖的可重入
持有鎖的線程可以多次獲取鎖
分布式鎖注解支持
以上就是Java實(shí)現(xiàn)開箱即用的redis分布式鎖的詳細(xì)內(nèi)容,更多關(guān)于Java redis分布式鎖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot2.7.14整合redis7的詳細(xì)過程
這篇文章主要介紹了SpringBoot2.7.14整合redis7的詳細(xì)過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10
SpringBoot整合jnotify實(shí)現(xiàn)針對(duì)指定目錄及其(動(dòng)態(tài))子目錄的監(jiān)聽的方法
本文介紹了JNotify這一Java庫在SpringBoot中的應(yīng)用,JNotify允許應(yīng)用程序監(jiān)聽文件系統(tǒng)事件,包括文件夾/文件的創(chuàng)建、刪除、修改和重命名,由于JNotify底層調(diào)用的關(guān)鍵部分是C語言開發(fā)的,所以在使用前需要在系統(tǒng)中加入相應(yīng)的動(dòng)態(tài)庫2024-10-10
java實(shí)現(xiàn)圖片任意角度旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片任意角度旋轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Swagger3.0 整合spring boot2.7x避免swagger2.0與boot2.7沖突
這篇文章主要介紹了Swagger3.0 整合spring boot2.7x避免swagger2.0與boot2.7沖突問題,通過注釋掉2.0引入的倆包,直接引入3.0,文中結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10

