分布式锁—6.Redisson的同步器组件

简介: Redisson提供了多种分布式同步工具,包括分布式锁、Semaphore和CountDownLatch。分布式锁包括可重入锁、公平锁、联锁、红锁和读写锁,适用于不同的并发控制场景。Semaphore允许多个线程同时获取锁,适用于资源池管理。CountDownLatch则用于线程间的同步,确保一组线程完成操作后再继续执行。Redisson通过Redis实现这些同步机制,提供了高可用性和高性能的分布式同步解决方案。源码剖析部分详细介绍了这些组件的初始化和操作流程,展示了Redisson如何利用Redis命令和

大纲

1.Redisson的分布式锁简单总结

2.Redisson的Semaphore简介

3.Redisson的Semaphore源码剖析

4.Redisson的CountDownLatch简介

5.Redisson的CountDownLatch源码剖析

 

1.Redisson的分布式锁简单总结

(1)可重入锁RedissonLock

(2)公平锁RedissonFairLock

(3)联锁MultiLock

(4)红锁RedLock

(5)读写锁之读锁RedissonReadLock和写锁RedissonWriteLock

 

Redisson分布式锁包括:可重入锁、公平锁、联锁、红锁、读写锁。

 

(1)可重入锁RedissonLock

非公平锁,最基础的分布式锁,最常用的锁。

 

(2)公平锁RedissonFairLock

各个客户端尝试获取锁时会排队,按照队列的顺序先后获取锁。

 

(3)联锁MultiLock

可以一次性加多把锁,从而实现一次性锁多个资源。

 

(4)红锁RedLock

RedLock相当于一把锁。虽然利用了MultiLock包裹了多个小锁,但这些小锁并不对应多个资源,而是每个小锁的key对应一个Redis实例。只要大多数的Redis实例加锁成功,就可以认为RedLock加锁成功。RedLock的健壮性要比其他普通锁要好。

 

但是RedLock也有一些场景无法保证正确性,当然RedLock只要求部署主库。比如客户端A尝试向5个Master实例加锁,但仅仅在3个Maste中加锁成功。不幸的是此时3个Master中有1个Master突然宕机了,而且锁key还没同步到该宕机Master的Slave上,此时Salve切换为Master。于是在这5个Master中,由于其中有一个是新切换过来的Master,所以只有2个Master是有客户端A加锁的数据,另外3个Master是没有锁的。但继续不幸的是,此时客户端B来加锁,那么客户端B就很有可能成功在没有锁数据的3个Master上加到锁,从而满足了过半数加锁的要求,最后也完成了加锁,依然发生重复加锁。

 

(5)读写锁之读锁RedissonReadLock和写锁RedissonWriteLock

不同客户端线程的四种加锁情况:

情况一:先加读锁再加读锁,不互斥

情况二:先加读锁再加写锁,互斥

情况三:先加写锁再加读锁,互斥

情况四:先加写锁再加写锁,互斥

 

同一个客户端线程的四种加锁情况:

情况一:先加读锁再加读锁,不互斥

情况二:先加读锁再加写锁,互斥

情况三:先加写锁再加读锁,不互斥

情况四:先加写锁再加写锁,不互斥

 

2.Redisson的Semaphore简介

(1)Redisson的Semaphore原理图

Semaphore也是Redisson支持的一种同步组件。Semaphore作为一个锁机制,可以允许多个线程同时获取一把锁。任何一个线程释放锁之后,其他等待的线程就可以尝试继续获取锁。

(2)Redisson的Semaphore使用演示

