1、新增Redis分布式锁;
This commit is contained in:
parent
dc83ba0356
commit
9b37acb26f
8
pom.xml
8
pom.xml
|
|
@ -36,6 +36,7 @@
|
|||
<minio.version>8.0.3</minio.version>
|
||||
<poi.version>4.1.2</poi.version>
|
||||
<common-pool.version>2.6.2</common-pool.version>
|
||||
<redisson.version>3.14.1</redisson.version>
|
||||
</properties>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
|
|
@ -213,6 +214,13 @@
|
|||
<artifactId>ruoyi-api-system</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Redisson锁 -->
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
<version>3.14.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
|
|
|||
|
|
@ -22,7 +22,12 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- RuoYi Common Core-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
package com.ruoyi.common.redis.service;
|
||||
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* redis锁工具类
|
||||
*
|
||||
* @author Mask
|
||||
*/
|
||||
@Component
|
||||
public class RedisLock
|
||||
{
|
||||
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
/**
|
||||
* 获取锁对象,加锁前需要先获取此对象
|
||||
* @param lockKey
|
||||
* @return
|
||||
*/
|
||||
public RLock getLock(String lockKey)
|
||||
{
|
||||
return redissonClient.getLock(lockKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试加锁(续命锁)
|
||||
* @param rLock
|
||||
* @param waitTime 加锁等待时间, 超时会返回false
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public boolean tryLock(RLock rLock, long waitTime) throws InterruptedException
|
||||
{
|
||||
return rLock.tryLock(waitTime, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试加锁(自动释放锁)
|
||||
* @param rLock
|
||||
* @param waitTime 加锁等待时间, 超时会返回false
|
||||
* @param releaseTime 锁释放时间,上锁releaseTime秒后自动释放锁
|
||||
* @return
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public boolean tryAutoReleaseLock(RLock rLock, long waitTime, long releaseTime) throws InterruptedException
|
||||
{
|
||||
return rLock.tryLock(waitTime, releaseTime, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放锁
|
||||
*/
|
||||
public void unlock(RLock rLock)
|
||||
{
|
||||
rLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.ruoyi.common.redis.configure.RedisConfig,\
|
||||
com.ruoyi.common.redis.service.RedisService
|
||||
com.ruoyi.common.redis.service.RedisService,\
|
||||
com.ruoyi.common.redis.service.RedisLock
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue