原文网址:lock4j--分布式锁中间件--使用/实例_IT利刃出鞘的博客-CSDN博客
简介
说明
本文用示例介绍分布式锁中间件lock4j 的用法。
相关网址
gitee:https://gitee.com/baomidou/lock4j
github:https://github.com/baomidou/lock4j(Star数:68)
lock4j的参考项目:https://gitee.com/kekingcn/spring-boot-klock-starter
友情提示
本人实际使用lock4j过程中遇到了很多问题,不推荐使用lock4j,推荐直接使用redisson的分布式锁。
遇到的问题有:
- 获取锁超时时并没有抛异常,而是一直等待获取到锁
概述
- lock4j与@Transactional类似:将注解写在方法上,自动控制上锁与释放锁。
- lock4j支持Redis(RedisTemplate或Redisson)、Zookeeper作为底层
- 2.0之后支持Redisson和Zookeeper
- 建议基于Redisson
- 原因:可以利用redisson的特性:自动续期等
- 方法:引入依赖:lock4j-redisson-spring-boot-starter
- 支持SPEL
- 执行顺序
- 如果在service上有@Transactional和@lock4j,则执行顺序如下
- 上锁
- 开启事务
- 执行逻辑
- 提交/回滚事务
- 释放锁
- 如果在service上有@Transactional和@lock4j,则执行顺序如下
- 实现原理
- AOP:Advisor + methodInterception
实例
本文以Redisson为底层使用lock4j。
依赖
-
<dependency>
-
<groupId>com.baomidou
</groupId>
-
<artifactId>lock4j-redisson-spring-boot-starter
</artifactId>
-
<version>2.2.2
</version>
-
</dependency>
整个pom.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0
</modelVersion>
-
<parent>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-parent
</artifactId>
-
<version>2.4.13
</version>
-
<relativePath/>
<!-- lookup parent from repository -->
-
</parent>
-
<groupId>com.knife.demo
</groupId>
-
<artifactId>demo_lock4j_SpringBoot
</artifactId>
-
<version>0.0.1-SNAPSHOT
</version>
-
<name>demo_lock4j_SpringBoot
</name>
-
<description>demo_lock4j_SpringBoot
</description>
-
<properties>
-
<java.version>1.8
</java.version>
-
</properties>
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
-
<!-- https://mvnrepository.com/artifact/com.baomidou/lock4j-redisson-spring-boot-starter -->
-
<dependency>
-
<groupId>com.baomidou
</groupId>
-
<artifactId>lock4j-redisson-spring-boot-starter
</artifactId>
-
<version>2.2.2
</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.projectlombok
</groupId>
-
<artifactId>lombok
</artifactId>
-
<optional>true
</optional>
-
</dependency>
-
-
<dependency>
-
<groupId>com.github.xiaoymin
</groupId>
-
<artifactId>knife4j-spring-boot-starter
</artifactId>
-
<version>3.0.3
</version>
-
</dependency>
-
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-maven-plugin
</artifactId>
-
<configuration>
-
<excludes>
-
<exclude>
-
<groupId>org.projectlombok
</groupId>
-
<artifactId>lombok
</artifactId>
-
</exclude>
-
</excludes>
-
</configuration>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
配置
application.yml
-
spring:
-
redis:
-
host: 192.168.5.193
-
port: 6379
-
password: 222333
-
-
#lock4j:
-
# acquire-timeout: 3000 #默认值3s,可不设置
-
# expire: 30000 #默认值30s,可不设置
-
# primary-executor: com.baomidou.lock.executor.RedissonLockExecutor #默认redisson > redisTemplate > zookeeper,可不设置
-
# lock-key-prefix: lock4j #锁key前缀, 默认值lock4j,可不设置
代码
Controller
本处直接将@Lock4j写到Controller方法上,实际开发中应该写到Service的实现类的方法上。
-
package com.knife.demo.controller;
-
-
import com.baomidou.lock.annotation.Lock4j;
-
import com.knife.demo.entity.User;
-
import io.swagger.annotations.Api;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
-
import java.util.List;
-
-
@Api(tags = "用户")
-
@RestController
-
@RequestMapping("user")
-
public
class
UserController {
-
@GetMapping("listAll")
-
@Lock4j
-
public List<User>
listAllUser
() {
-
return
null;
-
}
-
-
@GetMapping("find")
-
@Lock4j(keys = {"#user.id", "#user.name"}, expire = 60000, acquireTimeout = 1000)
-
public List<User>
find
(User user) {
-
return
null;
-
}
-
}
Entity
-
package com.knife.demo.entity;
-
-
import lombok.Data;
-
-
@Data
-
public
class
User {
-
private Long id;
-
-
private String name;
-
}
测试
访问测试页面:http://localhost:8080/doc.html
测试1:无参数、无选项
运行前的Redis:(没有数据)
打断点
访问接口
进入断点
查看Redis信息(写入了数据)
放开断点,方法执行结束(Redis数据被删除)
测试2:有参数、有选项
运行前的Redis:(没有数据)
打断点
访问接口(id赋值为:123,name赋值为:Tony)
进入断点
查看Redis信息(写入了数据)
放开断点,方法执行结束(Redis数据被删除)
转载:https://blog.csdn.net/feiying0canglang/article/details/125321100
查看评论