分布式锁优化之 使用lua脚本改造分布式锁保证判断和删除的原子性(优化之LUA脚本保证删除的原子性)

文章目录

1、lua脚本入门

Lua 教程 | 菜鸟教程:https://www.runoob.com/lua/lua-tutorial.html\\

redis提供了一组命令行直接可以执行lua脚本。

redis中的lua脚本是有原子性:因为是通过一个指令执行的

sh 复制代码
[root@localhost ~]# docker exec -it spzx-redis redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> eval script numkeys key [key ...] arg [arg ...]

script:lua 脚本字符串

numkeys:需要传递的key列表的元素数量

key:指定具体key列表,元素之间用空格区分,用KEYSIindex从1开始

arg:指定具体arg列表,元素之间用空格区分,用ARGVindex从1开始

sh 复制代码
127.0.0.1:6379> eval "return 'hello world!'" 0
"hello world!"

1.1、变量:弱类型

复制代码
全局变量:a = 5
局部变量:local a = 5   redis中的lua脚本只支持局部变量
sh 复制代码
127.0.0.1:6379> eval "local a=5 return a" 0
(integer) 5

1.2、流程控制

sh 复制代码
127.0.0.1:6379> eval "if 10>20 then return 10 else return 20 end" 0
(integer) 20

要修正你的脚本,你应该使用 KEYS 和 ARGV 而不是 keys 和 argv(注意大小写)。KEYS 数组包含了传递给脚本的键名(key names),而 ARGV 数组包含了传递给脚本的参数(arguments)。

sh 复制代码
127.0.0.1:6379> eval "if keys[1]>argv[1] then return keys[2] else return argv[2] end" 2 10 20 30 40
(error) ERR Error running script (call to f_6913b8a1459b5bd1de1628cd76db221048464f0a): @enable_strict_lua:15: user_script:1: Script attempted to access nonexistent global variable 'keys'
127.0.0.1:6379> eval "if KEYS[1]>ARGV[1] then return KEYS[2] else return ARGV[2] end" 2 10 20 30 40
"40"
sh 复制代码
127.0.0.1:6379> eval "return {10,20,30,40}" 0
1) (integer) 10
2) (integer) 20
3) (integer) 30
4) (integer) 40
sh 复制代码
127.0.0.1:6379> eval "return {KEYS[1],KEYS[3],ARGV[1],ARGV[3]}" 4 10 20 30 40 50 60 70 80 90 
1) "10"
2) "30"
3) "50"
4) "70"

1.3、在lua中执行redis指令

redis.call('指令名称',...)

sh 复制代码
127.0.0.1:6379> eval "return redis.call('set','lock','333')" 0
OK
127.0.0.1:6379> get lock
"333"
sh 复制代码
127.0.0.1:6379> eval "return redis.call('get','lock')" 0
"333"
sh 复制代码
127.0.0.1:6379> eval "return redis.call('set',KEYS[1],ARGV[1])" 1 lock 123
OK
127.0.0.1:6379> get lock
"123"

1.4、实战:先判断是否自己的锁,如果是才能删除

sh 复制代码
eval "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end"
sh 复制代码
eval "if redis.call('get',KEYS[1]) == ARGV[1] 
then 
	return redis.call('del',KEYS[1]) 
else 
	return 0 
end"
sh 复制代码
127.0.0.1:6379> eval "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end" 1 lock 4343323
(integer) 0
sh 复制代码
127.0.0.1:6379> eval "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end" 1 lock 123
(integer) 1

2、AlbumInfoApiController --》testLock()

java 复制代码
@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("测试分布式锁案例");
	}
	
}

3、AlbumInfoServiceImpl --》testLock()

java 复制代码
    @Override
    public  void testLock(){
        // 加锁:set k v nx ex 3
        String 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");
            //}
            String script = "if redis.call('get',KEYS[1]) == ARGV[1] " +
                    "then " +
                        "return redis.call('del',KEYS[1]) " +
                    "else " +
                        "return 0 " +
                    "end";
            this.redisTemplate.execute(new DefaultRedisScript<>(script, Boolean.class), Arrays.asList("lock"), uuid);
        }
    }

压力测试肯定也没有问题。自行测试

启动多个运行实例:

redis中的值重新改为0。

sh 复制代码
[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 requests


Server Software:        
Server Hostname:        192.168.74.1
Server Port:            8500

Document Path:          /api/album/albumInfo/test/lock
Document Length:        76 bytes

Concurrency Level:      100
Time taken for tests:   49.543 seconds
Complete requests:      5000
Failed requests:        674
   (Connect: 0, Receive: 0, Length: 674, Exceptions: 0)
Write errors:           0
Total transferred:      2353370 bytes
HTML transferred:       383370 bytes
Requests per second:    100.92 [#/sec] (mean)
Time per request:       990.856 [ms] (mean)
Time per request:       9.909 [ms] (mean, across all concurrent requests)
Transfer rate:          46.39 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   2.6      1      81
Processing:     5  948 2587.5     14   33884
Waiting:        5  948 2587.5     14   33883
Total:          6  950 2587.7     16   33887

Percentage of the requests served within a certain time (ms)
  50%     16
  66%    197
  75%    627
  80%   1054
  90%   2846
  95%   5011
  98%   8277
  99%  12678
 100%  33887 (longest request)
相关推荐
qq_4523962318 小时前
第十篇:分布式事务架构如何设计?从 Outbox 到 Saga 的生产级选型指南
分布式·架构·wpf
我不会起名字32219 小时前
etcd 与 Raft 工程边界:线性读、Watch 断连与空间膨胀的 4 个取舍
分布式·服务发现·etcd·raft·后端架构
凯云-金山HIL20 小时前
SimuRTS 的三类典型场景:快速原型、硬件在环、分布式协同
分布式·#配置项测试·半实物仿真测试
IT枫斗者21 小时前
MQ 选型对比:RocketMQ、RabbitMQ、Kafka 各自适合什么场景
开发语言·人工智能·分布式·kafka·rabbitmq·rocketmq
千里达景区票务系统21 小时前
分布式锁的几种实现方式对比:Redis、数据库、ZooKeeper 怎么选
数据库·redis·分布式·分布式锁
Pniubi1 天前
Kafka 零拷贝
分布式·kafka
有风听风有雨看雨2 天前
MinIO 四节点分布式集群部署文档
分布式·github
我不会起名字3222 天前
对象存储架构:直传签名、分片断点与防盗链的 4 个取舍
分布式·对象存储·oss·架构设计·s3
Pniubi2 天前
Kafka和Zookeeper、KRaft
分布式·zookeeper·kafka
Pniubi3 天前
Kafka在CDC增量迁移数据库中的作用
分布式·kafka