【入门篇】1.4 redis 客户端 之 Lettuce 详解


在这里插入图片描述

1. 简介

1. 什么是Lettuce

Lettuce是一个高级Redis客户端,用于线程安全的同步、异步和反应式访问。它支持高级的Redis特性,如Sentinel、集群、流水线、自动重新连接和redis数据模型等。Lettuce是完全非阻塞的,基于Netty事件驱动的通信层,其方法将返回具有可观察的类型,可在流或异步编程环境中使用。

2. Lettuce与其他Redis客户端的比较

与Jedis这种阻塞的Java Redis客户端相比,Lettuce的主要优势在于其完全非阻塞的IO和线程安全。使用Jedis,在多线程环境中,需要使用连接池以避免线程安全问题,但是对于Lettuce来说,可以避免这种复杂的设置,因为一个Lettuce连接实例(StatefulRedisConnection)就是线程安全的。另一个重要的区别是,Lettuce支持Redis的高级特性,如集群、流水线、发布订阅、和Lua脚本。

3. Lettuce的特性和优势

  • 完全非阻塞IO和线程安全:Lettuce使用Netty进行网络通信,采用了异步和事件驱动的模型。一个Lettuce连接实例(StatefulRedisConnection)是线程安全的,可以在多个线程间共享。
  • 支持Redis的高级特性:Lettuce支持Redis的高级特性,如Sentinel、集群、流水线、发布订阅、Lua脚本等。
  • 自动重连:Lettuce支持自动重连,当Redis服务器重新启动或断开连接时,Lettuce可以自动恢复连接。
  • 可观察的数据类型:Lettuce的所有操作都返回Observable,这可以让在流或异步编程环境中使用。

2. 安装和配置

在Java项目中,可以使用Maven或者Gradle来引入Lettuce的依赖。

  1. 使用Maven引入依赖
<dependency>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    <version>6.1.5.RELEASE</version> <!-- 请将此版本号替换为所需要的版本 -->
</dependency>
  1. 使用Gradle引入依赖
dependencies {
    implementation 'io.lettuce:lettuce-core:6.1.5.RELEASE'  
}

配置Lettuce非常简单
创建一个新的RedisClient实例,使用redis://password@localhost:6379/0作为Redis服务器的URL。然后我们打开一个新的连接,并通过此连接获取同步命令来执行Redis命令。执行完命令后,我们关闭了连接并关闭了Redis客户端。

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

public class Example {

    public static void main(String[] args) {

        // 初始化Redis客户端
        RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");

        // 打开一个新的连接
        StatefulRedisConnection<String, String> connection = redisClient.connect();

        // 获取同步命令
        RedisCommands<String, String> syncCommands = connection.sync();

        // 执行命令
        syncCommands.set("key", "Hello, Redis!");

        // 关闭连接
        connection.close();

        // 关闭Redis客户端
        redisClient.shutdown();
    }
}

3. 连接池配置

1. 什么是连接池

连接池是一个预先创建并存储数据库连接的容器,应用程序在需要与数据库交互时,可以从连接池中获取已经建立的连接。使用完毕后,将该连接归还给连接池,而不是关闭它。这种技术可以避免每次需要与数据库交互时都打开新的连接,节省了创建新连接所需的时间。

Lettuce是一个基于Netty的高级Redis客户端,用于线程安全同步、异步和响应式通信。在Lettuce中,每次发送命令都会获取新的连接,执行完命令后就会关闭。这就是说,Lettuce本身并没有传统意义上的"连接池"。

2. Lettuce的连接池使用与配置

虽然在Lettuce本身并未实现连接池,但是在使用Spring Data Redis的情况下,可以通过配置LettuceConnectionFactory来实现连接池的效果。

以下是一个配置示例:

import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

// 创建连接池配置
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
    .poolConfig(new GenericObjectPoolConfig())
    .build();

// 创建服务器配置
RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration("localhost", 6379);

// 使用这两种配置创建Lettuce连接工厂
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(serverConfig, clientConfig);

在这个配置中,我们使用LettucePoolingClientConfiguration.builder()来创建一个LettuceClientConfiguration的Builder,并使用poolConfig()方法来设置连接池配置。然后,我们使用这个配置和服务器配置来创建一个新的LettuceConnectionFactory

注意,GenericObjectPoolConfig可以让设置一些连接池的参数,例如maxTotal(最大连接数),maxIdle(最多的空闲连接数),minIdle(最少的空闲连接数)等等。

在Lettuce中,使用RedisClient或者RedisClusterClient创建的基本连接实际上是一个连接池。可以通过ClientOptionsClientResources来定制连接池的行为。

默认连接池的行为如下:

  1. 默认情况下,连接池大小为8个连接。
  2. 连接尝试失败后,默认重试时间间隔为30秒。

