当前位置:网站首页>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 )

边栏推荐
- Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
- 网上注册股票开户很困难么?在线开户是安全么?
- Explanation: Luogu p1762 even number /6.21 internal examination T2
- 强大的开源API接口可视化管理平台-YApi
- Non scientific class! The road of self-study!
- Leetcode 324 Swing sort II [tri double pointeur] le chemin du leetcode pour l'héroding
- Code example of hiredis
- One card can sell tens of millions, and the business expansion is faster than that of players: you don't understand the Jianghu of star cards
- Linq连表查询
- On the necessity and solution of building a campus online teaching video convergence platform
猜你喜欢

机器学习6-决策树

CIN at QT (the clearest tutorial in the whole network)

Progress of dbnn experiment

第五章 虚拟存储器 练习

浅析搭建校园在线教学视频汇聚平台的必要性及解决方案

Go language - reflect

C interview questions_ 20220627 record

FANUC机器人_KAREL编程入门(2)_通用IO信号的使用方法

WMS仓库管理系统模块之波次拣货

Wechat red envelope cover making tutorial and use guide with link jump
随机推荐
The picture display on the left of the two column layout is determined by the content height on the right
网上注册股票开户很困难么?在线开户是安全么?
邂逅阿维塔 11:强产品力下久违的新鲜感
在DialogFragment的onStop(完全不可见)时调用dismiss退出界面报错解决办法
长投学堂帮忙开证券账户是安全靠谱的吗?个人如何开
笔记
Google Earth Engine(GEE)——利用sentinel-2数据进行农作物提取分析
超级工厂里的生意图鉴
C language - word analysis
[stm32 HAL库] RTC和BKP驱动
SqlServer复习
Mono's execution process
第五章 虚拟存储器 练习
【Flutter 問題系列第 71 篇】Flutter 中 Uint8List 和 Image 之間的相互轉換
Is it safe and reliable for changtou school to help open a securities account? How to drive
老家出资,俞敏洪设立两支基金
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
在长投学堂开通证券账户是安全可靠的吗?
Go language - reflect
在线文本过滤小于指定长度工具