public class RedissonDemo {
    public static void main(String[] args) throws Exception {
        //连接3主3从的Redis CLuster
        Config config = new Config();
        ...
        //Semaphore
        RedissonClient redisson = Redisson.create(config);
        final RSemaphore semaphore = redisson.getSemaphore("semaphore");
        semaphore.trySetPermits(3);
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        System.out.println(new Date() + ":线程[" + Thread.currentThread().getName() + "]尝试获取Semaphore锁");
                        semaphore.acquire();
                        System.out.println(new Date() + ":线程[" + Thread.currentThread().getName() + "]成功获取到了Semaphore锁,开始工作");
                        Thread.sleep(3000);
                        semaphore.release();
                        System.out.println(new Date() + ":线程[" + Thread.currentThread().getName() + "]释放Semaphore锁");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}

 

3.Redisson的Semaphore源码剖析

(1)Semaphore的初始化

(2)Semaphore设置允许获取的锁数量

(3)客户端尝试获取Semaphore的锁

(4)客户端释放Semaphore的锁

 

(1)Semaphore的初始化

public class Redisson implements RedissonClient {
    //Redis的连接管理器,封装了一个Config实例
    protected final ConnectionManager connectionManager;
    //Redis的命令执行器,封装了一个ConnectionManager实例
    protected final CommandAsyncExecutor commandExecutor;
    ...
    protected Redisson(Config config) {
        this.config = config;
        Config configCopy = new Config(config);
        //初始化Redis的连接管理器
        connectionManager = ConfigSupport.createConnectionManager(configCopy);
        ...  
        //初始化Redis的命令执行器
        commandExecutor = new CommandSyncService(connectionManager, objectBuilder);
        ...
    }
    @Override
    public RSemaphore getSemaphore(String name) {
        return new RedissonSemaphore(commandExecutor, name);
    }
    ...
}
public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
    private final SemaphorePubSub semaphorePubSub;
    final CommandAsyncExecutor commandExecutor;
    public RedissonSemaphore(CommandAsyncExecutor commandExecutor, String name) {
        super(commandExecutor, name);
        this.commandExecutor = commandExecutor;
        this.semaphorePubSub = commandExecutor.getConnectionManager().getSubscribeService().getSemaphorePubSub();
    }
    ...
}

(2)Semaphore设置允许获取的锁数量

public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
    ...
    @Override
    public boolean trySetPermits(int permits) {
        return get(trySetPermitsAsync(permits));
    }
    @Override
    public RFuture<Boolean> trySetPermitsAsync(int permits) {
        RFuture<Boolean> future = commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
            //执行命令"get semaphore",获取到当前的数值
            "local value = redis.call('get', KEYS[1]); " +
            "if (value == false) then " +
                //然后执行命令"set semaphore 3"
                //设置这个信号量允许客户端同时获取锁的总数量为3
                "redis.call('set', KEYS[1], ARGV[1]); " +
                "redis.call('publish', KEYS[2], ARGV[1]); " +
                "return 1;" +
            "end;" +
            "return 0;",
            Arrays.asList(getRawName(), getChannelName()),
            permits
        );
        if (log.isDebugEnabled()) {
            future.onComplete((r, e) -> {
                if (r) {
                    log.debug("permits set, permits: {}, name: {}", permits, getName());
                } else {
                    log.debug("unable to set permits, permits: {}, name: {}", permits, getName());
                }
            });
        }
        return future;
    }
    ...
}

首先执行命令"get semaphore",获取到当前的数值。然后执行命令"set semaphore 3",也就是设置这个信号量允许客户端同时获取锁的总数量为3。

 

(3)客户端尝试获取Semaphore的锁

