集成redisson

## 集成redisson实现redis分布式锁 Redisson是Redis官方推荐的Java版的Redis客户端。它提供的功能非常多,也非常强大,此处我们只用它的分布式锁功能。 1、引入依赖 ```xml <!-- Redisson 锁功能 --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.16.2</version> </dependency> ``` 2、添加工具类RedisLock.java ```java package com.ruoyi.common.core.redis; 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 ruoyi */ @Component public class RedisLock { @Autowired private RedissonClient redissonClient; /** * 获取锁 * * @param lockKey 锁实例key * @return 锁信息 */ public RLock getRLock(String lockKey) { return redissonClient.getLock(lockKey); } /** * 加锁 * * @param lockKey 锁实例key * @return 锁信息 */ public RLock lock(String lockKey) { RLock lock = getRLock(lockKey); lock.lock(); return lock; } /** * 加锁 * * @param lockKey 锁实例key * @param leaseTime 上锁后自动释放锁时间 * @return true=成功;false=失败 */ public Boolean tryLock(String lockKey, long leaseTime) { return tryLock(lockKey, 0, leaseTime, TimeUnit.SECONDS); } /** * 加锁 * * @param lockKey 锁实例key * @param leaseTime 上锁后自动释放锁时间 * @param unit 时间颗粒度 * @return true=加锁成功;false=加锁失败 */ public Boolean tryLock(String lockKey, long leaseTime, TimeUnit unit) { return tryLock(lockKey, 0, leaseTime, unit); } /** * 加锁 * * @param lockKey 锁实例key * @param waitTime 最多等待时间 * @param leaseTime 上锁后自动释放锁时间 * @param unit 时间颗粒度 * @return true=加锁成功;false=加锁失败 */ public Boolean tryLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) { RLock rLock = getRLock(lockKey); boolean tryLock = false; try { tryLock = rLock.tryLock(waitTime, leaseTime, unit); } catch (InterruptedException e) { return false; } return tryLock; } /** * 释放锁 * * @param lockKey 锁实例key */ public void unlock(String lockKey) { RLock lock = getRLock(lockKey); lock.unlock(); } /** * 释放锁 * * @param lock 锁信息 */ public void unlock(RLock lock) { lock.unlock(); } } ``` 3、新增配置RedissonConfig.java ```java package com.ruoyi.framework.config; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * redisson配置 * * @author ruoyi */ @Configuration public class RedissonConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private String port; @Value("${spring.redis.password}") private String password; @Bean(destroyMethod = "shutdown") @ConditionalOnMissingBean(RedissonClient.class) public RedissonClient redissonClient() { Config config = new Config(); config.useSingleServer().setAddress("redis://" + host + ":" + port); // 更多.set return Redisson.create(config); } } ``` 4、使用方式 ```java @Autowired private RedisLock redisLock; // lockKey 锁实例key waitTime 最多等待时间 leaseTime 上锁后自动释放锁时间 unit 时间颗粒度 redisLock.lock(lockKey); redisLock.tryLock(lockKey, leaseTime); redisLock.tryLock(lockKey, leaseTime, unit); redisLock.tryLock(lockKey, waitTime, leaseTime, unit); redisLock.unlock(lockKey); redisLock.unlock(lock); ```