在创建RedisClient或者RedisClusterClient之前,可以通过ClientResources.Builder来定制连接池的一些参数,如下所示:

import io.lettuce.core.RedisClient;
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;

public class Example {

    public static void main(String[] args) {

        // 创建一个ClientResources来定制连接池参数
        ClientResources clientResources = DefaultClientResources.builder()
            .ioThreadPoolSize(16) // IO线程池大小
            .computationThreadPoolSize(16) // 计算线程池大小
            .build();

        // 使用定制的ClientResources来创建RedisClient
        RedisClient redisClient = RedisClient.create(clientResources, "redis://localhost:6379");

        // 其他代码...
    }
}

对于更复杂的需求,如最大连接数、最小空闲连接数等,Lettuce本身并没有提供对应的配置项。Lettuce的设计理念是每个Redis命令都运行在一个独立的连接上,因此并没有传统意义上的“连接池”。可以根据自己的需求,通过控制并发命令的数量来间接控制连接数量。

然而,如果正在使用Spring Data Redis,那么在Spring Data Redis中,可以通过LettucePoolingClientConfiguration来配置连接池(包括最大连接数、最小空闲连接数等),如下所示:

import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
    .poolConfig(new GenericObjectPoolConfig())
    .build();

RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration("server", 6379);

LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(serverConfig, clientConfig);

在上面的代码中,我们使用LettucePoolingClientConfiguration.builder()来创建一个LettuceClientConfiguration的Builder,并使用poolConfig()方法来设置连接池配置。然后,我们使用这个配置和服务器配置来创建一个新的LettuceConnectionFactory

3. 连接池配置项

配置Lettuce的连接池可以通过LettuceConnectionFactory类来实现。下面是配置连接池的一些常用参数:

  1. 最大连接数(MaxConnections):指定连接池的最大连接数,即同时能够从连接池中获取的最大连接数。可以使用setConnectionPoolConfiguration()方法来设置。
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(maxConnections);
lettuceConnectionFactory.setPoolConfig(poolConfig);
  1. 最小空闲连接数(MinIdleConnections):指定连接池中的最小空闲连接数,即连接池中保持的最少可用连接数。可以使用setPoolConfig()方法来设置。
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(minIdleConnections);
lettuceConnectionFactory.setPoolConfig(poolConfig);
  1. 连接空闲超时时间(IdleTimeout):指定连接在空闲状态下的超时时间,超过该时间会被连接池回收。可以使用setTimeout()方法来设置。
lettuceConnectionFactory.setTimeout(idleTimeout);
  1. 连接重试次数(RetryAttempts):指定在连接池无法获取到可用连接时的重试次数。可以使用setRetryAttempts()方法来设置。
lettuceConnectionFactory.setRetryAttempts(retryAttempts);
  1. 连接重试间隔时间(RetryInterval):指定连接重试的间隔时间,即每次重试之间的等待时间。可以使用setRetryInterval()方法来设置。
lettuceConnectionFactory.setRetryInterval(retryInterval);

以上是一些常用的连接池参数的配置方法。根据实际需求,您可以根据需要设置这些参数来优化连接池的性能。

4. 基本操作

1. 如何创建Lettuce连接

你可以使用RedisClientRedisClusterClientconnect()方法来建立一个连接。例如:

RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = redisClient.connect();

2. Lettuce的基本操作如增删改查

可以使用RedisCommands的对应方法进行操作。例如:

RedisCommands<String, String> syncCommands = connection.sync();
// 添加键值对
syncCommands.set("key", "value");
// 获取键值对
String value = syncCommands.get("key");
// 删除键值对
syncCommands.del("key");
// 修改键值对
syncCommands.set("key", "newValue");

3. Lettuce的事务操作

Lettuce支持事务操作,可以使用multi()和exec()方法进行事务的开始和提交:

RedisCommands<String, String> syncCommands = connection.sync();
// 开启事务
syncCommands.multi();
syncCommands.set("key1", "value1");
syncCommands.set("key2", "value2");
// 提交事务
syncCommands.exec();

4. Lettuce的管道操作

Lettuce支持管道操作,可以使用RedisCommandsautoFlushCommands()方法来关闭自动刷新,然后用flushCommands()方法来手动刷新:

RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.setAutoFlushCommands(false);
syncCommands.set("key1", "value1");
syncCommands.set("key2", "value2");
syncCommands.flushCommands();

5. Lettuce的发布订阅操作

Lettuce支持Redis的发布订阅模式,例如:

RedisCommands<String, String> syncCommands = connection.sync();
// 订阅频道
RedisPubSubCommands<String, String> pubSubCommands = connection.sync();
pubSubCommands.subscribe("channel");
// 发布消息
syncCommands.publish("channel", "message");

6. Lettuce的脚本执行

Lettuce支持Lua脚本,可以使用RedisScriptingCommandseval()evalsha()方法来执行脚本:

