您好,欢迎访问一九零五行业门户网

介绍redis分布式锁

推荐(免费):redis
redisson
redisson和下列一下自行封装两种方式的区别(场景):
redisson未获取到锁的会进入等待,直到获取到锁。另外两种方式如果未获取到锁,会放弃,不会执行业务代码。<dependency>    <groupid>org.redisson</groupid>    <artifactid>redisson-spring-boot-starter</artifactid>    <version>3.13.6</version></dependency>
@autowiredprivate redisson redisson;@getmapping(/redissonlock)public string redissonlock() {    log.info(进入了方法);    rlock lock = redisson.getlock(redissonlock);    try {        lock.lock(30, timeunit.seconds);        thread.sleep(10000);        system.out.println(我是你大哥);    } catch (interruptedexception e) {        e.printstacktrace();    } finally {    // 如果不释放,不会唤起其他线程,则其他线程会超时过期自动唤醒,不会执行业务代码        lock.unlock();    }    return 运行结束;}
redistemplate封装redis锁(1)
<dependency>   <groupid>org.springframework.boot</groupid>   <artifactid>spring-boot-starter-data-redis</artifactid></dependency>
package com.util;import org.springframework.dao.dataaccessexception;import org.springframework.data.redis.connection.redisconnection;import org.springframework.data.redis.connection.redisstringcommands;import org.springframework.data.redis.core.rediscallback;import org.springframework.data.redis.core.redistemplate;import org.springframework.data.redis.core.script.redisscript;import org.springframework.data.redis.core.types.expiration;import org.springframework.stereotype.component;import java.util.arrays;@componentpublic class redislock {    private static redistemplate redistemplate;    private static string script = if redis.call(\get\,keys[1]) == argv[1] then\n +            \treturn redis.call(\del\,keys[1])\n +            else\n +               \treturn 0\t\n +            end  ;    public redislock(redistemplate redistemplate) {        redislock.redistemplate = redistemplate;    }    public static boolean getlock(string key, string value, long expiretime) {        redisstringcommands.setoption setoption = redisstringcommands.setoption.ifabsent();        expiration expiration = expiration.seconds(expiretime);        rediscallback<boolean> booleanrediscallback = new rediscallback<boolean>() {            @override            public boolean doinredis(redisconnection connection) throws dataaccessexception {                return connection.set(redistemplate.getkeyserializer().serialize(key), redistemplate.getvalueserializer().serialize(value), expiration, setoption);            }        };        return (boolean) redistemplate.execute(booleanrediscallback);    }    public static boolean unlock(string key, string value) {        redisscript<boolean> redisscript = redisscript.of(script, boolean.class);        return (boolean) redistemplate.execute(redisscript, arrays.aslist(key), value);    }}
@getmapping(/redislock)public string redislock() {    log.info(进入了方法);    string key = redislock;    string uuid = uuid.randomuuid().tostring();    try {        if (redislock.getlock(key, uuid, 30l)) {            log.info(进入了锁);            thread.sleep(10000);        }    } catch (interruptedexception e) {        e.printstacktrace();    } finally {        redislock.unlock(key, uuid);    }    log.info(方法执行完成);    return 程序结束;}
redistemplate封装redis锁(2)
package com.util;import lombok.extern.slf4j.slf4j;import org.springframework.dao.dataaccessexception;import org.springframework.data.redis.connection.redisconnection;import org.springframework.data.redis.connection.redisstringcommands;import org.springframework.data.redis.core.rediscallback;import org.springframework.data.redis.core.redistemplate;import org.springframework.data.redis.core.script.redisscript;import org.springframework.data.redis.core.types.expiration;import java.util.arrays;import java.util.uuid;@slf4jpublic class highredislock implements autocloseable{    private redistemplate redistemplate;    private string key;    private string value;    private long expiretime;    private static string script = if redis.call(\get\,keys[1]) == argv[1] then\n +            \treturn redis.call(\del\,keys[1])\n +            else\n +               \treturn 0\t\n +            end  ;    public highredislock(redistemplate redistemplate, string key, long expiretime) {        this.redistemplate = redistemplate;        this.key = key;        this.value = uuid.randomuuid().tostring();        this.expiretime = expiretime;    }    public boolean getlock() {        redisstringcommands.setoption setoption = redisstringcommands.setoption.ifabsent();        expiration expiration = expiration.seconds(expiretime);        rediscallback<boolean> booleanrediscallback = new rediscallback<boolean>() {            @override            public boolean doinredis(redisconnection connection) throws dataaccessexception {                return connection.set(redistemplate.getkeyserializer().serialize(key), redistemplate.getvalueserializer().serialize(value), expiration, setoption);            }        };        return (boolean) redistemplate.execute(booleanrediscallback);    }    public boolean unlock() {        redisscript<boolean> redisscript = redisscript.of(script, boolean.class);        return (boolean) redistemplate.execute(redisscript, arrays.aslist(key), value);    }    @override    public void close() throws exception {        unlock();    }}
@autowiredprivate redistemplate redistemplate;@getmapping(/highredislock)public string highredislock() {    log.info(进入了方法);    try (highredislock redislock = new highredislock(redistemplate, highredislock, 30l)) {        if (redislock.getlock()) {            log.info(进入了锁);            thread.sleep(10000);        }    } catch (interruptedexception e) {        e.printstacktrace();    } catch (exception e) {        e.printstacktrace();    }    log.info(方法执行完成);    return 程序结束;}
以上就是介绍redis分布式锁的详细内容。
其它类似信息

推荐信息