百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

Redis客户端Lettuce源码「二」Lettuce是如何基于Netty建立连接的

mhr18 2024-11-28 08:35 15 浏览 0 评论

lettuce-core版本: 5.1.7.RELEASE

先看一下Lettuce的基本使用方法,使用Lettuce大概分为如下几步:

  1. 基于Redis连接信息创建RedisClient
  2. 基于RedisClient创建StatefulRedisConnection
  3. 从Connection中获取Command,基于Command执行Redis命令操作。
/**
 * @author xiaobing
 * @date 2019/12/20
 */
public class LettuceSimpleUse {
    private void testLettuce() throws ExecutionException, InterruptedException {
        //构建RedisClient对象,RedisClient包含了Redis的基本配置信息,可以基于RedisClient创建RedisConnection
        RedisClient client = RedisClient.create("redis://localhost");

        //创建一个线程安全的StatefulRedisConnection,可以多线程并发对该connection操作,底层只有一个物理连接.
        StatefulRedisConnection<String, String> connection = client.connect();

        //获取SyncCommand。Lettuce支持SyncCommand、AsyncCommands、ActiveCommand三种command
        RedisStringCommands<String, String> sync = connection.sync();
        String value = sync.get("key");
        System.out.println("get redis value with lettuce sync command, value is :" + value);

        //获取SyncCommand。Lettuce支持SyncCommand、AsyncCommands、ActiveCommand三种command
        RedisAsyncCommands<String, String> async = connection.async();
        RedisFuture<String> getFuture = async.get("key");
        value = getFuture.get();
        System.out.println("get redis value with lettuce async command, value is :" + value);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new LettuceSimpleUse().testLettuce();
    }
}

先看一张建立连接的时序图,有一个直观的印象。

lettuce源码--建立redis连接


RedisClient

一个可扩展、线程安全的RedisClient,支持sync、async、reactor执行模式。
RedisClient.create只是传入了一些配置信息,此时并没有创建连接。

// 使用默认的ClientResource
public static RedisClient create(String uri) {
    LettuceAssert.notEmpty(uri, "URI must not be empty");
    return new RedisClient(null, RedisURI.create(uri));
}
// ClientResources中包含了一些配置和线程池信息,是一个比较重的资源,多个RedisClient可以共享同一个ClientResource
protected RedisClient(ClientResources clientResources, RedisURI redisURI) {
    super(clientResources);
    assertNotNull(redisURI);
    this.redisURI = redisURI;
    setDefaultTimeout(redisURI.getTimeout());
 }

RedisClient.connnect

可以看到connect方法有一些重载方法,默认的是用UTF8 String对key和value序列化,通过传入RedisCodec支持自定义的对Key和Value的序列化方式。

    public StatefulRedisConnection<String, String> connect() {
        return connect(newStringStringCodec());
    }

    public <K, V> StatefulRedisConnection<K, V> connect(RedisCodec<K, V> codec) {

        checkForRedisURI();
        //connectStandaloneAsync是异步创建connection,返回的是Future对象,通过getConnection转为同步操作
        return getConnection(connectStandaloneAsync(codec, this.redisURI, timeout));
    }
    //异步转同步操作
    protected <T> T getConnection(ConnectionFuture<T> connectionFuture) {
        try {
            return connectionFuture.get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw RedisConnectionException.create(connectionFuture.getRemoteAddress(), e);
        } catch (Exception e) {

            if (e instanceof ExecutionException) {
                throw RedisConnectionException.create(connectionFuture.getRemoteAddress(), e.getCause());
            }
            throw RedisConnectionException.create(connectionFuture.getRemoteAddress(), e);
        }
    }

