赞
踩
redis分布式锁在该博客中,讲解了基于jedis的单价redis实现的分布式锁,如果redis是分布式部署的,该方法就没法使用了,本篇介绍的是基于redisson实现的分布式锁
首先我们要通过Maven引入Jedis
开源组件,在pom.xml
文件加入下面的代码:
- <dependency>
- <groupId>org.redisson</groupId>
- <artifactId>redisson</artifactId>
- <version>3.10.5</version>
- </dependency>
redisson配置,我们的redis是用的是集群版,使用的代理模式
redis架构图如下
redisson使用单节点配置(还有哨兵模式,多节点模式)
- /**
- * Redisson 配置
- * @return
- */
- @Bean
- public RedissonClient redissonClient() {
- Config config = new Config();
- String host = redisProperties.getHost();
- int port = redisProperties.getPort();
- SingleServerConfig serverConfig = config.useSingleServer().setAddress("redis://" + host + ":" + port)
- .setTimeout(redisProperties.getTimeout())
- .setDatabase(redisProperties.getDatabase())
- .setConnectionPoolSize(redisProperties.getMaxActive())
- .setConnectionMinimumIdleSize(redisProperties.getMinIdle());
- if (StringUtils.isNotEmpty(redisProperties.getPassword())) {
- serverConfig.setPassword(redisProperties.getPassword());
- }
- return Redisson.create(config);
- }
RedissonLocker的具体实现类
- public class RedissonLockerImpl implements RedissonLocker {
-
- @Autowired
- private RedissonClient redissonClient;
-
- /**************************可重入锁**************************/
-
- /**
- * 拿不到lock就不罢休,不然线程就一直block
- * 没有超时时间,默认30s
- *
- * @param lockKey
- * @return
- */
- @Override
- public RLock lock(String lockKey) {
- RLock lock = redissonClient.getLock(lockKey);
- lock.lock();
- return lock;
- }
-
- /**
- * 自己设置超时时间
- *
- * @param lockKey 锁的key
- * @param timeout 秒 如果是-1,直到自己解锁,否则不会自动解锁
- * @return
- */
- @Override
- public RLock lock(String lockKey, int timeout) {
- RLock lock = redissonClient.getLock(lockKey);
- lock.lock(timeout, TimeUnit.SECONDS);
- return lock;
- }
-
- /**
- * 自己设置超时时间
- *
- * @param lockKey 锁的key
- * @param unit 锁时间单位
- * @param timeout 超时时间
- *
- */
- @Override
- public RLock lock(String lockKey, TimeUnit unit, int timeout) {
- RLock lock = redissonClient.getLock(lockKey);
- lock.lock(timeout, unit);
- return lock;
- }
-
- /**
- * 尝试加锁,最多等待waitTime,上锁以后leaseTime自动解锁
- * @param lockKey 锁key
- * @param unit 锁时间单位
- * @param waitTime 等到最大时间,强制获取锁
- * @param leaseTime 锁失效时间
- * @return 如果获取成功,则返回true,如果获取失败(即锁已被其他线程获取),则返回false
- */
- @Override
- public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
- RLock lock = redissonClient.getLock(lockKey);
- try {
- return lock.tryLock(waitTime, leaseTime, unit);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return false;
- }
-
-
-
- /**************************公平锁**************************/
- /**
- * 尝试加锁,最多等待waitTime,上锁以后leaseTime自动解锁
- * @param lockKey 锁key
- * @param unit 锁时间单位
- * @param waitTime 等到最大时间,强制获取锁
- * @param leaseTime 锁失效时间
- * @return 如果获取成功,则返回true,如果获取失败(即锁已被其他线程获取),则返回false
- */
- public boolean fairLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
- RLock fairLock = redissonClient.getFairLock(lockKey);
- try {
- return fairLock.tryLock(waitTime, leaseTime, unit);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return false;
- }
-
- /**
- * 释放锁
- * @param lockKey 锁key
- */
- @Override
- public void unlock(String lockKey) {
- RLock lock = redissonClient.getLock(lockKey);
- lock.unlock();
- }
-
- /**
- * 释放锁
- */
- @Override
- public void unlock(RLock lock) {
- lock.unlock();
- }
- }
业务代码中使用
- String lockKey = userId;
- // 公平加锁,60秒后锁自动释放
- boolean isLocked = false;
- try {
- isLocked = redissonLocker.fairLock(lockKey , TimeUnit.SECONDS, 3, 60);
- if (isLocked) { // 如果成功获取到锁就继续执行
- // 执行业务代码操作
- return GlobalResponse.success();
- } else { // 未获取到锁
- return GlobalResponse.fail(500, "请勿重复点击!!");
- }
- } catch (Exception e) {
- return GlobalResponse.fail(500, e.getMessage());
- } finally {
- if (isLocked) { // 如果锁还存在,在方法执行完成后,释放锁
- redissonLocker.unlock(lockKey);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。