SpringBoot集成Redisson实现分布式锁的方法示例

作者:Joyu_chen 时间:2021-10-30 16:59:38 

上篇 《SpringBoot 集成 redis 分布式锁优化》对死锁的问题进行了优化,今天介绍的是 redis 官方推荐使用的 Redisson ,Redisson 架设在 redis 基础上的 Java 驻内存数据网格(In-Memory Data Grid),基于NIO的 Netty 框架上,利用了 redis 键值数据库。功能非常强大,解决了很多分布式架构中的问题。

Github的wiki地址: https://github.com/redisson/redisson/wiki

官方文档: https://github.com/redisson/redisson/wiki/目录

项目代码结构图:

SpringBoot集成Redisson实现分布式锁的方法示例 

导入依赖


<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.8.0</version>
</dependency>

属性配置

application.properites 资源文件中添加单机&哨兵相关配置


server.port=3000

# redisson lock 单机模式
redisson.address=redis://127.0.0.1:6379
redisson.password=

#哨兵模式
#redisson.master-name= master
#redisson.password=
#redisson.sentinel-addresses=10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381

注意:

这里如果不加 redis:// 前缀会报 URI 构建错误


Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0

更多的配置信息可以去官网查看

定义Lock的接口定义类


package com.tuhu.thirdsample.service;

import org.redisson.api.RLock;
import java.util.concurrent.TimeUnit;
/**
* @author chendesheng
* @create 2019/10/12 10:48
*/
public interface DistributedLocker {

RLock lock(String lockKey);

RLock lock(String lockKey, int timeout);

RLock lock(String lockKey, TimeUnit unit, int timeout);

boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime);

void unlock(String lockKey);

void unlock(RLock lock);
}

Lock接口实现类


package com.tuhu.thirdsample.service;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

import java.util.concurrent.TimeUnit;

/**
* @author chendesheng
* @create 2019/10/12 10:49
*/
public class RedissonDistributedLocker implements DistributedLocker{

private RedissonClient redissonClient;

@Override
public RLock lock(String lockKey) {
 RLock lock = redissonClient.getLock(lockKey);
 lock.lock();
 return lock;
}

@Override
public RLock lock(String lockKey, int leaseTime) {
 RLock lock = redissonClient.getLock(lockKey);
 lock.lock(leaseTime, TimeUnit.SECONDS);
 return lock;
}

@Override
public RLock lock(String lockKey, TimeUnit unit ,int timeout) {
 RLock lock = redissonClient.getLock(lockKey);
 lock.lock(timeout, unit);
 return lock;
}

@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) {
  return false;
 }
}

@Override
public void unlock(String lockKey) {
 RLock lock = redissonClient.getLock(lockKey);
 lock.unlock();
}

@Override
public void unlock(RLock lock) {
 lock.unlock();
}

public void setRedissonClient(RedissonClient redissonClient) {
 this.redissonClient = redissonClient;
}
}

redisson属性装配类


package com.tuhu.thirdsample.common;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
* @author chendesheng
* @create 2019/10/11 20:04
*/
@Configuration
@ConfigurationProperties(prefix = "redisson")
@ConditionalOnProperty("redisson.password")
@Data
public class RedissonProperties {

private int timeout = 3000;

private String address;

private String password;

private int database = 0;

private int connectionPoolSize = 64;

private int connectionMinimumIdleSize=10;

private int slaveConnectionPoolSize = 250;

private int masterConnectionPoolSize = 250;

private String[] sentinelAddresses;

private String masterName;

}

SpringBoot自动装配类


package com.tuhu.thirdsample.configuration;

import com.tuhu.thirdsample.common.RedissonProperties;
import com.tuhu.thirdsample.service.DistributedLocker;
import com.tuhu.thirdsample.service.RedissonDistributedLocker;