RedisClient.connectStandaloneAsync

    private <K, V> ConnectionFuture<StatefulRedisConnection<K, V>> connectStandaloneAsync(RedisCodec<K, V> codec,
            RedisURI redisURI, Duration timeout) {

        assertNotNull(codec);
        checkValidRedisURI(redisURI);

        logger.debug("Trying to get a Redis connection for: " + redisURI);
        //创建一个有状态的EndPoint用于抽象底层channel的实现,DefaultEndpoint内部封装断线重连、重连后成功后回放连接失败期间的command。同时封装了AT_MOST_ONCE、AT_LEAST_ONCE的可靠性实现(该逻辑是基于内存的,所以并不可靠)。
        DefaultEndpoint endpoint = new DefaultEndpoint(clientOptions, clientResources);
        RedisChannelWriter writer = endpoint;

        //进一步封装,添加支持过期时间的执行命令
        if (CommandExpiryWriter.isSupported(clientOptions)) {
            writer = new CommandExpiryWriter(writer, clientOptions, clientResources);
        }
        //创建StatefulRedisConnectionImpl对象,StatefulRedisConnectionImpl对外提供RedisCommand对象,内部基于writer发送命令。此时并没有真正的创建物理连接,该类本身是无状态、线程安全的。
        StatefulRedisConnectionImpl<K, V> connection = newStatefulRedisConnection(writer, codec, timeout);
        //异步创建Redis物理连接,返回future对象。后面可以看到future中返回的对象其实还是上面的connection
        ConnectionFuture<StatefulRedisConnection<K, V>> future = connectStatefulAsync(connection, codec, endpoint, redisURI,
                () -> new CommandHandler(clientOptions, clientResources, endpoint));

        future.whenComplete((channelHandler, throwable) -> {

            if (throwable != null) {
                connection.close();
            }
        });

        return future;
    }
    //StatefulRedisConnectionImpl的构造函数,此时已经创建了sync、async、reactive三种类型的RedisCommand。基于RedisCodec对key和value序列化,通过write把命令真正的发出去。
    public StatefulRedisConnectionImpl(RedisChannelWriter writer, RedisCodec<K, V>     codec, Duration timeout) {

        super(writer, timeout);

        this.codec = codec;
        this.async = newRedisAsyncCommandsImpl();
        this.sync = newRedisSyncCommandsImpl();
        this.reactive = newRedisReactiveCommandsImpl();
    }

RedisClient.connectStatefulAsync

    private <K, V, S> ConnectionFuture<S> connectStatefulAsync(StatefulRedisConnectionImpl<K, V> connection,
            RedisCodec<K, V> codec, Endpoint endpoint,
            RedisURI redisURI, Supplier<CommandHandler> commandHandlerSupplier) {
        //构建ConnectionBuidler,通过ConnectionBuilder来创建connection
        ConnectionBuilder connectionBuilder;
        if (redisURI.isSsl()) {
            SslConnectionBuilder sslConnectionBuilder = SslConnectionBuilder.sslConnectionBuilder();
            sslConnectionBuilder.ssl(redisURI);
            connectionBuilder = sslConnectionBuilder;
        } else {
            connectionBuilder = ConnectionBuilder.connectionBuilder();
        }
        //填充StatefulRedisConnectionImpl
        connectionBuilder.connection(connection);
        //控制RedisClient行为的一些配置参数
        connectionBuilder.clientOptions(clientOptions);
        //ClientResource包含了一些EventLoopGroup信息
        connectionBuilder.clientResources(clientResources);
        //配置commandHandlerSupplier,这个commandHandler很重要,是实现StatefulRedisConnectionImpl线程安全的关键,后面会详细讲。
        connectionBuilder.commandHandler(commandHandlerSupplier).endpoint(endpoint);
        //connectionBuilder填充Bootstrap等更多的信息
        //getSocketAddressSupplier是根据redisURI获取真正的Redis连接信息,如:sentinel模式下,需要从sentinel获取到真实的redis连接地址
        connectionBuilder(getSocketAddressSupplier(redisURI), connectionBuilder, redisURI);
        //配置netty的channeltype
        channelType(connectionBuilder, redisURI);

        if (clientOptions.isPingBeforeActivateConnection()) {
            if (hasPassword(redisURI)) {
                connectionBuilder.enableAuthPingBeforeConnect();
            } else {
                connectionBuilder.enablePingBeforeConnect();
            }
        }
        //初始化channel,在这一步才真正的异步的去创建物理连接
        ConnectionFuture<RedisChannelHandler<K, V>> future = initializeChannelAsync(connectionBuilder);
        ConnectionFuture<?> sync = future;

        if (!clientOptions.isPingBeforeActivateConnection() && hasPassword(redisURI)) {
            //连接成功之后发送auth命令,做密码的验证
            sync = sync.thenCompose(channelHandler -> {

                CommandArgs<K, V> args = new CommandArgs<>(codec).add(redisURI.getPassword());
                return connection.async().dispatch(CommandType.AUTH, new StatusOutput<>(codec), args);
            });
        }
        //设置clientName,从Redis服务端执行client list可以看到clientname
        if (LettuceStrings.isNotEmpty(redisURI.getClientName())) {
            sync = sync.thenApply(channelHandler -> {
                connection.setClientName(redisURI.getClientName());
                return channelHandler;
            });
        }
        //选择db
        if (redisURI.getDatabase() != 0) {

            sync = sync.thenCompose(channelHandler -> {

                CommandArgs<K, V> args = new CommandArgs<>(codec).add(redisURI.getDatabase());
                return connection.async().dispatch(CommandType.SELECT, new StatusOutput<>(codec), args);
            });
        }
        //返回connection对象
        return sync.thenApply(channelHandler -> (S) connection);
    }

