Spring Boot 分布式锁的实现
作者 | 葫芦胡
来源 | urlify.cn/632yIv
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--redisson-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.10.6</version>
</dependency>
配置信息
spring:
# redis
redis:
host: 47.103.5.190
port: 6379
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 100
# 连接池中的最小空闲连接
max-idle: 10
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接超时时间(毫秒)
timeout: 5000
#默认是索引为0的数据库
database: 0
配置类
/**
* redisson 配置,下面是单节点配置:
*
* @author gourd
*/
@Configuration
publicclassRedissonConfig{
@Value("${spring.redis.host}")
privateString host;
@Value("${spring.redis.port}")
privateString port;
@Value("${spring.redis.password:}")
privateString password;
@Bean
publicRedissonClient redissonClient() {
Config config = newConfig();
//单节点
config.useSingleServer().setAddress("redis://"+ host + ":"+ port);
if(StringUtils.isEmpty(password)) {
config.useSingleServer().setPassword(null);
} else{
config.useSingleServer().setPassword(password);
}
//添加主从配置
// config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""});
// 集群模式配置 setScanInterval()扫描间隔时间,单位是毫秒, //可以用"rediss://"来启用SSL连接
// config.useClusterServers().setScanInterval(2000).addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001").addNodeAddress("redis://127.0.0.1:7002");
returnRedisson.create(config);
}
}
Redisson 工具类
/**
* redis分布式锁帮助类
*
* @author gourd
*
*/
publicclassRedisLockUtil{
privatestaticDistributedLocker distributedLocker = SpringContextHolder.getBean("distributedLocker",DistributedLocker.class);
/**
* 加锁
* @param lockKey
* @return
*/
publicstaticRLocklock(String lockKey) {
return distributedLocker.lock(lockKey);
}
/**
* 释放锁
* @param lockKey
*/
publicstaticvoid unlock(String lockKey) {
distributedLocker.unlock(lockKey);
}
/**
* 释放锁
* @param lock
*/
publicstaticvoid unlock(RLocklock) {
distributedLocker.unlock(lock);
}
/**
* 带超时的锁
* @param lockKey
* @param timeout 超时时间 单位:秒
*/
publicstaticRLocklock(String lockKey, int timeout) {
return distributedLocker.lock(lockKey, timeout);
}
/**
* 带超时的锁
* @param lockKey
* @param unit 时间单位
* @param timeout 超时时间
*/
publicstaticRLocklock(String lockKey, int timeout,TimeUnit unit ) {
return distributedLocker.lock(lockKey, unit, timeout);
}
/**
* 尝试获取锁
* @param lockKey
* @param waitTime 最多等待时间
* @param leaseTime 上锁后自动释放锁时间
* @return
*/
publicstaticboolean tryLock(String lockKey, int waitTime, int leaseTime) {
return distributedLocker.tryLock(lockKey, TimeUnit.SECONDS, waitTime, leaseTime);
}
/**
* 尝试获取锁
* @param lockKey
* @param unit 时间单位
* @param waitTime 最多等待时间
* @param leaseTime 上锁后自动释放锁时间
* @return
*/
publicstaticboolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
return distributedLocker.tryLock(lockKey, unit, waitTime, leaseTime);
}
/**
* 获取计数器
*
* @param name
* @return
*/
publicstaticRCountDownLatch getCountDownLatch(String name){
return distributedLocker.getCountDownLatch(name);
}
/**
* 获取信号量
*
* @param name
* @return
*/
publicstaticRSemaphore getSemaphore(String name){
return distributedLocker.getSemaphore(name);
}
}
底层封装
搜索顶级架构师公众号回复“架构整洁”,送你一份惊喜礼包。
/**
* @author gourd
*/
publicinterfaceDistributedLocker{
RLocklock(String lockKey);
RLocklock(String lockKey, int timeout);
RLocklock(String lockKey, TimeUnit unit, int timeout);
boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime);
void unlock(String lockKey);
void unlock(RLocklock);
}
/**
* @author gourd
*/
@Component
publicclassRedisDistributedLockerimplementsDistributedLocker{
@Autowired
privateRedissonClient redissonClient;
@Override
publicRLocklock(String lockKey) {
RLocklock= redissonClient.getLock(lockKey);
lock.lock();
returnlock;
}
@Override
publicRLocklock(String lockKey, int leaseTime) {
RLocklock= redissonClient.getLock(lockKey);
lock.lock(leaseTime, TimeUnit.SECONDS);
returnlock;
}
@Override
publicRLocklock(String lockKey, TimeUnit unit ,int timeout) {
RLocklock= redissonClient.getLock(lockKey);
lock.lock(timeout, unit);
returnlock;
}
@Override
publicboolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
RLocklock= redissonClient.getLock(lockKey);
try{
returnlock.tryLock(waitTime, leaseTime, unit);
} catch(InterruptedException e) {
returnfalse;
}
}
@Override
publicvoid unlock(String lockKey) {
RLocklock= redissonClient.getLock(lockKey);
lock.unlock();
}
@Override
publicvoid unlock(RLocklock) {
lock.unlock();
}
}
/**
* redis分布式锁控制器
* @author gourd
* @since 2019-07-30
*/
@RestController
@Api(tags = "redisson", description = "redis分布式锁控制器")
@RequestMapping("/redisson")
@Slf4j
publicclassRedissonLockController{
/**
* 锁测试共享变量
*/
privateInteger lockCount = 10;
/**
* 无锁测试共享变量
*/
privateInteger count = 10;
/**
* 模拟线程数
*/
privatestaticint threadNum = 10;
/**
* 模拟并发测试加锁和不加锁
* @return
*/
@GetMapping("/test")
@ApiOperation(value = "模拟并发测试加锁和不加锁")
publicvoidlock(){
// 计数器
finalCountDownLatch countDownLatch = newCountDownLatch(1);
for(int i = 0; i < threadNum; i ++) {
MyRunnable myRunnable = newMyRunnable(countDownLatch);
Thread myThread = newThread(myRunnable);
myThread.start();
}
// 释放所有线程
countDownLatch.countDown();
}
/**
* 加锁测试
*/
privatevoid testLockCount() {
String lockKey = "lock-test";
try{
// 加锁,设置超时时间2s
RedisLockUtil.lock(lockKey,2, TimeUnit.SECONDS);
lockCount--;
log.info("lockCount值:"+lockCount);
}catch(Exception e){
log.error(e.getMessage(),e);
}finally{
// 释放锁
RedisLockUtil.unlock(lockKey);
}
}
/**
* 无锁测试
*/
privatevoid testCount() {
count--;
log.info("count值:"+count);
}
publicclassMyRunnableimplementsRunnable{
/**
* 计数器
*/
finalCountDownLatch countDownLatch;
publicMyRunnable(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
publicvoid run() {
try{
// 阻塞当前线程,直到计时器的值为0
countDownLatch.await();
} catch(InterruptedException e) {
log.error(e.getMessage(),e);
}
// 无锁操作
testCount();
// 加锁操作
testLockCount();
}
}
}
调用接口后打印值:
「顶级架构师」建立了读者架构师交流群,大家可以添加小编微信进行加群
扫描添加好友邀你进架构师群,加我时注明【姓名+公司+职位】
版权申明:内容来源网络,版权归原作者所有。如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。
猜你还想看
JAVA 线上故障排查指南!
轻松两步,我在 SpringBoot 服务上实现了接口限流
Mybatis Plus 多租户架构(Multi-tenancy)实现
如何学会阅读源码,看这篇就够了!
长按识别图片二维码关注,订阅更多精彩
顶级架构师,企业架构、系统架构、网站架构、大规模分布式架构、高可用架构等架构讨论,以及结合互联网技术的架构调整。欢迎有想法、乐于分享的架构师交流学习