当前位置:网站首页>Lock4j -- distributed locking Middleware -- use / instance
Lock4j -- distributed locking Middleware -- use / instance
2022-06-28 23:10:00 【It blade out of sheath】
Original website :lock4j-- Distributed lock middleware -- Use / example _IT A blog with a sharp blade -CSDN Blog
brief introduction
explain
This article introduces the distributed locking middleware with examples lock4j Usage of .
Related website
gitee:https://gitee.com/baomidou/lock4j
lock4j Reference items for :https://gitee.com/kekingcn/spring-boot-klock-starter
summary
- lock4j And @Transactional similar : Write comments on Methods , Automatically control the lock and release lock .
- lock4j Support Redis(RedisTemplate or Redisson)、Zookeeper As the bottom layer
- 2.0 Later support Redisson and Zookeeper
- Recommendations based on Redisson
- reason : You can use redisson Characteristics of : Automatic renewal, etc
- Method : Introduce dependencies :lock4j-redisson-spring-boot-starter
- Support SPEL
- Execution order
- If in service There are @Transactional and @lock4j, The execution sequence is as follows
- locked
- Open transaction
- Perform logical
- Submit / Roll back the transaction
- Release the lock
- If in service There are @Transactional and @lock4j, The execution sequence is as follows
- Realization principle
- AOP:Advisor + methodInterception
example
This article takes Redisson Use... For the bottom layer lock4j.
rely on
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>lock4j-redisson-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>Whole 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>
To configure
application.yml
spring:
redis:
host: 192.168.5.193
port: 6379
password: 222333
#lock4j:
# acquire-timeout: 3000 # The default value is 3s, Not set up
# expire: 30000 # The default value is 30s, Not set up
# primary-executor: com.baomidou.lock.executor.RedissonLockExecutor # Default redisson > redisTemplate > zookeeper, Not set up
# lock-key-prefix: lock4j # lock key Prefix , The default value is lock4j, Not set up Code
Controller
This office will directly @Lock4j writes Controller On the way , It should be written in the actual development Service On the method of the implementation class .
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 = " user ")
@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;
}
test
Visit the test page :http://localhost:8080/doc.html

test 1: No parameter 、 No options
Pre operational Redis:( No data )

Breaking point

Access interface

Enter the breakpoint

see Redis Information ( Write data )

Let go of the breakpoint , Method execution ends (Redis Data is deleted )

test 2: With parameters 、 With options
Pre operational Redis:( No data )

Breaking point

Access interface (id The assignment is :123,name The assignment is :Tony)

Enter the breakpoint

see Redis Information ( Write data )

Let go of the breakpoint , Method execution ends (Redis Data is deleted )

边栏推荐
- WMS仓库管理系统模块之波次拣货
- Redis+aop+ user defined annotation to realize flow restriction
- 第四章 存储器管理练习
- 邂逅阿维塔 11:强产品力下久违的新鲜感
- Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
- Undefined symbol main (referred from entry9a.o).
- 一文搞懂shell脚本
- 【深度学习】(3) Transformer 中的 Encoder 机制,附Pytorch完整代码
- LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路
- Web API learning notes 1
猜你喜欢

Flowable boundary timer

How to analyze the trend chart of London gold market with the moving average

Counting sorting and stability of sorting

第四章 存储器管理练习

WEB API学习笔记1

在线文本过滤小于指定长度工具

The picture display on the left of the two column layout is determined by the content height on the right

Linq连表查询

leetCode-栈类型详解

邂逅阿维塔 11:强产品力下久违的新鲜感
随机推荐
Huawei cloud gaussdb (for redis) unveiling issue 19: inventory of six second level capabilities
[word Tutorial Series Part 1] how to remove arrows in word tables
LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路
计数排序和排序的稳定性
[stm32 HAL库] 串口通信
收藏 | VLOOKUP函数的这些妙用你都知道吗?
一文搞懂shell脚本
强大的开源API接口可视化管理平台-YApi
Powerful open source API interface visual management platform Yapi
数学知识:求组合数 I—求组合数
Small sample sharp weapon 2 Text confrontation + semi supervised FGSM & VAT & FGM code implementation
2022-06-28: what does the following golang code output? A:true; B:false; C:panic; D: Compilation failed. package main import “fm
Understand shell script in one article
Online linear programming: Dual convergence, new algorithms, and regret bounds
【深度学习】(2) Transformer 网络解析,代码复现,附Pytorch完整代码
What is the difference between WMS warehouse management system and ERP
Hit the industry directly | the flying propeller launched the industry's first model selection tool
国盛证券开户是真的安全可靠吗
O & M troubleshooting - use hcache plug-in to troubleshoot excessive buffer/cache occupancy
一文读懂,WMS仓储管理系统与ERP有什么区别