RedisScriptingCommands<String, String> scriptingCommands = connection.sync();
// 执行Lua脚本
String result = scriptingCommands.eval("return redis.call('get', KEYS[1])", ScriptOutputType.VALUE, new String[]{"key"});

注意:以上代码示例假设你已经有了一个有效的StatefulRedisConnection<String, String>对象。

5. 错误处理和重试

Lettuce的错误处理机制和重试策略,如何处理连接错误和命令执行失败等情况。

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.RedisCommandExecutionException;
import io.lettuce.core.RedisConnectionException;
import io.lettuce.core.RedisCommandInterruptedException;
import io.lettuce.core.RedisCommandTimeoutException;

public class RedisExample {

    public static void main(String[] args) {
        RedisClient redisClient = RedisClient.create("redis://localhost:6379/");
        try {
            StatefulRedisConnection<String, String> connection = redisClient.connect();
            
            try {
                String result = connection.sync().get("key");
                System.out.println(result);
            } catch (RedisCommandExecutionException e) {
                System.err.println("Command execution failed: " + e.getMessage());
            } catch (RedisCommandInterruptedException e) {
                System.err.println("Command interrupted: " + e.getMessage());
            } catch (RedisCommandTimeoutException e) {
                System.err.println("Command timed out: " + e.getMessage());
            } finally {
                connection.close();
            }

        } catch (RedisConnectionException e) {
            System.err.println("Failed to connect to Redis: " + e.getMessage());
        } finally {
            redisClient.shutdown();
        }
    }
}

1. Lettuce的常见异常

  1. RedisConnectionException:当无法连接到Redis服务器时,或在尝试发送命令时连接已关闭或断开,将抛出此异常。

  2. RedisCommandTimeoutException:当命令的执行超过设定的超时时间时,将抛出此异常。

  3. RedisCommandInterruptedException:当等待命令完成的过程中线程被中断,将抛出此异常。

  4. RedisCommandExecutionException:当Redis命令执行失败时(如键不存在,类型不匹配等),将抛出此异常。

2. 如何处理这些异常

  1. RedisConnectionException:检查你的Redis服务器是否正在运行,网络连接是否畅通。如果是因为连接已关闭或断开,可以尝试重新连接。

  2. RedisCommandTimeoutException:可以尝试增加命令的超时时间,或者查看Redis服务器是否有性能问题,如CPU使用过高,磁盘IO过高等。

  3. RedisCommandInterruptedException:可以查看线程为何被中断,并尝试解决中断的原因。

  4. RedisCommandExecutionException:检查你的Redis命令是否正确,如键是否存在,类型是否匹配等。如果命令没有问题,则可能是Redis服务器的问题,需要检查服务器的状况。

6. Lettuce vs Jedis

维度/库LettuceJedis
单线程/多线程Lettuce是基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,应该被用作长期存在的线程安全对象。Jedis实例不是线程安全的,因此在多线程环境下,你需要每个线程创建一个新的连接实例,或者使用连接池。
阻塞/非阻塞Lettuce支持异步、反应式、同步和非阻塞操作。Jedis操作是同步阻塞的,不支持异步和非阻塞操作。
集群支持Lettuce提供了Redis Cluster的原生支持。Jedis也支持Redis Cluster,但需要手动处理重定向。
PUB/SUB模型Lettuce支持发布-订阅模型。Jedis也支持发布-订阅模型。
二进制协议Lettuce直接使用Netty来处理命令和结果,可以处理任何Redis协议和命令。Jedis使用自己的协议处理器,对协议的支持可能不太完整。
项目活跃度Lettuce是目前最活跃的Redis Java客户端项目,一直在持续更新和添加新特性。Jedis的活跃度较低,更新和新功能的添加较慢。
连接池Lettuce的连接实例是线程安全的,大多数情况下,你不需要使用连接池。不过,Lettuce也提供了一个可选的内置连接池。在多线程环境下,你需要使用Jedis的连接池来管理和复用连接。
依赖Lettuce依赖于Netty。Jedis没有外部依赖。
事务Lettuce支持Redis的事务。Jedis也支持Redis的事务。
SentinelLettuce提供了对Redis Sentinel的原生支持。Jedis也支持Redis Sentinel。

7. 参考资料

Lettuce

  1. 官方文档:Lettuce
  2. GitHub:Lettuce GitHub
  3. 深入了解Lettuce:Understanding Lettuce
  4. Lettuce源码分析:Lettuce source code analysis

Jedis

  1. 官方文档:Jedis
  2. GitHub:Jedis GitHub
  3. 深入了解Jedis:Understanding Jedis
  4. Jedis源码分析:Jedis source code analysis

Redis

  1. 官方文档:Redis
  2. GitHub:Redis GitHub
  3. Redis命令:Redis commands
  4. Redis入门教程:Redis tutorial
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰点.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值