public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
    ...
    private final SemaphorePubSub semaphorePubSub;
    final CommandAsyncExecutor commandExecutor;
    public RedissonSemaphore(CommandAsyncExecutor commandExecutor, String name) {
        super(commandExecutor, name);
        this.commandExecutor = commandExecutor;
        this.semaphorePubSub = commandExecutor.getConnectionManager().getSubscribeService().getSemaphorePubSub();
    }
    @Override
    public void acquire() throws InterruptedException {
        acquire(1);
    }
    
    @Override
    public void acquire(int permits) throws InterruptedException {
        if (tryAcquire(permits)) {
            return;
        }
        CompletableFuture<RedissonLockEntry> future = subscribe();
        commandExecutor.syncSubscriptionInterrupted(future);
        try {
            while (true) {
                if (tryAcquire(permits)) {
                    return;
                }
                //获取Redisson的Semaphore失败,于是便调用本地JDK的Semaphore的acquire()方法,此时当前线程会被阻塞
                //之后如果Redisson的Semaphore释放了锁,那么当前客户端便会通过监听订阅事件释放本地JDK的Semaphore,唤醒被阻塞的线程,继续执行while循环
                //注意:getLatch()返回的是JDK的Semaphore = "new Semaphore(0)" ==> (state - permits)
                //首先调用CommandAsyncService.getNow()方法
                //然后调用RedissonLockEntry.getLatch()方法
                //接着调用JDK的Semaphore的acquire()方法
                commandExecutor.getNow(future).getLatch().acquire();
            }
        } finally {
            unsubscribe(commandExecutor.getNow(future));
        }
    }
    
    @Override
    public boolean tryAcquire(int permits) {
        //异步转同步
        return get(tryAcquireAsync(permits));
    }
    
    @Override
    public RFuture<Boolean> tryAcquireAsync(int permits) {
        if (permits < 0) {
            throw new IllegalArgumentException("Permits amount can't be negative");
        }
        if (permits == 0) {
            return RedissonPromise.newSucceededFuture(true);
        }
        return commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
            //执行命令"get semaphore",获取到当前值
            "local value = redis.call('get', KEYS[1]); "+
            //如果semaphore的当前值不是false,且大于客户端线程申请获取锁的数量
            "if (value ~= false and tonumber(value) >= tonumber(ARGV[1])) then " +
                //执行"decrby semaphore 1",将信号量允许获取锁的总数量递减1
                "local val = redis.call('decrby', KEYS[1], ARGV[1]); " +
                "return 1; " +
            "end; " +
            //如果semaphore的值变为0,那么客户端就无法获取锁了,此时返回false
            "return 0;",
            Collections.<Object>singletonList(getRawName()),
            permits//ARGV[1]默认是1
        );
    }
    ...
}
public class CommandAsyncService implements CommandAsyncExecutor {
    ...
    @Override
    public <V> V getNow(CompletableFuture<V> future) {
        try {
            return future.getNow(null);
        } catch (Exception e) {
            return null;
        }
    }
    ...
}
public class RedissonLockEntry implements PubSubEntry<RedissonLockEntry> {
    private final Semaphore latch;
    ...
    public RedissonLockEntry(CompletableFuture<RedissonLockEntry> promise) {
        super();
        this.latch = new Semaphore(0);
        this.promise = promise;
    }
    
    public Semaphore getLatch() {
        return latch;
    }
    ...
}

执行命令"get semaphore",获取到semaphore的当前值。如果semaphore的当前值不是false,且大于客户端线程申请获取锁的数量。那么就执行"decrby semaphore 1",将信号量允许获取锁的总数量递减1。

 

如果semaphore的值变为0,那么客户端就无法获取锁了,此时tryAcquire()方法返回false。表示获取semaphore的锁失败了,于是当前客户端线程便会通过本地JDK的Semaphore进行阻塞。

 

当客户端后续收到一个订阅事件把本地JDK的Semaphore进行释放后,便会唤醒阻塞线程继续while循环。在while循环中,会不断尝试获取这个semaphore的锁,如此循环往复,直到成功获取。

 

(4)客户端释放Semaphore的锁

public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
    ...
    @Override
    public void release() {
        release(1);
    }
    @Override
    public void release(int permits) {
        get(releaseAsync(permits));
    }
    @Override
    public RFuture<Void> releaseAsync(int permits) {
        if (permits < 0) {
            throw new IllegalArgumentException("Permits amount can't be negative");
        }
        if (permits == 0) {
            return RedissonPromise.newSucceededFuture(null);
        }
        RFuture<Void> future = commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_VOID,
            //执行命令"incrby semaphore 1"
            "local value = redis.call('incrby', KEYS[1], ARGV[1]); " +
            "redis.call('publish', KEYS[2], value); ",
            Arrays.asList(getRawName(), getChannelName()),
            permits
        );
        if (log.isDebugEnabled()) {
            future.onComplete((o, e) -> {
                if (e == null) {
                    log.debug("released, permits: {}, name: {}", permits, getName());
                }
            });
        }
        return future;
    }
    ...
}
//订阅semaphore不为0的事件,semaphore不为0时会触发执行这里的监听回调
public class SemaphorePubSub extends PublishSubscribe<RedissonLockEntry> {
    public SemaphorePubSub(PublishSubscribeService service) {
        super(service);
    }
    