import com.tuhu.thirdsample.util.RedissonLockUtil;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author chendesheng
* @create 2019/10/12 10:50
*/
@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {

@Autowired
private RedissonProperties redissonProperties;

/**
 * 哨兵模式自动装配
 * @return
 */
@Bean
@ConditionalOnProperty(name="redisson.master-name")
RedissonClient redissonSentinel() {
 Config config = new Config();
 SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redissonProperties.getSentinelAddresses())
   .setMasterName(redissonProperties.getMasterName())
   .setTimeout(redissonProperties.getTimeout())
   .setMasterConnectionPoolSize(redissonProperties.getMasterConnectionPoolSize())
   .setSlaveConnectionPoolSize(redissonProperties.getSlaveConnectionPoolSize());

if(StringUtils.isNotBlank(redissonProperties.getPassword())) {
  serverConfig.setPassword(redissonProperties.getPassword());
 }
 return Redisson.create(config);
}

/**
 * 单机模式自动装配
 * @return
 */
@Bean
@ConditionalOnProperty(name="redisson.address")
RedissonClient redissonSingle() {
 Config config = new Config();
 SingleServerConfig serverConfig = config.useSingleServer()
   .setAddress(redissonProperties.getAddress())
   .setTimeout(redissonProperties.getTimeout())
   .setConnectionPoolSize(redissonProperties.getConnectionPoolSize())
   .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize());

if(StringUtils.isNotBlank(redissonProperties.getPassword())) {
  serverConfig.setPassword(redissonProperties.getPassword());
 }

return Redisson.create(config);
}

/**
 * 装配locker类,并将实例注入到RedissLockUtil中
 * @return
 */
@Bean
DistributedLocker distributedLocker(RedissonClient redissonClient) {
 DistributedLocker locker = new RedissonDistributedLocker();
 ((RedissonDistributedLocker) locker).setRedissonClient(redissonClient);
 RedissonLockUtil.setLocker(locker);
 return locker;
}
}

Lock帮助类


package com.tuhu.thirdsample.util;

import com.tuhu.thirdsample.service.DistributedLocker;
import org.redisson.api.RLock;

import java.util.concurrent.TimeUnit;

/**
* @author chendesheng
* @create 2019/10/12 10:54
*/
public class RedissonLockUtil {

private static DistributedLocker redissLock;

public static void setLocker(DistributedLocker locker) {
 redissLock = locker;
}

/**
 * 加锁
 * @param lockKey
 * @return
 */
public static RLock lock(String lockKey) {
 return redissLock.lock(lockKey);
}

/**
 * 释放锁
 * @param lockKey
 */
public static void unlock(String lockKey) {
 redissLock.unlock(lockKey);
}

/**
 * 释放锁
 * @param lock
 */
public static void unlock(RLock lock) {
 redissLock.unlock(lock);
}

/**
 * 带超时的锁
 * @param lockKey
 * @param timeout 超时时间 单位:秒
 */
public static RLock lock(String lockKey, int timeout) {
 return redissLock.lock(lockKey, timeout);
}

/**
 * 带超时的锁
 * @param lockKey
 * @param unit 时间单位
 * @param timeout 超时时间
 */
public static RLock lock(String lockKey, TimeUnit unit , int timeout) {
 return redissLock.lock(lockKey, unit, timeout);
}

/**
 * 尝试获取锁
 * @param lockKey
 * @param waitTime 最多等待时间
 * @param leaseTime 上锁后自动释放锁时间
 * @return
 */
public static boolean tryLock(String lockKey, int waitTime, int leaseTime) {
 return redissLock.tryLock(lockKey, TimeUnit.SECONDS, waitTime, leaseTime);
}

/**
 * 尝试获取锁
 * @param lockKey
 * @param unit 时间单位
 * @param waitTime 最多等待时间
 * @param leaseTime 上锁后自动释放锁时间
 * @return
 */
public static boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
 return redissLock.tryLock(lockKey, unit, waitTime, leaseTime);
}
}

控制层


