Redis学习笔记18:基于spring data redis及lua脚本的分布式锁

Redis分布式锁是一种在分布式系统中使用Redis实现的互斥锁。它可以确保在多个客户端同时访问共享资源时,只有一个客户端能够获取到锁,其它客户端需要等待或执行相应的逻辑。

一个对springboot redis框架进行重写,支持lettuce、jedis、连接池、同时连接多个集群、多个redis数据库、开发自定义属性配置的开源SDK

xml 复制代码
<dependency>
    <groupId>io.github.mingyang66</groupId>
    <artifactId>emily-spring-boot-redis</artifactId>
    <version>4.4.0</version>
</dependency>

GitHub地址:https://github.com/mingyang66/spring-parent

一、lua脚本SET指令实现的锁逻辑
lua 复制代码
-- 键值
local key = KEYS[1]
-- 值
local value = ARGV[1]
-- 过期时间
local expire = ARGV[2]
-- SET key value [NX | XX] [GET] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
-- NX 仅当key不存在时才设置key
-- XX 仅当key已经存在时才设置key
local success = redis.call('SET', key, value, 'NX', 'EX', expire)
if success then
    return true
else
    return false
end

上述脚本通过redis的SET指令及参数NX控制仅当key不存在时才设置key,通过参数EX控制过期时间单位是秒。

二、lua脚本实现解锁的逻辑
lua 复制代码
-- 键
local key = KEYS[1]
-- 删除已存在的键,不存在的 key 会被忽略
local success = redis.call('DEL', key)
if success then
    return true
else
    return false
end
三、spring data redis代码实现调用执行加锁lua脚本
java 复制代码
    /**
     * 尝试获取锁
     * 只有在key不存在的时候才可以加锁成功
     *
     * @param redisTemplate redis 模板工具类
     * @param key           键名
     * @param expire        过期时间
     * @return true-加锁成功 false-加锁失败
     */
    public static Boolean tryGetLock(RedisTemplate redisTemplate, String key, Duration expire) {
        try {
            if (StringUtils.isEmpty(LUA_SCRIPT_LOCK_GET)) {
                LUA_SCRIPT_LOCK_GET = getLuaScript("META-INF/scripts/lock_get.lua");
            }
            RedisScript<Boolean> script = RedisScript.of(LUA_SCRIPT_LOCK_GET, Boolean.class);
            return (Boolean) redisTemplate.execute(script, singletonList(key), "1", expire.getSeconds());
        } catch (Exception ex) {
            BaseLogger baseLogger = BaseLoggerBuilder.create()
                    .withSystemNumber(SystemNumberHelper.getSystemNumber())
                    .withTraceId(UUIDUtils.randomSimpleUUID())
                    .withClientIp(RequestUtils.getClientIp())
                    .withServerIp(RequestUtils.getServerIp())
                    .withTriggerTime(DateConvertUtils.format(LocalDateTime.now(), DatePatternInfo.YYYY_MM_DD_HH_MM_SS_SSS))
                    .withUrl("Redis")
                    .withRequestParams("key", key)
                    .withRequestParams("expire", expire.getSeconds())
                    .withBody(PrintExceptionInfo.printErrorInfo(ex.getCause()))
                    .build();
            logger.info(JsonUtils.toJSONString(baseLogger));
            return false;
        }
    }
四、spring data redis代码实现调用解锁lua脚本
java 复制代码
    /**
     * 释放指定的锁,如果锁不存在,则忽略
     *
     * @param redisTemplate redis 模板工具类
     * @param key           键名
     * @return true-加锁成功 false-加锁失败
     */
    public static Boolean releaseLock(RedisTemplate redisTemplate, String key) {
        try {
            if (StringUtils.isEmpty(LUA_SCRIPT_LOCK_DEL)) {
                LUA_SCRIPT_LOCK_DEL = getLuaScript("META-INF/scripts/lock_del.lua");
            }
            RedisScript<Boolean> script = RedisScript.of(LUA_SCRIPT_LOCK_DEL, Boolean.class);
            return (Boolean) redisTemplate.execute(script, singletonList(key));
        } catch (Exception ex) {
            BaseLogger baseLogger = BaseLoggerBuilder.create()
                    .withSystemNumber(SystemNumberHelper.getSystemNumber())
                    .withTraceId(UUIDUtils.randomSimpleUUID())
                    .withClientIp(RequestUtils.getClientIp())
                    .withServerIp(RequestUtils.getServerIp())
                    .withTriggerTime(DateConvertUtils.format(LocalDateTime.now(), DatePatternInfo.YYYY_MM_DD_HH_MM_SS_SSS))
                    .withUrl("Redis")
                    .withRequestParams("key", key)
                    .withBody(PrintExceptionInfo.printErrorInfo(ex.getCause()))
                    .build();
            logger.info(JsonUtils.toJSONString(baseLogger));
            return false;
        }
    }
五、加锁和解锁控制器
java 复制代码
    @GetMapping("tryGetLock")
    public boolean tryGetLock() {
        return LuaScriptTools.tryGetLock(redisTemplate, "mykey", Duration.ofSeconds(60));
    }

    @GetMapping("releaseLock")
    public boolean releaseLock() {
        return LuaScriptTools.releaseLock(redisTemplate, "mykey");
    }
相关推荐
E___V___E3 小时前
黑马点评redis改 part 5
数据库·redis·缓存
洛神灬殇3 小时前
【Redis技术进阶之路】「系统架构系列中篇」高可用之Master-Slave主从架构的复制问题(分析旧版点制功能)
redis·后端·架构
zizisuo8 小时前
6.2.Redis高阶实战
redis
洛卡卡了9 小时前
别人都在用 Redis Cluster 高可用了,你还在手动重启 Redis?😅
redis·后端
沸材9 小时前
Redis——通信协议
redis·缓存·resp通信协议
forestsea9 小时前
Redis核心技术知识点全集
数据库·redis·缓存
CopyLower9 小时前
设计与实现分布式锁:基于Redis的Java解决方案
java·redis·分布式
Narutolxy21 小时前
MySQL+Redis实战教程:从Docker安装部署到自动化备份与数据恢复20250418
redis·mysql·docker
sinat_2622921121 小时前
Java面试实战:谢飞机的求职记 - Spring Boot、Redis与微服务技术问答解析
java·spring boot·redis·微服务·分布式事务
盖世英雄酱581361 天前
同事说缓存都用redis啊,数据不会丢失!真的吗?
redis·后端·面试