Skip to content

v0.1.0 · Spring 注解式分布式锁(Redis)首个发布

Latest

Choose a tag to compare

@helloworldtang helloworldtang released this 29 Dec 11:54
· 4 commits to main since this release

Release Notes

  • 亮点

    • 单包兼容 Spring Boot 2.x/3.x(同时提供 spring.factories 与 AutoConfiguration.imports)
    • 锁 key 采用“union all”组合:@LockKeyParam 片段 + SpEL 片段(顺序:先注解、后 SpEL)
    • 自旋等待策略:FIXED / LINEAR / EXPONENTIAL,可按指定窗口等待获取锁
    • 安全释放:ThreadLocal 令牌 + Lua 原子校验删除,避免误解锁
    • 关键路径日志:获取/重试/释放/Key 构建(@slf4j),便于问题定位
  • 重要变更

    • groupId 统一为 io.github.helloworldtang(与 Central Portal 验证命名空间一致)
    • 新增线性自旋等待示例与集成测试
    • 增加核心组件的轻量日志打印
  • 依赖坐标

    • lock-key-param
<dependency>
  <groupId>io.github.helloworldtang</groupId>
  <artifactId>lock-key-param</artifactId>
  <version>0.1.0</version>
</dependency>
  • distributed-lock-redis-spring
<dependency>
  <groupId>io.github.helloworldtang</groupId>
  <artifactId>distributed-lock-redis-spring</artifactId>
  <version>0.1.0</version>
</dependency>
  • 快速开始
    • 添加依赖,配置 Redis(spring.data.redis.),无需任何 @enable,自动装配即生效
    • 在方法上使用 @lock;在参数/DTO 字段上标注 @LockKeyParam
@Lock(
  prefix = "dl", delimiter = ":", expireTime = 10, waitTime = 3, timeUnit = 
  TimeUnit.SECONDS,
  keys = {"#p0.userId", "#p1"},
  spinWaitTimeParam = @SpinWaitTimeParam(interval = 100, maxAttempts = 0, 
  strategy = SpinWaitStrategy.LINEAR)
)
public void place(@LockKeyParam("userId") OrderRequest req, @LockKeyParam Long 
orderId) { ... }
  • 行为说明

    • waitTime=0:快速失败;waitTime>0:在窗口内自旋等待(可配置间隔与策略)
    • 释放时校验令牌匹配后再删除,防止误删其它线程/请求的锁
  • 兼容性与示例

    • Boot 3.x / Java 17、Boot 2.7.x / Java 8 均已通过示例与集成测试
    • 示例端点:/orders/place(快速失败)、/orders/place-wait(等待窗口)、/orders/place-wait-linear(线性自旋)

Full Changelog: https://github.com/helloworldtang/spring-annotation-distributed-lock/commits/v0.1.0