    @Override
    protected RedissonLockEntry createEntry(CompletableFuture<RedissonLockEntry> newPromise) {
        return new RedissonLockEntry(newPromise);
    }
    
    @Override
    protected void onMessage(RedissonLockEntry value, Long message) {
    Runnable runnableToExecute = value.getListeners().poll();
        if (runnableToExecute != null) {
            runnableToExecute.run();
        }
        //将客户端本地JDK的Semaphore进行释放
        value.getLatch().release(Math.min(value.acquired(), message.intValue()));
    }
}
//订阅锁被释放的事件,锁被释放为0时会触发执行这里的监听回调
public class LockPubSub extends PublishSubscribe<RedissonLockEntry> {
    public static final Long UNLOCK_MESSAGE = 0L;
    public static final Long READ_UNLOCK_MESSAGE = 1L;
    
    public LockPubSub(PublishSubscribeService service) {
        super(service);
    }  
      
    @Override
    protected RedissonLockEntry createEntry(CompletableFuture<RedissonLockEntry> newPromise) {
        return new RedissonLockEntry(newPromise);
    }
    
    @Override
    protected void onMessage(RedissonLockEntry value, Long message) {
        if (message.equals(UNLOCK_MESSAGE)) {
            Runnable runnableToExecute = value.getListeners().poll();
            if (runnableToExecute != null) {
                runnableToExecute.run();
            }
            value.getLatch().release();
        } else if (message.equals(READ_UNLOCK_MESSAGE)) {
            while (true) {
                Runnable runnableToExecute = value.getListeners().poll();
                if (runnableToExecute == null) {
                    break;
                }
                runnableToExecute.run();
            }
            //将客户端本地JDK的Semaphore进行释放
            value.getLatch().release(value.getLatch().getQueueLength());
        }
    }
}

客户端释放Semaphore的锁时,会执行命令"incrby semaphore 1"。每当客户端释放掉permits个锁,就会将信号量的值累加permits,这样Semaphore信号量的值就不再是0了。然后通过publish命令发布一个事件,之后订阅了该事件的其他客户端都会对getLatch()返回的本地JDK的Semaphore进行加1。于是其他客户端正在被本地JDK的Semaphore进行阻塞的线程,就会被唤醒继续执行。此时其他客户端就可以尝试获取到这个信号量的锁,然后再次将这个Semaphore的值递减1。

 

4.Redisson的CountDownLatch简介

(1)Redisson的CountDownLatch原理图解

(2)Redisson的CountDownLatch使用演示

 

(1)Redisson的CountDownLatch原理图解

CountDownLatch的基本原理:要求必须有n个线程来进行countDown,才能让执行await的线程继续执行。如果没有达到指定数量的线程来countDown,会导致执行await的线程阻塞。

(2)Redisson的CountDownLatch使用演示

public class RedissonDemo {
    public static void main(String[] args) throws Exception {
        //连接3主3从的Redis CLuster
        Config config = new Config();
        ...
        //CountDownLatch
        final RedissonClient redisson = Redisson.create(config);
        RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
        //1.设置可以countDown的数量为3
        latch.trySetCount(3);
        System.out.println(new Date() + ":线程[" + Thread.currentThread().getName() + "]设置了必须有3个线程执行countDown,进入等待中。。。");
        for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        System.out.println(new Date() + ":线程[" + Thread.currentThread().getName() + "]在做一些操作,请耐心等待。。。。。。");
                        Thread.sleep(3000);
                        RCountDownLatch localLatch = redisson.getCountDownLatch("myCountDownLatch");
                        localLatch.countDown();
                        System.out.println(new Date() + ":线程[" + Thread.currentThread().getName() + "]执行countDown操作");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        latch.await();
        System.out.println(new Date() + ":线程[" + Thread.currentThread().getName() + "]收到通知,有3个线程都执行了countDown操作,可以继续往下执行");
    }
}

 

