当前位置:网站首页>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数据被删除)

边栏推荐
- Websocket for im instant messaging development: concept, principle and common sense of mistakes
- Google Earth engine (GEE) -- crop extraction and analysis using sentinel-2 data
- 手机办理股票开户安全性高吗?
- 带链接跳转的微信红包封面制作教程和使用指南
- torch. nn. Transformer import failed
- hiredis的代码示例
- C interview questions_ 20220627 record
- leetCode-栈类型详解
- With the development of industrial Internet as the starting point, the industry can enter a new stage of development
- 【剑指Offer】50. 第一个只出现一次的字符
猜你喜欢

两栏布局左边图片显示部分由右边内容高度决定

LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路

Qtcreater5.15.0 source code compilation process record

Hit the industry directly | the flying propeller launched the industry's first model selection tool

Business atlas in super factory

Websocket for im instant messaging development: concept, principle and common sense of mistakes
![LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路](/img/41/b8ba8d771b7224eac1cc8c54fe9d29.png)
LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路

CS5463代码模块解析(包含下载链接)

With a monthly salary of 60000 yuan, such people began to be robbed after the Internet "reduced costs and increased efficiency"

After crossing, she said that the multiverse really exists
随机推荐
Differences among CPU, GPU, TPU and NPU
Basic knowledge diagram of K-line Diagram -- meaning of single K-line
Realization of 2D code generation in micro build low code
Sample code of using redis to realize the like function
【Word 教程系列第 2 篇】Word 中如何设置每页的表格都有表头
微搭低代码中实现二维码生成
k线图基础知识图解——单根K线的含义
强大的开源API接口可视化管理平台-YApi
Flowable boundary timer
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
Qtcreater5.15.0 source code compilation process record
Summary of time series prediction series (code usage)
一文读懂,WMS仓储管理系统与ERP有什么区别
没找到实习,他总结了这些
数学知识:求组合数 I—求组合数
Jointly explore digital technology and information security, and the fourth China Russia Digital Forum was successfully held
Mysql通过ibd文件恢复数据的详细步骤
Prometeus 2.36.0 新特性
如何使用伦敦金画出支撑阻力线
一张能卖上千万,商家扩张比玩家还快:球星卡的江湖你不懂