当前位置:网站首页>一个注解替换synchronized关键字:分布式场景下实现方法加锁
一个注解替换synchronized关键字:分布式场景下实现方法加锁
2022-08-04 01:08:00 【[email protected】
一个注解替换synchronized关键字:分布式场景下实现方法加锁
一、前言
单机部署场景下,当我们需要锁住少量代码块或者方法,通常会使用synchronized关键字进行加锁,如下所示
public synchronized void test(){
}
public void test2(){
Object o = new Object();
synchronized (o){
}
}
但是,在分布式场景下,由于jvm之间无法通信,一个服务是无法感知另外一个服务对当前代码快加锁。
该demo实现了分布式场景下,使用一个注解,对方法进行加锁。单机也可以使用。
二 、实现原理
1.原理
利用Aspect的Around方法,对需要加锁的方法进行动态代理
在方法执行前获取Redisson锁对象,获取成功,方法执行。执行成功后释放锁
获取规则根据注解的相关参数进行设置。
用Redisson进行加锁
2.注解参数描述
- value锁名称
- waitTime等待超时:默认5秒,当waitTime<0,表示一直等待获取锁
- leaseTime过期时长:默认10秒
- timeUnit时长单位:默认单位秒
三、公平锁注解(@FairLock)
@FairLock实现了对接口fairLock加锁,知道该接口当前请求结束后,才可以再次被请求
@FairLock("fairLock")
@RequestMapping("fairLock")
public Date fairLock() throws InterruptedException {
Thread.sleep(3000);
return new Date();
}
四、读写锁注解(@ReadWriteLock)
@ReadWriteLock(value = “readWrite”,lockType = ReadWriteLockType.WRITELOCK)
如下图所示,用@ReadWriteLock对方法进行加读写锁,锁名称为readWrite,类型为读锁
import com.mabo.redis.easycode.readWriteLock.ReadWriteLock;
import com.mabo.redis.easycode.readWriteLock.ReadWriteLockType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
public class ReadWriteLockController {
private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ReadWriteLock(value = "readWrite",lockType = ReadWriteLockType.READLOCK)
@RequestMapping("read")
public String read() throws InterruptedException {
System.out.println("开始执行read");
Thread.sleep(100000);
System.out.println("read执行结束");
return sdf.format(new Date());
}
/** * value锁名称 * waitTime等待超时:默认5秒,当waitTime<0,表示一直等待获取锁 * leaseTime过期时长:默认10秒 * timeUnit时长单位:默认秒 */
@ReadWriteLock(value = "readWrite",lockType = ReadWriteLockType.WRITELOCK)
@RequestMapping("write")
public String write() throws InterruptedException {
Thread.sleep(3000);
return sdf.format(new Date());
}
}
五、下载地址
github下载地址: github
https://github.com/MaBo2420935619/EasycodeRedissonLock
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47053123/article/details/126138519
边栏推荐
- C语言 函数递归
- .NET Static Code Weaving - Rougamo Release 1.1.0
- 无代码7月热讯 | 微软首推数字联络中心平台;全球黑客马拉松...
- 七夕佳节即将来到,VR全景云游为你神助攻
- 2022 China Computing Power Conference released the excellent results of "Innovation Pioneer"
- KunlunBase 1.0 is released!
- typescript56-泛型接口
- Is there any jdbc link to Youxuan database documentation and examples?
- 网络带宽监控,带宽监控工具哪个好
- How to copy baby from Taobao (or Tmall store) through API interface to Pinduoduo interface code docking tutorial
猜你喜欢
【日志框架】
typescript50 - type specification between cross types and interfaces
ML18-自然语言处理
IDEA02:配置SQL Server2019数据库
typescript52 - simplify generic function calls
Getting started with MATLAB 3D drawing command plot3
Vant3—— 点击对应的name名称跳转到下一页对应的tab栏的name的位置
中原银行实时风控体系建设实践
[store mall project 01] environment preparation and testing
【虚拟户生态平台】虚拟化平台安装时遇到的坑
随机推荐
Slipper - virtual point, shortest path
typescript53-泛型约束
快速入门EasyX图形编程
VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
boot issue
【store商城项目01】环境准备以及测试
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
js中常用的几种遍历处理数据的方法梳理
【无标题】
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
typescript58-泛型类
Google Earth Engine - Calculates the effective width of rivers using publicly available river data
typescript54 - generic constraints
jmeter跨平台运行csv等文件
Google Earth Engine ——利用公开的河流数据计算河流的有效宽度
无代码7月热讯 | 微软首推数字联络中心平台;全球黑客马拉松...
typescript48 - type compatibility between functions
电子制造企业部署WMS仓储管理系统的好处是什么
MATLAB三维绘图命令plot3入门
pygame 中的transform模块