RedisClient.connectionBuilder

//为ConnectionBuidler填充更多的信息,如Bootstrap、channelGroup
protected void connectionBuilder(Mono<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder,
            RedisURI redisURI) {
        //创建Netty客户端的Bootstrap对象
        Bootstrap redisBootstrap = new Bootstrap();
        //Bootstrap的一些配置参数,具体可以参考Netty的相关书籍(Netty权威指南)
        redisBootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
        redisBootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
        redisBootstrap.option(ChannelOption.ALLOCATOR, BUF_ALLOCATOR);

        SocketOptions socketOptions = getOptions().getSocketOptions();

        redisBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
                Math.toIntExact(socketOptions.getConnectTimeout().toMillis()));

        if (LettuceStrings.isEmpty(redisURI.getSocket())) {
            //keepAlive参数,默认为true
            redisBootstrap.option(ChannelOption.SO_KEEPALIVE, socketOptions.isKeepAlive());
            //tcp_nodelay参数,默认为true
            redisBootstrap.option(ChannelOption.TCP_NODELAY, socketOptions.isTcpNoDelay());
        }

        connectionBuilder.timeout(redisURI.getTimeout());
        connectionBuilder.password(redisURI.getPassword());
        //把构建出来的bootStrap对象赋值给connectionBuidler,由connectionBuilder创建连接
        connectionBuilder.bootstrap(redisBootstrap);

        //Netty的相关参数配置,待研究        
        connectionBuilder.channelGroup(channels).connectionEvents(connectionEvents).timer(timer);
        //配置socket地址提供者
        connectionBuilder.socketAddressSupplier(socketAddressSupplier);
    }

RedisClient.initializeChannelAsync

//初始化redis连接,返回ChannelFuture对象
protected <K, V, T extends RedisChannelHandler<K, V>> ConnectionFuture<T> initializeChannelAsync(
            ConnectionBuilder connectionBuilder) {

        Mono<SocketAddress> socketAddressSupplier = connectionBuilder.socketAddress();

        if (clientResources.eventExecutorGroup().isShuttingDown()) {
            throw new IllegalStateException("Cannot connect, Event executor group is terminated.");
        }
        //创建socketAddressFuture 对象,当socketAddressSupplier异步获取SocketAddress成功之后会把SocketAddress数据放入该对象中
        CompletableFuture<SocketAddress> socketAddressFuture = new CompletableFuture<>();
        //创建channelReadyFuture,当连接建立成功之后会把Channel对象放入该对象中
        CompletableFuture<Channel> channelReadyFuture = new CompletableFuture<>();

        //配置获取SocketAddress异步操作之后的操作:
        //1. 把SocketAddress对象放入socketAddressFuture中
        //2. 基于SocketAddress调用initializeChannelAsync0方法真正去建立连接
        socketAddressSupplier.doOnError(socketAddressFuture::completeExceptionally).doOnNext(socketAddressFuture::complete)
                .subscribe(redisAddress -> {

                    if (channelReadyFuture.isCancelled()) {
                        return;
                    }
                    //异步建立真正的连接,如果建立成功会把生产的Channel对象放入channelReadyFuture中
                    initializeChannelAsync0(connectionBuilder, channelReadyFuture, redisAddress);
                }, channelReadyFuture::completeExceptionally);
        //建立连接成功之后返回的还是connectionBuilder的connection对象,即StatefulRedisConnectionImpl
        return new DefaultConnectionFuture<>(socketAddressFuture, channelReadyFuture.thenApply(channel -> (T) connectionBuilder
                .connection()));
    }

