当前位置: 首页 > news >正文

分布式锁实现方案 - Lock4j 使用

一、Lock4j 分布式锁工具

你是不是在使用分布式锁的时候,还在自己用 AOP 封装框架?那么 Lock4j 你可以考虑一下。

Lock4j 是一个分布式锁组件,其提供了多种不同的支持以满足不同性能和环境的需求。

立志打造一个简单但富有内涵的分布式锁组件。

并且支持redission,redisTemplate,zookeeper。可混用,支持扩展。

Giee地址:https://gitee.com/baomidou/lock4j

二、使用方式

这里我以 redisson 作为分布式锁的底层。

添加依赖:

<dependency><groupId>com.baomidou</groupId><artifactId>lock4j-redisson-spring-boot-starter</artifactId><version>2.2.5</version>
</dependency>

然后再配置中增加 redis 的配置:

spring:redis:timeout: 6000password:cluster:max-redirects:nodes:- 192.168.40.120:6379- 192.168.40.121:6379- 192.168.40.122:6379

声明 RedissonClient

@Configuration
public class RedissonConfig {@Beanpublic RedissonClient getRedisson(RedisProperties redisProperties) {Config config = new Config();String[] nodes = redisProperties.getCluster().getNodes().stream().filter(StringUtils::isNotBlank).map(node -> "redis://" + node).collect(Collectors.toList()).toArray(new String[]{});ClusterServersConfig clusterServersConfig = config.useClusterServers().addNodeAddress(nodes);if (StringUtils.isNotBlank(redisProperties.getPassword())) {clusterServersConfig.setPassword(redisProperties.getPassword());}clusterServersConfig.setConnectTimeout((int) (redisProperties.getTimeout().getSeconds() * 1000));clusterServersConfig.setScanInterval(2000);return Redisson.create(config);}
}

然后只需在需要分布式锁的地方加 @Lock4j 即可:

