佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 基于Redis實(shí)現(xiàn)分布式鎖的方法(lua腳本版)

基于Redis實(shí)現(xiàn)分布式鎖的方法(lua腳本版)

熱門標(biāo)簽:呼叫中心市場需求 服務(wù)外包 地方門戶網(wǎng)站 Linux服務(wù)器 鐵路電話系統(tǒng) AI電銷 百度競價(jià)排名 網(wǎng)站排名優(yōu)化

1、前言

在Java中,我們通過鎖來避免由于競爭而造成的數(shù)據(jù)不一致問題。通常我們使用synchronized 、Lock來實(shí)現(xiàn)。但是Java中的鎖只能保證在同一個(gè)JVM進(jìn)程內(nèi)中可用,在跨JVM進(jìn)程,例如分布式系統(tǒng)上則不可靠了。

2、分布式鎖

分布式鎖,是一種思想,它的實(shí)現(xiàn)方式有很多,如基于數(shù)據(jù)庫實(shí)現(xiàn)、基于緩存(Redis等)實(shí)現(xiàn)、基于Zookeeper實(shí)現(xiàn)等等。為了確保分布式鎖可用,我們至少要確保鎖的實(shí)現(xiàn)同時(shí)滿足以下四個(gè)條件

  • 互斥性:在任意時(shí)刻,只有一個(gè)客戶端能持有鎖。
  • 不會(huì)發(fā)生死鎖:即使客戶端在持有鎖的期間崩潰而沒有主動(dòng)解鎖,也能保證后續(xù)其他客戶端能加鎖。
  • 具有容錯(cuò)性:只要大部分的Redis節(jié)點(diǎn)正常運(yùn)行,客戶端就可以加鎖和解鎖。
  • 解鈴還須系鈴人:加鎖和解鎖必須是同一個(gè)客戶端,客戶端自己不能把別人加的鎖給解了。

 3、基于Redis實(shí)現(xiàn)分布式鎖

以下代碼實(shí)現(xiàn)了基于redis中間件的分布式鎖。加鎖的過程中為了保障setnx(設(shè)置KEY)和expire(設(shè)置超時(shí)時(shí)間)盡可能在一個(gè)事務(wù)中,使用到了lua腳本的方式,將需要完成的指令一并提交到redis中;

3.1、RedisConfig.java

package com.demo.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplateString, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplateString, Object> template = new RedisTemplate>();
        template.setConnectionFactory(factory);
        // key采用String的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        // value序列化方式采用jackson
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

}

3.2、RedisLockController.java

package com.demo.controller;

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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/redis")
public class RedisLockController {

    @Autowired
    private RedisTemplateString, Object> redisTemplate;

    @RequestMapping(value = "/lock/{key}/{uid}/{expire}")
    public Long lock(@PathVariable("key") String key, @PathVariable("uid") String uid, @PathVariable("expire") Integer expire) {
        Long result = null;
        try {
            //調(diào)用lua腳本并執(zhí)行
            DefaultRedisScriptLong> redisScript = new DefaultRedisScript>();
            redisScript.setResultType(Long.class);//返回類型是Long
            //lua文件存放在resources目錄下的redis文件夾內(nèi)
            redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("redis/redis_lock.lua")));
            result = redisTemplate.execute(redisScript, Arrays.asList(key), uid, expire);
            System.out.println("lock==" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "/unlock/{key}/{uid}")
    public Long unlock(@PathVariable("key") String key, @PathVariable("uid") String uid) {
        Long result = null;
        try {
            //調(diào)用lua腳本并執(zhí)行
            DefaultRedisScriptLong> redisScript = new DefaultRedisScript>();
            redisScript.setResultType(Long.class);//返回類型是Long
            //lua文件存放在resources目錄下的redis文件夾內(nèi)
            redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("redis/redis_unlock.lua")));
            result = redisTemplate.execute(redisScript, Arrays.asList(key), uid);
            System.out.println("unlock==" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}

3.3、redis_lock.lua

if redis.call('setnx',KEYS[1],ARGV[1]) == 1 then
    return redis.call('expire',KEYS[1],ARGV[2])
else
    return 0
end

3.4、redis_unlock.lua

if redis.call("exists",KEYS[1]) == 0 then
    return 1
end

if redis.call('get',KEYS[1]) == ARGV[1] then
    return redis.call('del',KEYS[1])
else
    return 0
end

4、測試效果

key123為key,thread12345為value標(biāo)識(shí)鎖的主人,300為該鎖的超時(shí)時(shí)間

加鎖:鎖主人為thread12345
http://127.0.0.1:8080/redis/lock/key123/thread12345/300

解鎖:解鎖人為thread123456
http://127.0.0.1:8080/redis/unlock/key123/thread123456

解鎖:解鎖人為thread12345
http://127.0.0.1:8080/redis/unlock/key123/thread12345

4.1、加鎖,其他人解鎖


thread12345加的鎖,thread123456是解不了的,只有等thread12345自己解鎖或者鎖的超時(shí)時(shí)間過期

4.2、加鎖,自己解鎖


thread12345加的鎖,thread12345自己隨時(shí)可以解鎖,也可以等鎖的超時(shí)時(shí)間過期

5、總結(jié)

  •  使用Redis鎖,會(huì)有業(yè)務(wù)未執(zhí)行完,鎖過期的問題,也就是鎖不具有可重入性的特點(diǎn)。
  • 使用Redis鎖,在嘗試獲取鎖的時(shí)候,是非阻塞的,不滿足在一定期限內(nèi)不斷嘗試獲取鎖的場景。
  • 以上兩點(diǎn),都可以采用Redisson鎖解決。

到此這篇關(guān)于基于Redis實(shí)現(xiàn)分布式鎖的方法(lua腳本版)的文章就介紹到這了,更多相關(guān)Redis實(shí)現(xiàn)分布式鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 詳解redis分布式鎖的這些坑
  • SpringBoot之使用Redis實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
  • 詳解Redis 分布式鎖遇到的序列化問題
  • 詳解RedisTemplate下Redis分布式鎖引發(fā)的系列問題
  • redisson分布式鎖的用法大全
  • php基于redis的分布式鎖實(shí)例詳解
  • Redis分布式鎖升級版RedLock及SpringBoot實(shí)現(xiàn)方法
  • 利用redis實(shí)現(xiàn)分布式鎖,快速解決高并發(fā)時(shí)的線程安全問題
  • 詳解基于redis實(shí)現(xiàn)分布式鎖

標(biāo)簽:湖南 黃山 湘潭 銅川 崇左 蘭州 仙桃 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于Redis實(shí)現(xiàn)分布式鎖的方法(lua腳本版)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    张北县| 北川| 德格县| 扎兰屯市| 郑州市| 庆云县| 纳雍县| 简阳市| 福建省| 南涧| 金堂县| 桐柏县| 万全县| 许昌市| 霸州市| 高阳县| 北碚区| 永兴县| 布拖县| 女性| 文登市| 扬中市| 邢台市| 科技| 内乡县| 浙江省| 玉龙| 旬阳县| 英超| 金昌市| 佛坪县| 岳阳市| 蕲春县| 邹平县| 阿图什市| 大厂| 三明市| 庆城县| 滦南县| 鱼台县| 环江|