5.Redisson的CountDownLatch源码剖析

(1)CountDownLatch的初始化

(2)trySetCount()方法设置countDown的数量

(3)awati()方法进行阻塞等待

(4)countDown()方法对countDown的数量递减

 

(1)CountDownLatch的初始化

public class Redisson implements RedissonClient {
    //Redis的连接管理器,封装了一个Config实例
    protected final ConnectionManager connectionManager;
    //Redis的命令执行器,封装了一个ConnectionManager实例
    protected final CommandAsyncExecutor commandExecutor;
    ...
    protected Redisson(Config config) {
        this.config = config;
        Config configCopy = new Config(config);
        //初始化Redis的连接管理器
        connectionManager = ConfigSupport.createConnectionManager(configCopy);
        ...  
        //初始化Redis的命令执行器
        commandExecutor = new CommandSyncService(connectionManager, objectBuilder);
        ...
    }
    @Override
    public RCountDownLatch getCountDownLatch(String name) {
        return new RedissonCountDownLatch(commandExecutor, name);
    }
    ...
}
public class RedissonCountDownLatch extends RedissonObject implements RCountDownLatch {
    ...
    private final CountDownLatchPubSub pubSub;
    private final String id;
    protected RedissonCountDownLatch(CommandAsyncExecutor commandExecutor, String name) {
        super(commandExecutor, name);
        this.id = commandExecutor.getConnectionManager().getId();
        this.pubSub = commandExecutor.getConnectionManager().getSubscribeService().getCountDownLatchPubSub();
    }
    ...
}

(2)trySetCount()方法设置countDown的数量

trySetCount()方法的工作就是执行命令"set myCountDownLatch 3"。

public class RedissonCountDownLatch extends RedissonObject implements RCountDownLatch {
    ...
    @Override
    public boolean trySetCount(long count) {
        return get(trySetCountAsync(count));
    }
    @Override
    public RFuture<Boolean> trySetCountAsync(long count) {
        return commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
            "if redis.call('exists', KEYS[1]) == 0 then " +
                "redis.call('set', KEYS[1], ARGV[2]); " +
                "redis.call('publish', KEYS[2], ARGV[1]); " +
                "return 1 " +
            "else " +
                "return 0 " +
            "end",
            Arrays.asList(getRawName(), getChannelName()),
            CountDownLatchPubSub.NEW_COUNT_MESSAGE,
            count
        );
    }
    ...
}

(3)awati()方法进行阻塞等待

public class RedissonCountDownLatch extends RedissonObject implements RCountDownLatch {
    ...
    @Override
    public void await() throws InterruptedException {
        if (getCount() == 0) {
            return;
        }
        CompletableFuture<RedissonCountDownLatchEntry> future = subscribe();
        try {
            commandExecutor.syncSubscriptionInterrupted(future);
            while (getCount() > 0) {
                // waiting for open state
                //获取countDown的数量还大于0,就先阻塞线程,然后再等待唤醒,执行while循环
                //其中getLatch()返回的是JDK的semaphore = "new Semaphore(0)" ==> (state - permits)
                commandExecutor.getNow(future).getLatch().await();
            }
        } finally {
            unsubscribe(commandExecutor.getNow(future));
        }
    }
    @Override
    public long getCount() {
        return get(getCountAsync());
    }
    @Override
    public RFuture<Long> getCountAsync() {
        //执行命令"get myCountDownLatch"
        return commandExecutor.writeAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.GET_LONG, getRawName());
    }
    ...
}

在while循环中,首先会执行命令"get myCountDownLatch"去获取countDown值。如果该值不大于0,就退出循环不阻塞线程。如果该值大于0,则说明还没有指定数量的线程去执行countDown操作,于是就会先阻塞线程,然后再等待唤醒来继续循环。

 