@RestController
@RequestMapping("/lock")
public class Lock4jController {//不指定,默认获取锁超时3秒,30秒锁过期@Lock4j@GetMapping("/test")public String test() {return "success";}@Lock4j(keys = {"#id", "#name"}, expire = 60000, acquireTimeout = 10000)@GetMapping("/test1")public String test1(Long id, String name) {Thread.sleep(5000);return "success";}}

如果同时两次访问 /test1,可以感觉出第二次没有获得锁的请求等待的时间更长,因为要等待锁的释放:

在这里插入图片描述

获取锁超时时间和锁过期时间,可以通过配置在配置文件中全局生效:

lock4j:acquire-timeout: 3000 #默认值3s,可不设置expire: 30000 #默认值30s,可不设置primary-executor: com.baomidou.lock.executor.RedisTemplateLockExecutor #默认redisson>redisTemplate>zookeeper,可不设置lock-key-prefix: lock4j #锁key前缀, 默认值lock4j,可不设置

acquire-timeout 等待锁的时长,超过这个时间会默认抛出 com.baomidou.lock.exception.LockFailureException 异常。

在这里插入图片描述

也可以自定义异常捕获,需要实现 LockFailureStrategy 接口:

@Slf4j
@Component
public class MyLockFailureStrategy implements LockFailureStrategy {@Overridepublic void onLockFailure(String key, Method method, Object[] arguments) {log.error("key: {} , method: {} ,arguments: {} ", key, method.getName(), Arrays.asList(arguments).toString());}
}

如果获取锁超时,则可以看到打印的日志:

在这里插入图片描述

锁的获取逻辑也可以自定义,如果是 Redisson 依赖下,可以继承 AbstractLockExecutor<RLock> 抽象类,例如:

@Slf4j
@Component
public class MyLockExecutor extends AbstractLockExecutor<RLock> {@ResourceRedissonClient redissonClient;/*** 尝试获取锁*/@Overridepublic RLock acquire(String lockKey, String lockValue, long expire, long acquireTimeout) {log.info("key: {} 尝试获取锁", lockKey);try {RLock lockInstance = this.redissonClient.getLock(lockKey);boolean locked = lockInstance.tryLock(acquireTimeout, expire, TimeUnit.MILLISECONDS);return (RLock)this.obtainLockInstance(locked, lockInstance);} catch (InterruptedException var9) {return null;}}/*** 释放锁*/@Overridepublic boolean releaseLock(String key, String value, RLock lockInstance) {log.info("key: {} 释放锁", key);if (lockInstance.isHeldByCurrentThread()) {try {return (Boolean)lockInstance.forceUnlockAsync().get();} catch (InterruptedException | ExecutionException var5) {return false;}} else {return false;}}
}

然后在使用时指定执行器:

@Lock4j(keys = {"#id", "#name"}, expire = 60000, acquireTimeout = 1000, executor = MyLockExecutor.class)
@GetMapping("/test2")
public String test2(Long id, String name) throws InterruptedException {Thread.sleep(5000);return "success";
}

请求 test2 接口可以看到打印的日志:

在这里插入图片描述

Key 的生成也可以自定义,只需继承 DefaultLockKeyBuilder 抽象类,例如:

@Slf4j
@Component
public class MyLockKeyBuilder extends DefaultLockKeyBuilder {public MyLockKeyBuilder(BeanFactory beanFactory) {super(beanFactory);}@Overridepublic String buildKey(MethodInvocation invocation, String[] definitionKeys) {String key = super.buildKey(invocation, definitionKeys);log.info("生成的key:{} ", key);return key;}
}

运行后可以观察日志:

在这里插入图片描述

上面都是通过注解的方式,同样也可以手动控制锁的获取和释放,只需要引入 LockTemplate ,例如:

@RestController
@RequestMapping("/lock")
public class Lock4j2Controller {@Resourceprivate LockTemplate lockTemplate;@GetMapping("/test3")public String test1(Long id, String name) {// 获取锁final LockInfo lockInfo = lockTemplate.lock(id + name, 30000L, 5000L, RedissonLockExecutor.class);try {Thread.sleep(5000);return "success";} catch (InterruptedException e) {throw new RuntimeException(e);} finally {//释放锁lockTemplate.releaseLock(lockInfo);}}
}

三、通过锁实现限流

在注解中 autoRelease 控制着是否自动在方法执行结束后释放锁,如果为 false 则是在 expire 时间到的时候移除锁,实际是通过 Redis 的过期机制,通过这个机制可以限制某个 key 的访问频次,例如:

@Lock4j(keys = {"#id", "#name"}, expire = 3000, acquireTimeout = 10000, autoRelease = false)
@GetMapping("/test4")
public String test4(Long id, String name) throws InterruptedException {return "success";
}

当同一个 idname 第一次访问的时候速度会很快:

在这里插入图片描述

如果频繁访问则会被限流:

在这里插入图片描述

http://www.lryc.cn/news/257567.html

相关文章:

  • [虚拟机]使用VM打开虚拟机电脑重启解决方案。
  • Linux 详细介绍strace命令
  • 【知识分享】__RS485-嵌入式常用的通信协议
  • Qt生成动态链接库并使用动态链接库
  • E4990A 阻抗分析仪,20 Hz 至 10/20/30/50/120 MHz
  • k8s volumes and data
  • 万宾科技智能水环境综合治理监测系统效果
  • 掌控安全 暖冬杯 CTF Writeup By AheadSec
  • jQuery-操作DOM
  • 高级网工在Linux服务器抓包,少不了这几条常用的tcpdump命令。
  • Hough算法数学原理
  • 基于Debain安装 Docker 和 Docker Compose
  • gittee使用教学
  • q2-qt-多线程
  • 指针,函数指针,二级指针,指针传参,回调函数,指针步长
  • StringUtils.isEmpty()方法过期的替代方法
  • 智慧电力运维综合辅助监控系统
  • v-model和:model的区别
  • 网络攻击(二)--情报搜集阶段
  • oracle异常:ORA-03297:文件包含在请求的 RESIZE 值以外使用的数据
  • Redis 环境搭建
  • 什么是Helpdesk?对工程师有什么帮助?
  • flutter添加全局水印
  • Usergolang 一些优质关于sip协议包
  • MYSQL数据类型详解
  • 解决vue3 动态引入报错问题
  • Mysql dumpling 导入导出sql文件
  • 【数字经济】你必须知道的SABOE数字化转型
  • 【Python网络爬虫入门教程2】成为“Spider Man”的第二课:观察目标网站、代码编写
  • vue2和vue3中注意全局属性的区别(例如全局使用axios )