RedisClient.initializeChannelAsync0

//真正的去建立Redis物理连接,这里面有很多基于Future的异步操作,如果看不太懂,建议先看看Future的相关知识,多看几遍。
private void initializeChannelAsync0(ConnectionBuilder connectionBuilder, CompletableFuture<Channel> channelReadyFuture,
            SocketAddress redisAddress) {

        logger.debug("Connecting to Redis at {}", redisAddress);

        Bootstrap redisBootstrap = connectionBuilder.bootstrap();
        //创建PlainChannelInitializer对象,PlainChannelIntializer对象会在Channel初始化的时候添加很多Handlers(Netty的Handler概念可以参考Netty权威指南),如:CommandEncoder、CommandHandler(非常重要的Handler)、ConnectionWatchdog(实现断线重连)
        RedisChannelInitializer initializer = connectionBuilder.build();
        //RedisChannelInitializer配置到Bootstrap中
        redisBootstrap.handler(initializer);

        //调用一些通过ClientResources自定义的回调函数
        clientResources.nettyCustomizer().afterBootstrapInitialized(redisBootstrap);
        //获取initFuture 对象,如果Channel初始化完成,可以通过该对象获取到初始化的结果
        CompletableFuture<Boolean> initFuture = initializer.channelInitialized();
        //真正的通过Netty异步的方式去建立物理连接,返回ChannelFuture对象
        ChannelFuture connectFuture = redisBootstrap.connect(redisAddress);
        //配置异常处理
        channelReadyFuture.whenComplete((c, t) -> {

            if (t instanceof CancellationException) {
                connectFuture.cancel(true);
                initFuture.cancel(true);
            }
        });

        connectFuture.addListener(future -> {
            //异常处理
            if (!future.isSuccess()) {

                logger.debug("Connecting to Redis at {}: {}", redisAddress, future.cause());
                connectionBuilder.endpoint().initialState();
                //赋值channelReadyFuture告知出现异常了
                channelReadyFuture.completeExceptionally(future.cause());
                return;
            }
            //当Channel初始化完成之后,根据初始化的结果做判断
            initFuture.whenComplete((success, throwable) -> {
                //如果异常为空,则初始化成功。
                if (throwable == null) {

                    logger.debug("Connecting to Redis at {}: Success", redisAddress);
                    RedisChannelHandler<?, ?> connection = connectionBuilder.connection();
                    connection.registerCloseables(closeableResources, connection);
                    //把成功之后的结果赋值给channelReadyFuture对象
                    channelReadyFuture.complete(connectFuture.channel());
                    return;
                }

                //如果初始化Channel的过程中出现异常的处理逻辑
                logger.debug("Connecting to Redis at {}, initialization: {}", redisAddress, throwable);
                connectionBuilder.endpoint().initialState();
                Throwable failure;

                if (throwable instanceof RedisConnectionException) {
                    failure = throwable;
                } else if (throwable instanceof TimeoutException) {
                    failure = new RedisConnectionException("Could not initialize channel within "
                            + connectionBuilder.getTimeout(), throwable);
                } else {
                    failure = throwable;
                }
                //赋值channelReadyFuture告知出现异常了
                channelReadyFuture.completeExceptionally(failure);
            });
        });
    }

至此,Redis的Connection的建立连接的主流程就结束了,具体的一些逻辑如:断线重连是如何实现的,Redis模式下是怎么基于Sentinel获取Redis实际连接的等等会在后续的文章中介绍。

相关推荐

JDK从8升级到21的问题集(jdk8版本)

一、背景与挑战1.升级动因oOracle长期支持策略o现代特性需求:协程、模式匹配、ZGC等o安全性与性能的需求oAI新技术引入的版本要求2.项目情况o100+项目并行升级的协同作战o多技术栈并存o持...

