当前位置:网站首页>lock4j--分布式锁中间件--使用/实例
lock4j--分布式锁中间件--使用/实例
2022-06-28 23:03:00 【IT利刃出鞘】
原文网址:lock4j--分布式锁中间件--使用/实例_IT利刃出鞘的博客-CSDN博客
简介
说明
本文用示例介绍分布式锁中间件lock4j 的用法。
相关网址
gitee:https://gitee.com/baomidou/lock4j
lock4j的参考项目:https://gitee.com/kekingcn/spring-boot-klock-starter
概述
- 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数据被删除)

边栏推荐
- C语言-单词分析解析
- How to analyze the trend chart of London gold market with the moving average
- Mathematical knowledge: finding combinatorial number I - finding combinatorial number
- Zadig + cave Iast: let safety dissolve in continuous delivery
- This simple little function saves 213 hours for our production research team in half a year
- Is it safe to open a stock account by mobile phone?
- 数学知识:求组合数 I—求组合数
- YuMinHong set up two funds funded by his hometown
- Mysql通过ibd文件恢复数据的详细步骤
- [gateway development] handle the IP address segment represented by CIDR when NGX nested Lua
猜你喜欢

flowable 边界定时器

第四章 存储器管理练习

Online text filter less than specified length tool

Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary

超级工厂里的生意图鉴

【Word 教程系列第 2 篇】Word 中如何设置每页的表格都有表头

How to solve the problem of desktop without sound

Wave picking of WMS warehouse management system module

Websocket for im instant messaging development: concept, principle and common sense of mistakes

在线文本过滤小于指定长度工具
随机推荐
在DialogFragment的onStop(完全不可见)时调用dismiss退出界面报错解决办法
Is it safe and reliable to open a securities account in changtou school?
Is it safe and reliable for changtou school to help open a securities account? How to drive
Master the usage of const
手机办理股票开户安全性高吗?
CPU、GPU、TPU、NPU区别
Leetcode 324 swing sort ii[sort double pointer] the leetcode path of heroding
How to use London gold to draw support resistance line
C语言-单词分析解析
全面掌握const的用法《一》
How to analyze the trend chart of London gold market with the moving average
Encounter with avita 11: long lost freshness under strong product power
torch. nn. Transformer import failed
Serious internal entanglement in the we media industry: where should the enterprise we media go
在QT进行cin(全网最清晰教程)
第五章 虚拟存储器 练习
运维排查-使用hcache插件排查Buffer/cache占用过高
油猴脚本学习
Basic knowledge diagram of K-line Diagram -- meaning of single K-line
TDD案例实战