当前位置: 首页 > article >正文

基于 Redis + Lua 脚本实现分布式锁,确保操作的原子性

1.加锁的Lua脚本: lock.lua
--- -1 failed
--- 1 success

--- getLock key
local result = redis.call('setnx' , KEYS[1] , ARGV[1])
if result == 1 then
    --PEXPIRE:以毫秒的形式指定过期时间
    redis.call('pexpire' , KEYS[1] , 3600000)
else
    result = -1;
    -- 如果value相同,则认为是同一个线程的请求,则认为重入锁
    local value = redis.call('get' , KEYS[1])
    if (value == ARGV[1]) then
        result = 1;
        redis.call('pexpire' , KEYS[1] , 3600000)
    end
end
--  如果获取锁成功,则返回 1
return result
2.解锁的Lua脚本: unLock.lua
if redis.call('get', KEYS[1]) == ARGV[1]
    then
        return redis.call('del', KEYS[1])
    else
        return 0
end
3.将资源文件放在资源文件夹下

4.Java中调用lua脚本

1)获取文件方式

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @Author: best_liu
 * @Description:
 * @Date Create in 14:04 2023/10/26
 * @Modified By:
 */
@Slf4j
@RestController
public class LuaLock {


    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping(value = "/getLock")
    public Long getLock() {
        DefaultRedisScript<Long> script = new DefaultRedisScript<Long>();
        script.setResultType(Long.class);
//        获取lua文件方式
        script.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/lock.lua")));
        
        Long execute = (Long) redisTemplate.execute(script, Arrays.asList("best_liu"),"best_liu20231026150600");
        return execute;
    }
}

2)lua字符串方式

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @Author: best_liu
 * @Description:
 * @Date Create in 14:04 2023/10/26
 * @Modified By:
 */
@Slf4j
@RestController
public class LuaLock {


    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping(value = "/getLock")
    public Long getLock() {
        DefaultRedisScript<Long> script = new DefaultRedisScript<Long>();
        script.setResultType(Long.class);
//        获取lua文件方式
//        script.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/lock.lua")));
        String lua = "local result = redis.call('setnx' , KEYS[1] , ARGV[1])\n" +
                "if result == 1 then\n" +
                "    redis.call('pexpire' , KEYS[1] , 3600000)\n" +
                "else\n" +
                "    result = -1;\n" +
                "    local value = redis.call('get' , KEYS[1])\n" +
                "    if (value == ARGV[1]) then\n" +
                "        result = 1;\n" +
                "        redis.call('pexpire' , KEYS[1] , 3600000)\n" +
                "    end\n" +
                "end\n" +
                "return result";
        script.setScriptText(lua);
        Long execute = (Long) redisTemplate.execute(script, Arrays.asList("best_liu"),"best_liu20231026150600");
        return execute;
    }
}
5.jedis调用Lua脚本实现分布式重试锁

1)引入jedis依赖

<!-- jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

2)jedis调用lua



import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
public class LuaLock {


    @GetMapping(value = "/getLock")
    public Long getLock() {
        //获取连接
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        String lua = "local result = redis.call('setnx' , KEYS[1] , ARGV[1])\n" +
                "if result == 1 then\n" +
                "    redis.call('pexpire' , KEYS[1] , ARGV[2])\n" +
                "else\n" +
                "    result = -1;\n" +
                "    local value = redis.call('get' , KEYS[1])\n" +
                "    if (value == ARGV[1]) then\n" +
                "        result = 1;\n" +
                "        redis.call('pexpire' , KEYS[1] , ARGV[2])\n" +
                "    end\n" +
                "end\n" +
                "return result";
        List<String> keys = new ArrayList<>();
        List<String> values = new ArrayList<>();
        keys.add("best_liu");
        values.add("best_liu20231026150600");
        values.add("3600000");
        Object result = jedis.eval(lua, keys, values);
        System.out.println(result);
        return 1L;
    }
}


http://www.kler.cn/news/108219.html

相关文章:

  • Web APIs——事件流
  • 【CSDN 每日一练 ★★☆】【字符串】外观数列
  • golang连接池检查连接失败时如何重试
  • Linux网络编程01
  • npm更新包时This operation requires a one-time password.
  • 数学家陶哲轩在形式证明帮助下发现论文中错误
  • moviepy处理手机端图片旋转问题
  • JAVA同城服务智慧养老小程序怎么开发?
  • 企业微信接入芋道SpringBoot项目
  • sql--索引使用
  • sharepoint2016-2019升级到sharepoint订阅版
  • python DevOps
  • uniapp实现瀑布流
  • Android问题笔记四十二:signal 11 (SIGSEGV), code 1 (SEGV_MAPERR) 的解决方法
  • 一个Binder的前生今世 (二):Binder进程和线程的创建
  • 爬取抖音用户的个人基本信息
  • Latex报错 “Paragraph ended before \Gin@iii was complete“
  • 万字解析设计模式之工厂方法模式与简单工厂模式
  • 竞赛选题 深度学习图像修复算法 - opencv python 机器视觉
  • 4.5 final修饰符
  • hive使用中的参数优化与问题排查
  • Kafka KRaft模式探索
  • 【unity小技巧】unity排序问题的探究
  • 如何使用 Rask AI 进行视频本地化
  • 腾讯云和阿里云双11优惠大战,服务器价格相差1块钱?
  • laravel+vue2 element 一套项目级医院手术麻醉信息系统源码
  • python实现ModBusTCP协议的server
  • 【vtk学习笔记1】编译安装vtk9.2.6,运行官方例子
  • 如何在linux服务器上安装Anaconda与pytorch,以及pytorch卸载
  • TextureView和SurfaceView