科莱特SAP ABAP干货 | 基础篇:二、数据字典(1)

ABAP开发基础及应用二、数据字典(1)目标及说明目标:1、掌握SAP数据字典各种对象的定义和使用2、能根据业务数据要求,建立存储数据用的表和视图需求说明:1、在SAP中自建程序,以对一企业(自由行集...

Mycat入门(mycatalog)

一、Linux系统下搭建Mycat注:如果没有服务器可以选择虚拟机进行操作1、首先准备好服务器或者虚拟机2、安装和配置MySQL数据库2.1、删除原来的数据库//查询已安装的mariadb[root...

MySQL从入门到实战:表设计、索引优化与高频面试解析

一、业务场景驱动表设计:电商订单系统案例场景背景某电商平台需设计用户、商品、订单模块,要求支持以下操作:用户高频查询最新订单按商品分类+价格区间筛选商品统计每日订单金额建表实战--用户表(反范式设计...

oracle生成AWR报告的两种方法(oracle中awr报告的输出格式有哪些)

方法1:sqlplus中执行:execdbms_workload_repository.create_snapshot();@?/rdbms/admin/awrrpt.sql方法2:setpage...

Navicat Premium:数据库管理神器(navicat 本地数据库)

提供免费下载网站Mavom.cn在数据库管理的世界里,「NavicatPremium」是一款「一体化的数据库管理和迁移工具」,适用于MySQL、SQLServer、Oracle和PostgreSQ...

Iperius Backup功能特色(backup如何使用)

备份作业:1.文件夹备份:本地的文件夹,带SMB的路径文件夹,网络路径文件夹,NAS文件夹等等2.文件备份:对某个文件进行备份3.备份镜像:可以把系统刻录成镜像用于恢复(图1)4.FTP:自带FTP...

sql注入攻击,拿到网站账户,黑客这样神操作

首先的话,我们来仔细搞清楚这个攻击的流程,第一步就是了解一下,什么是sql?又什么是sql注入、sql注入的流程和步骤。1、首先什么是sql,Sql是一种数据库查询和程序设计的语言,这里的数据库是指存...

Java 8 新特性指南(java的八大特性)

本教程可以在实验楼(shiyanlou.com)中在线练习。一、实验简介Java8是近年来最后起来的一个Java编程语言发行版本。Oracle在2014年3月发布了它。该版本为Java带来...

Java系统开发从入门到精通第三讲(文字版)

下来我们进入数据持久化的部分,对于一个真实的业务系统,能够正常的运转离不开数据的持久化。在数据持久化这块,目前主流的还是关系型数据库(RDBMS),NoSQL(NewSQL)也有了长足发展,特别在大数...

扣细节:while (true) 和 for (;;) 哪个更快

来源:zhihu.com/question/52311366/answer/130090347在JDK8u的jdk项目下做个很粗略的搜索:mymbp:/Users/me/workspace/jdk8u...

JAVA 8 环境安装配置(java环境怎么装)

一、下载这里选择的是OracleJDK,首先到Oracle官网下载JDK8,以Windows10操作系统为例,若是32位系统则下载“Windowsx86”,否则64位系统下载“Windows...

从零开始学Java系列之最详细的Java环境变量配置教程

前言:在上一篇文章中,壹哥给大家重点讲解了Java实现跨平台的原理,不知道你现在有没有弄清楚呢?如果你还有疑问,可以在评论区留言~之前的三篇文章,主要是理论性的内容,其实你暂时跳过不看也是可以的,我们...

Linux下乱码的文件名修改或删除(linux文件名乱码如何处理)

查看文件名#lstouch1?.txt#ll-itotal1469445217956913-rw-r--r--1oracleoinstall0Jan18...

「论文写作-解决方案」数据库TDE加密

数据是应用的核心。对于绝大多数应用,存储数据的数据库是系统日常管理的重中之重。数据库安全背景根据verizon2014年的统计数据,96%数据攻击行为是针对数据库进行的。数据库已经成为外部入侵与内部渗...

取消回复欢迎 发表评论: