欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > 分布式锁之 防误删(优化之UUID防误删)

分布式锁之 防误删(优化之UUID防误删)

2024/10/27 10:53:58 来源:https://blog.csdn.net/m0_65152767/article/details/142422473  浏览:    关键词:分布式锁之 防误删(优化之UUID防误删)

文章目录

  • 1、AlbumInfoApiController --》testLock()
  • 2、AlbumInfoServiceImpl --》testLock()
  • 3、问题:删除操作缺乏原子性。

在这里插入图片描述
实现如下:
在这里插入图片描述

1、AlbumInfoApiController --》testLock()

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AlbumInfoApiController {@GetMapping("test/lock")public Result testLock() {this.albumInfoService.testLock();return Result.ok("测试分布式锁案例");}}

2、AlbumInfoServiceImpl --》testLock()

    @Overridepublic  void testLock(){// 加锁:set k v nx ex 3String uuid = UUID.randomUUID().toString();Boolean lock = this.redisTemplate.opsForValue().setIfAbsent("lock", uuid, 3, TimeUnit.SECONDS);if (!lock) {try {// 获取锁失败,进行自旋Thread.sleep(50);this.testLock();} catch (InterruptedException e) {throw new RuntimeException(e);}}else {// 获取锁成功,执行业务//this.redisTemplate.expire("lock", 3, TimeUnit.SECONDS);Object numObj = this.redisTemplate.opsForValue().get("num");if (numObj == null) {this.redisTemplate.opsForValue().set("num", 1);return;}Integer num = Integer.parseInt(numObj.toString());this.redisTemplate.opsForValue().set("num", ++num);// 解锁// 先判断是否是自己的锁,如果是则删除if (StringUtils.equals(uuid, this.redisTemplate.opsForValue().get("lock").toString())) {this.redisTemplate.delete("lock");}}}

在这里插入图片描述
在这里插入图片描述
压力测试肯定也没有问题。自行测试
启动多个运行实例:
在这里插入图片描述
在这里插入图片描述
redis中的值重新改为0。

[root@localhost ~]# ab -n 5000 -c 100 http://192.168.74.1:8500/api/album/albumInfo/test/lock
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.74.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requestsServer Software:        
Server Hostname:        192.168.74.1
Server Port:            8500Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytesConcurrency Level:      100
Time taken for tests:   56.438 seconds
Complete requests:      5000
Failed requests:        663(Connect: 0, Receive: 0, Length: 663, Exceptions: 0)
Write errors:           0
Total transferred:      2353315 bytes
HTML transferred:       383315 bytes
Requests per second:    88.59 [#/sec] (mean)
Time per request:       1128.766 [ms] (mean)
Time per request:       11.288 [ms] (mean, across all concurrent requests)
Transfer rate:          40.72 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    2   2.5      1      23
Processing:     6 1078 3263.0     17   55303
Waiting:        6 1078 3263.0     17   55303
Total:          6 1079 3263.5     19   55319Percentage of the requests served within a certain time (ms)50%     1966%    31775%    79880%   121090%   277595%   487698%  1019199%  15899100%  55319 (longest request)

在这里插入图片描述

3、问题:删除操作缺乏原子性。

场景:

  1. service1执行删除时,查询到的lock值确实和uuid相等
  2. service1执行删除前,lock刚好过期时间已到,被redis自动释放
  3. service2获取了lock
  4. service1执行删除,此时会把service2的lock删除

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com