(java 实现开箱即用基于 redis 的分布式锁
创始人
2024-03-27 18:22:13
0

项目简介

lock 为 java 设计的分布式锁,开箱即用,纵享丝滑。

开源地址:https://github.com/houbb/lock

目的

  • 开箱即用,支持注解式和过程式调用

  • 基于 redis 的分布式锁

  • 内置支持多种 redis 的整合方式

  • 渐进式设计,可独立于 spring 使用

  • 整合 spring

  • 整合 spring-boot

快速开始

需要

jdk1.7+

maven 3.x+

maven 引入

com.github.houbblock-core1.3.0

入门例子

基于本地 redis 的测试案例。

public void helloTest() {ILock lock = LockBs.newInstance();String key = "ddd";try {// 加锁lock.tryLock(key);System.out.println("业务处理");} catch (Exception e) {throw new RuntimeException(e);} finally {// 释放锁lock.unlock(key);}
}

配置化

为了便于拓展,LockBs 的配置支持自定义:

LockBs.newInstance().id(Ids.uuid32())   //id 生成策略.cache(JedisRedisServiceFactory.pooled("127.0.0.1", 6379)) //缓存策略.lockSupport(new RedisLockSupport())    // 锁实现策略.lockKeyFormat(new LockKeyFormat())     // 针对 key 的格式化处理策略.lockReleaseFailHandler(new LockReleaseFailHandler())   //释放锁失败处理;

整合 spring

maven 引入

com.github.houbblock-spring1.3.0

指定 bean 使用

启用分布式锁

@EnableLock 启用分布式锁。

@EnableRedisConfig 启用 redis 的默认配置。

@Configurable
@ComponentScan(basePackages = "com.github.houbb.lock.test.service")
@EnableLock
@EnableRedisConfig
public class SpringConfig {
}

EnableLock 注解说明,和引导类对应:

public @interface EnableLock {/*** 唯一标识生成策略* @return 结果*/String id() default "lockId";/*** 缓存实现策略 bean 名称** 默认引入 redis-config 中的配置** @return 实现*/String cache() default "springRedisService";/*** 加锁 key 格式化策略* @return 策略*/String lockKeyFormat() default "lockKeyFormat";/*** 锁释放失败处理类* @return 结果*/String lockReleaseFailHandler() default "lockReleaseFailHandler";}

其中 springRedisService 使用的是 redis-config 中的实现。

对应注解 @EnableRedisConfig,redis 的配置信息如下:

配置说明默认值
redis.addressredis 地址127.0.0.1
redis.portredis 端口6379
redis.passwordredis 密码

使用 LockBs

我们可以直接 LockBs 的引导类,这种适合一些更加灵活的场景。

@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceRawTest {@Autowiredprivate UserService userService;@Autowiredprivate LockBs lockBs;@Testpublic void queryLogTest() {final String key = "name";try {lockBs.tryLock(key);final String value = userService.rawUserName(1L);} catch (Exception exception) {throw new RuntimeException(exception);} finally {lockBs.unlock(key);}}}

aop 注解使用

指定方法注解

当然,我们可以在方法上直接指定注解 @Lock,使用更加方便。

直接使用,AOP 切面生效即可。

@Service
public class UserService {@Lockpublic String queryUserName(Long userId) {}@Lock(value = "#user.name")public void queryUserName2(User user) {}
}

@Lock 属性说明,value 用于指定 key,支持 SPEL 表达式。

其他属性,和引导类的方法参数一一对应。

public @interface Lock {/*** 缓存的 key 策略,支持 SpEL* @return 结果*/String value() default "";/*** 时间单位* @return 单位*/TimeUnit timeUnit() default TimeUnit.SECONDS;/*** 等待锁时间* @return 等待锁时间*/long waitLockTime() default 10;/*** 业务加锁时间* @return 加锁时间*/long lockTime() default 60;}

spring boot 整合

maven 引入

com.github.houbblock-springboot-starter1.3.0

使用

同 spring

后期 Road-MAP

  • 支持锁的可重入

持有锁的线程可以多次获取锁

  • 分布式锁注解支持

拓展阅读

Redis 分布式锁

java 从零实现 redis 分布式锁

缓存相关工具

cache: 手写渐进式 redis

common-cache: 通用缓存标准定义

redis-config: 兼容各种常见的 redis 配置模式

lock: 开箱即用的分布式锁

resubmit: 防重复提交

rate-limit: 限流

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...