package com.tuhu.thirdsample.task;

import com.tuhu.thirdsample.common.KeyConst;
import lombok.extern.slf4j.Slf4j;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;
/**
* @author chendesheng
* @create 2019/10/12 11:03
*/
@RestController
@RequestMapping("/lock")
@Slf4j
public class LockController {
@Autowired
RedissonClient redissonClient;

@GetMapping("/task")
public void task(){
 log.info("task start");
 RLock lock = redissonClient.getLock(KeyConst.REDIS_LOCK_KEY);
 boolean getLock = false;

try {
  if (getLock = lock.tryLock(0,5,TimeUnit.SECONDS)){
   //执行业务逻辑
   System.out.println("拿到锁干活");

}else {
   log.info("Redisson分布式锁没有获得锁:{},ThreadName:{}",KeyConst.REDIS_LOCK_KEY,Thread.currentThread().getName());
  }

} catch (InterruptedException e) {
  log.error("Redisson 获取分布式锁异常,异常信息:{}",e);
 }finally {

if (!getLock){
   return;
  }
  //如果演示的话需要注释该代码;实际应该放开
  //lock.unlock();
  //log.info("Redisson分布式锁释放锁:{},ThreadName :{}", KeyConst.REDIS_LOCK_KEY, Thread.currentThread().getName());
 }
}

}

RLock 继承自 java.util.concurrent.locks.Lock ,可以将其理解为一个重入锁,需要手动加锁和释放锁 。

来看它其中的一个方法:tryLock(long waitTime, long leaseTime, TimeUnit unit)


getLock = lock.tryLock(0,5,TimeUnit.SECONDS)

通过 tryLock() 的参数可以看出,在获取该锁时如果被其他线程先拿到锁就会进入等待,等待 waitTime 时间,如果还没用机会获取到锁就放弃,返回 false;若获得了锁,除非是调用 unlock 释放,那么会一直持有锁,直到超过 leaseTime 指定的时间。

来源:https://juejin.im/post/5da16f0ee51d4578331cbd2d

标签:SpringBoot,Redisson,分布式锁
0
投稿

猜你喜欢

  • GSON实现Java对象的JSON序列化与反序列化的实例教程

    2023-11-23 12:39:02
  • C#关闭指定名字进程的方法

    2021-11-23 23:59:42
  • SpringBoot 利用thymeleaf自定义错误页面

    2023-11-29 08:29:55
  • Android Notification 使用方法详解

    2021-07-14 14:42:01
  • 快速解决android webview https图片不显示的问题

    2021-09-23 22:38:52
  • Android Socket实现多个客户端聊天布局

    2022-10-09 08:31:19
  • mybatis项目CRUD步骤实例详解

    2023-04-21 13:12:00
  • SpringBoot使用Kaptcha实现验证码的生成与验证功能

    2022-11-30 15:57:44
  • Java实现对两个List快速去重并排序操作示例

    2023-05-02 03:40:14
  • C#中的委托和事件

    2021-09-14 01:01:55
  • 解析Java中如何获取Spring中配置的bean

    2023-07-20 13:35:26
  • JDK1.8中ArrayList是如何扩容的

    2023-01-20 19:04:47
  • 详解SpringMVC实现图片上传以及该注意的小细节

    2022-04-29 08:34:30
  • 详解WPF如何使用必应地图控件

    2021-07-29 19:01:27
  • Android 扫描WIFI权限详解

    2023-07-03 11:34:32
  • Qt for Android开发实例教程

    2023-06-27 10:00:39
  • java在网页上面抓取邮件地址的方法

    2023-10-01 19:18:21
  • Javassist之一秒理解java动态编程

    2023-11-09 09:36:33
  • springboottest测试依赖和使用方式

    2021-11-21 13:41:38
  • 详解jeefast和Mybatis实现二级联动的问题

    2022-11-10 05:10:15
  • asp之家 软件编程 m.aspxhome.com