(4)countDown()方法对countDown的数量递减

public class RedissonCountDownLatch extends RedissonObject implements RCountDownLatch {
    ...
    @Override
    public void countDown() {
        get(countDownAsync());
    }
    @Override
    public RFuture<Void> countDownAsync() {
        return commandExecutor.evalWriteNoRetryAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
            "local v = redis.call('decr', KEYS[1]);" +
            "if v <= 0 then redis.call('del', KEYS[1]) end;" +
            "if v == 0 then redis.call('publish', KEYS[2], ARGV[1]) end;",
            Arrays.<Object>asList(getRawName(), getChannelName()),
            CountDownLatchPubSub.ZERO_COUNT_MESSAGE
        );
    }
    ...
}

countDownAsync()方法会执行decr命令,将countDown的数量进行递减1。如果这个值已经小于等于0,就执行del命令删除掉该CoutDownLatch。如果是这个值为0,还会发布一条消息:

publish redisson_countdownlatch__channel__{anyCountDownLatch} 0

 

相关文章
|
1月前
|
NoSQL 调度 Redis
分布式锁—3.Redisson的公平锁
Redisson公平锁(RedissonFairLock)是一种基于Redis实现的分布式锁,确保多个线程按申请顺序获取锁,从而实现公平性。其核心机制是通过队列和有序集合管理线程的排队顺序。加锁时,线程会进入队列并等待,锁释放后,队列中的第一个线程优先获取锁。RedissonFairLock支持可重入加锁,即同一线程多次加锁不会阻塞。新旧版本在排队机制上有所不同,新版本在5分钟后才会重排队列,而旧版本在5秒后就会重排。释放锁时,Redisson会移除队列中等待超时的线程,并通知下一个排队的线程获取锁。通过这种机制,RedissonFairLock确保了锁的公平性和顺序性。
|
NoSQL 安全 调度
【📕分布式锁通关指南 10】源码剖析redisson之MultiLock的实现
Redisson 的 MultiLock 是一种分布式锁实现,支持对多个独立的 RLock 同时加锁或解锁。它通过“整锁整放”机制确保所有锁要么全部加锁成功,要么完全回滚,避免状态不一致。适用于跨多个 Redis 实例或节点的场景,如分布式任务调度。其核心逻辑基于遍历加锁列表,失败时自动释放已获取的锁,保证原子性。解锁时亦逐一操作,降低死锁风险。MultiLock 不依赖 Lua 脚本,而是封装多锁协调,满足高一致性需求的业务场景。
45 0
【📕分布式锁通关指南 10】源码剖析redisson之MultiLock的实现
|
1月前
|
NoSQL 调度 Redis
分布式锁—5.Redisson的读写锁
Redisson读写锁(RedissonReadWriteLock)是Redisson提供的一种分布式锁机制,支持读锁和写锁的互斥与并发控制。读锁允许多个线程同时获取,适用于读多写少的场景,而写锁则是独占锁,确保写操作的互斥性。Redisson通过Lua脚本实现锁的获取、释放和重入逻辑,并利用WatchDog机制自动续期锁的过期时间,防止锁因超时被误释放。 读锁的获取逻辑通过Lua脚本实现,支持读读不互斥,即多个线程可以同时获取读锁。写锁的获取逻辑则确保写写互斥和读写互斥,即同一时间只能有一个线程获取写锁,
157 17
|
2月前
|
负载均衡 NoSQL 算法
Redisson分布式锁数据一致性解决方案
通过以上的设计和实现, Redisson能够有效地解决分布式环境下数据一致性问题。但是, 任何技术都不可能万无一失, 在使用过程中还需要根据实际业务需求进行逻辑屏障的设计和错误处理机制的建立。
187 48
|
1月前
|
算法 NoSQL Redis
分布式锁—4.Redisson的联锁和红锁
Redisson的MultiLock和RedLock机制为分布式锁提供了强大的支持。MultiLock允许一次性锁定多个资源,确保在更新这些资源时不会被其他线程干扰。它通过将多个锁合并为一个大锁,统一进行加锁和释放操作。RedissonMultiLock的实现通过遍历所有锁并尝试加锁,若在超时时间内无法获取所有锁,则释放已获取的锁并重试。 RedLock算法则基于多个Redis节点的加锁机制,确保在大多数节点上加锁成功即可。RedissonRedLock通过重载MultiLock的failedLocksLi
|
1月前
|
监控 NoSQL Java
分布式锁—2.Redisson的可重入锁
本文主要介绍了Redisson可重入锁RedissonLock概述、可重入锁源码之创建RedissonClient实例、可重入锁源码之lua脚本加锁逻辑、可重入锁源码之WatchDog维持加锁逻辑、可重入锁源码之可重入加锁逻辑、可重入锁源码之锁的互斥阻塞逻辑、可重入锁源码之释放锁逻辑、可重入锁源码之获取锁超时与锁超时自动释放逻辑、可重入锁源码总结。
|
3月前
|
安全
【📕分布式锁通关指南 07】源码剖析redisson利用看门狗机制异步维持客户端锁
Redisson 的看门狗机制是解决分布式锁续期问题的核心功能。当通过 `lock()` 方法加锁且未指定租约时间时,默认启用 30 秒的看门狗超时时间。其原理是在获取锁后创建一个定时任务,每隔 1/3 超时时间(默认 10 秒)通过 Lua 脚本检查锁状态并延长过期时间。续期操作异步执行,确保业务线程不被阻塞,同时仅当前持有锁的线程可成功续期。锁释放时自动清理看门狗任务,避免资源浪费。学习源码后需注意:避免使用带超时参数的加锁方法、控制业务执行时间、及时释放锁以优化性能。相比手动循环续期,Redisson 的定时任务方式更高效且安全。
176 24
【📕分布式锁通关指南 07】源码剖析redisson利用看门狗机制异步维持客户端锁
|
2月前
|
存储 安全 NoSQL
【📕分布式锁通关指南 09】源码剖析redisson之公平锁的实现
本文深入解析了 Redisson 中公平锁的实现原理。公平锁通过确保线程按请求顺序获取锁,避免“插队”现象。在 Redisson 中,`RedissonFairLock` 类的核心逻辑包含加锁与解锁两部分:加锁时,线程先尝试直接获取锁,失败则将自身信息加入 ZSet 等待队列,只有队首线程才能获取锁;解锁时,验证持有者身份并减少重入计数,最终删除锁或通知等待线程。其“公平性”源于 Lua 脚本的原子性操作:线程按时间戳排队、仅队首可尝试加锁、实时发布锁释放通知。这些设计确保了分布式环境下的线程安全与有序执行。
96 0
【📕分布式锁通关指南 09】源码剖析redisson之公平锁的实现
|
3月前
【📕分布式锁通关指南 08】源码剖析redisson可重入锁之释放及阻塞与非阻塞获取
本文深入剖析了Redisson中可重入锁的释放锁Lua脚本实现及其获取锁的两种方式(阻塞与非阻塞)。释放锁流程包括前置检查、重入计数处理、锁删除及消息发布等步骤。非阻塞获取锁(tryLock)通过有限时间等待返回布尔值,适合需快速反馈的场景;阻塞获取锁(lock)则无限等待直至成功,适用于必须获取锁的场景。两者在等待策略、返回值和中断处理上存在显著差异。本文为理解分布式锁实现提供了详实参考。
162 11
【📕分布式锁通关指南 08】源码剖析redisson可重入锁之释放及阻塞与非阻塞获取
|
3月前
|
NoSQL Java Redis
redisson分布式锁
Redisson 分布式锁提供了一种简单高效的方式来实现分布式系统中的锁机制。通过本文介绍的基本用法和高级用法,开发者可以根据具体的业务需求选择合适的锁类型来确保系统的稳定性和高并发性。希望本文能帮助读者更好地理解和使用 Redisson 分布式锁,提高系统的并发处理能力和可靠性。
233 10

热门文章

最新文章