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

news2025/3/11 7:03:25

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

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的初始化

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原理图解

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的初始

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

文章转载自:东阳马生架构

原文链接:分布式锁—6.Redisson的同步器组件 - 东阳马生架构 - 博客园

体验地址:引迈 - JNPF快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2313098.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

OpenEuler24.x下ZABBIX6/7实战1:zabbix7.2.4安装及zabbix-agent安装

兰生幽谷&#xff0c;不为莫服而不芳&#xff1b; 君子行义&#xff0c;不为莫知而止休。 1 安装及准备 先决条件&#xff1a;建议使用CentOS8以上的操作系统。 CentOS8.5.2111内核版本为 图1- 1 华为OpenEuler24(以后简称OE24)的内核为 [rootzbxsvr ~]# uname -r 5.10.0-…

ROS实践一构建Gazebo机器人模型文件urdf

URDF&#xff08;Unified Robot Description Format&#xff09;是一种基于XML的格式&#xff0c;用于描述机器人模型的结构、关节、连杆和传感器信息&#xff0c;并可以与Gazebo、RViz等仿真环境结合使用。 一、基础语法 1. urdf文件组成 URDF 主要由以下几个核心元素&#…

C++学习——哈希表(一)

文章目录 前言一、哈希表的模板代码二、哈希计数器三、哈希表中的无序映射四、哈希表的总结 前言 本文为《C学习》的第11篇文章&#xff0c;今天学习最后一个数据结构哈希表&#xff08;散列表&#xff09;。 一、哈希表的模板代码 #include<iostream> using namespace…

DeepSeek R1在医学领域的应用与技术分析(Discuss V1版)

DeepSeek R1作为一款高性能、低成本的国产开源大模型,正在深刻重塑医学软件工程的开发逻辑与应用场景。其技术特性,如混合专家架构(MoE)和参数高效微调(PEFT),与医疗行业的实际需求紧密结合,推动医疗AI从“技术驱动”向“场景驱动”转型。以下从具体业务领域需求出发,…

Git 如何配置多个远程仓库和免密登录?

自我简介&#xff1a;4年导游&#xff0c;10年程序员&#xff0c;最近6年一直深耕低代码领域&#xff0c;分享低代码和AI领域见解。 通用后台管理系统 代号&#xff1a;虎鲸 缘由 每次开发后台界面都会有很多相同模块&#xff0c;尝试抽离出公共模块作为快速开发的基座。 目标…

【Linux篇】从冯诺依曼到进程管理:计算机体系与操作系统的核心逻辑

&#x1f4cc; 个人主页&#xff1a; 孙同学_ &#x1f527; 文章专栏&#xff1a;Liunx &#x1f4a1; 关注我&#xff0c;分享经验&#xff0c;助你少走弯路&#xff01; 文章目录 1.冯诺依曼体系结构存储分级理解数据流动 2. 操作系统(Operator System)2.1 概念2.2 设计OS的…

【Linux docker】关于docker启动出错的解决方法。

无论遇到什么docker启动不了的问题 就是 查看docker状态sytemctl status docker查看docker日志sudo journalctl -u docker.service查看docker三个配置文件&#xff1a;/etc/docker/daemon.json&#xff08;如果存在&#xff09; /etc/systemd/system/docker.service&#xff…

【医院内部控制专题】7.医院内部控制环境要素剖析(三):人力资源政策

医院成本核算、绩效管理、运营统计、内部控制、管理会计专题索引 一、引言 在当今医疗行业竞争日益激烈的背景下,医院内部控制的重要性愈发凸显。内部控制作为医院管理的关键组成部分,对于保障医院资产安全、提高会计信息质量、提升运营效率以及实现战略目标起着至关重要的…

计算机网络——交换机

一、什么是交换机&#xff1f; 交换机&#xff08;Switch&#xff09;是局域网&#xff08;LAN&#xff09;中的核心设备&#xff0c;负责在 数据链路层&#xff08;OSI第二层&#xff09;高效转发数据帧。它像一位“智能交通警察”&#xff0c;根据设备的 MAC地址 精准引导数…

CentOS7离线部署安装Dify

离线部署安装Dify 在安装 Dify 之前&#xff0c;请确保您的机器满足以下最低系统要求&#xff1a; CPU > 2 核 内存 > 4 GiB 1.安装docker和docker compose 启动 Dify 服务器最简单的方式是通过docker compose。因此现在服务器上安装好docker和docker compose&#xf…

