当前位置:网站首页>一个注解替换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
边栏推荐
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
- BGP实验(含MPLS)
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
- GNSS【0】- 专题
- C 学生管理系统_添加学生
- - heavy OpenCV 】 【 mapping
- Eight things to pay attention to in spot silver
- Use nodejs switch version (no need to uninstall and reinstall)
- FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
- 快速入门EasyX图形编程
猜你喜欢
随机推荐
typescript52-简化泛型函数调用
How to find the cause of Fiori Launchpad routing errors by single-step debugging
《The Google File System》新说
哎,又跟HR在小群吵了一架!
七夕佳节即将来到,VR全景云游为你神助攻
优秀的测试/开发程序员,是怎样修炼的?步步为营地去执行......
typescript56-泛型接口
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
TensoFlow学习记录(二):基础操作
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
typescript48-函数之间的类型兼容性
typescript50-交叉类型和接口之间的类型说明
.NET Static Code Weaving - Rougamo Release 1.1.0
Slipper - virtual point, shortest path
jmeter跨平台运行csv等文件
XSS - Bypass for loop filtering
GeoAO:一种快速的环境光遮蔽方案
GNSS文章汇总