力扣hot100_二叉树(4)_python版本

一、199. 二叉树的右视图 思路&#xff1a; 直接复用层序遍历的代码&#xff0c;然后取每层的最后一个节点代码&#xff1a; class Solution:def rightSideView(self, root: Optional[TreeNode]) -> List[int]:层序遍历取每层的第一个if not root: return []res []queue …

bug-Ant中a-select的placeholder不生效(绑定默认值为undefined)

1.问题 Ant中使用a-select下拉框时&#xff0c;placeholder设置输入框显示默认值提示&#xff0c;vue2ant null与undefined在js中明确的区别&#xff1a; null&#xff1a;一个值被定义&#xff0c;定义为“空值” undefined&#xff1a;根本不存在定义 2.解决 2.1 a-select使…

颠覆语言认知的革命!神经概率语言模型如何突破人类思维边界?

颠覆语言认知的革命&#xff01;神经概率语言模型如何突破人类思维边界&#xff1f; 一、传统模型的世纪困境&#xff1a;当n-gram遇上"月光族难题" 令人震惊的案例&#xff1a;2012年Google语音识别系统将 用户说&#xff1a;“我要还信用卡” 系统识别&#xff…

练习:关于静态路由,手工汇总,路由黑洞,缺省路由相关

这是题目,我已经画分好了网段,题目要求是这样的: 划分网段 我为什么一个网段划了6个可用IP(一个网段8个地址)呢,因为我刚开始吧环回接口理解成一个主机了,导致我认为两个环回主机在一个网段,其实每个网段只需要2个地址就可以完成这个练习,我懒得划了,就按第一张图的网段来吧…

J6打卡——pytorch实现ResNeXt-50实现猴痘检测

&#x1f368; 本文为&#x1f517;365天深度学习训练营中的学习记录博客 &#x1f356; 原作者&#xff1a;K同学啊 1.检查GPU import torch import torch.nn as nn import torchvision.transforms as transforms import torchvision from torchvision import transforms, d…

vue+dhtmlx-gantt 实现甘特图-快速入门【甘特图】

文章目录 一、前言二、使用说明2.1 引入依赖2.2 引入组件2.3 引入dhtmlx-gantt2.4 甘特图数据配置2.5 初始化配置 三、代码示例3.1 Vue2完整示例3.2 Vue3 完整示例 四、效果图 一、前言 dhtmlxGantt 是一款功能强大的甘特图组件&#xff0c;支持 Vue 3 集成。它提供了丰富的功…

音视频入门基础:RTP专题(16)——RTP封装音频时,音频的有效载荷结构

一、引言 《RFC 3640》和《RFC 6416》分别定义了两种对MPEG-4流的RTP封包方式&#xff0c;这两个文档都可以从RFC官网下载&#xff1a; RFC Editor 本文主要对《RFC 3640》中的音频打包方式进行简介。《RFC 3640》总共有43页&#xff0c;本文下面所说的“页数”是指在pdf阅读…

超分之DeSRA

Desra: detect and delete the artifacts of gan-based real-world super-resolution models.DeSRA&#xff1a;检测并消除基于GAN的真实世界超分辨率模型中的伪影Xie L, Wang X, Chen X, et al.arXiv preprint arXiv:2307.02457, 2023. 摘要 背景&#xff1a; GAN-SR模型虽然…

Ubuntu用户安装cpolar内网穿透

前言 Cpolar作为一款体积小巧却功能强大的内网穿透软件&#xff0c;不仅能够在多种环境和应用场景中发挥巨大作用&#xff0c;还能适应多种操作系统&#xff0c;应用最为广泛的Windows、Mac OS系统自不必多说&#xff0c;稍显小众的Linux、树莓派、群辉等也在起支持之列&#…

小程序事件系统 —— 33 事件传参 - data-*自定义数据

事件传参&#xff1a;在触发事件时&#xff0c;将一些数据作为参数传递给事件处理函数的过程&#xff0c;就是事件传参&#xff1b; 在微信小程序中&#xff0c;我们经常会在组件上添加一些自定义数据&#xff0c;然后在事件处理函数中获取这些自定义数据&#xff0c;从而完成…