全部产品
Search
文档中心

云数据库 Redis:通过客户端程序连接Redis

更新时间:Feb 22, 2024

云数据库Redis版与原生Redis完全兼容,支持所有原生Redis支持的客户端,连接数据库的方式也基本相同,您可以根据自身应用特点选用任何兼容Redis协议的客户端程序。

前提条件

根据客户端程序的部署位置,完成下述操作:

客户端程序部署位置

需完成的操作

ECS实例(推荐)

  1. 确保ECS实例与Redis实例属于同一专有网络(即实例基本信息中的专有网络ID一致)。

    说明
  2. 获取ECS实例的内网IP地址。具体操作,请参见查询ECS实例的IP地址

  3. 将ECS实例的内网IP地址添加至Redis实例的白名单中。具体操作,请参见设置白名单

本地

  1. Redis实例默认仅提供内网连接地址,通过公网连接时您需要手动申请公网连接地址。具体操作,请参见申请公网连接地址

  2. 获取本地设备公网IP地址的方式可能因你所处的网络环境或操作不同而不同。以下是不同系统通过命令方式获取本地设备公网IP地址的参考方法:

    • Linux操作系统:打开终端,输入curl ifconfig.me命令后回车。

    • Windows操作系统:打开命令提示符,输入curl ip.me命令后回车。

    • macOS操作系统:打开终端,输入curl ifconfig.me命令后回车。

  3. 将本地客户端的公网IP地址添加至Redis实例的白名单中。具体操作,请参见设置白名单

注意事项

  • 如果您的Redis实例为集群架构读写分离架构,实例默认会提供Proxy(代理)节点的连接地址,连接方式与连接标准架构的Redis实例相同。

    说明

    集群架构的实例通过直连地址连接时,连接方式与连接开源Redis Cluster相同。

  • 如果实例开启了专有网络免密访问,同一专有网络下的客户端程序无需设置密码即可连接Redis实例。

如何获取连接信息

在使用客户端程序连接Redis实例时,通常您需要获取以下信息并设置在代码中:

需获取的信息

获取方式

实例的连接地址

Redis实例支持多种连接地址,推荐使用专有网络连接,可获得更高的安全性和更低的网络延迟。更多信息,请参见查看连接地址

端口号

端口号默认为6379,您也可以自定义端口号。具体操作,请参见修改连接地址或端口

实例的账号(部分客户端程序无需设置)

Redis实例默认会创建一个以实例ID命名的账号(例如r-bp10noxlhcoim2****),您也可以创建一个新的账号并赋予权限。更多信息,请参见创建与管理账号

账号的密码

根据选取账号的不同,密码的填写格式有一定区别:

  • 默认账号(以实例ID命名的账号):直接填写密码即可。

  • 新创建的账号:密码格式为<user>:<password>。例如自定义账号为testaccount,密码为Rp829dlwa,密码需填写为testaccount:Rp829dlwa

说明
  • 如果通过第三方数据库管理工具(例如RDM等)连接Redis实例,请在密码框中输入user:password进行连接。

  • 如果忘记密码,您可以重置密码。具体操作,请参见修改或重置密码

常见客户端示例

关于Redis支持的客户端列表请参见Redis Clients

重要

本文仅列举常见客户端程序连接Redis的代码示例,帮助您快速连接,如需查看通过客户端程序连接Tair(Redis企业版)的代码示例,请参见通过客户端程序连接Tair

Jedis

本示例使用Maven方式进行构建,您也可以手动下载Jedis客户端。

  1. 打开编译器,新建项目。

  2. 添加下述pom文件。

    本示例的Jedis版本为4.3.0。

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>4.3.0</version>
    </dependency>
  3. 在编辑器中输入下述代码,然后根据注释提示修改代码。

    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    public class JedisExample {
        public static void main(String[] args) {
            JedisPoolConfig config = new JedisPoolConfig();
            // 最大空闲连接数,需自行评估,不超过Redis实例的最大连接数。
            config.setMaxIdle(200);
            // 最大连接数,需自行评估,不超过Redis实例的最大连接数。
            config.setMaxTotal(300);
            config.setTestOnBorrow(false);
            config.setTestOnReturn(false);
            // 分别将host和password的值替换为实例的连接地址、密码。
            String host = "r-bp1s1bt2tlq3p1****pd.redis.rds.aliyuncs.com";
            String password = "r-bp1s1bt2tlq3p1****:Database123";
            JedisPool pool = new JedisPool(config, host, 6379, 3000, password);
            Jedis jedis = null;
            try {
                jedis = pool.getResource();
                // 执行相关操作,示例如下。
                jedis.set("foo10", "bar");
                System.out.println(jedis.get("foo10"));
                jedis.zadd("sose", 0, "car");
                jedis.zadd("sose", 0, "bike");
                System.out.println(jedis.zrange("sose", 0, -1));
            }
            catch (Exception e) {
                // 超时或其他异常处理。
                e.printStackTrace();
            }
            finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
            pool.destroy();    // 当应用退出,需销毁资源时,调用此方法。此方法会断开连接、释放资源。
        }
    }
  4. 运行上述Project,预期会返回如下结果:

    bar
    [bike, car]
    重要

    在使用Jedis的过程中,如果设置了一些不合理的参数或错误使用某些功能可能会引起报错,关于如何排查,请参见常见报错

PhpRedis

  1. 下载并安装PhpRedis客户端。

  2. 在PHP编辑器中输入下述代码,然后根据注释提示修改代码。

    本示例的PHP版本为8.2.1、PhpRedis版本为5.3.7。

    <?php
     /* 分别将host和port的值替换为实例的连接地址、端口号。 */
     $host = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com";
     $port = 6379;
     /* 分别将user和pwd的值替换为实例的账号和密码 */
     $user = "testaccount";
     $pwd = "Rp829dlwa";
     $redis = new Redis();
     if ($redis->connect($host, $port) == false) {
             die($redis->getLastError());
       }
     if ($redis->auth([$user, $pwd]) == false) {
             die($redis->getLastError());
      }
      /* 完成认证后可执行数据库操作,下述代码为您提供SET与GET的使用示例。 */
     if ($redis->set("foo", "bar") == false) {
             die($redis->getLastError());
     }
     $value = $redis->get("foo");
     echo $value;
     ?>
  3. 执行上述代码。

    说明

    常见报错与解决方法:

redis-py

  1. 下载并安装redis-py客户端。

  2. 在Python编辑器中输入下述代码,然后根据注释提示修改代码。

    本示例的Python版本为3.9、redis-py版本为4.4.1。

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    import redis
    # 分别将host和port的值替换为实例的连接地址、端口号。
    host = 'r-bp10noxlhcoim2****.redis.rds.aliyuncs.com'
    port = 6379
    # 将pwd的值替换为实例的密码。
    pwd = 'testaccount:Rp829dlwa'
    r = redis.Redis(host=host, port=port, password=pwd)
    # 连接建立后即可执行数据库操作,下述代码为您提供SET与GET的使用示例。
    r.set('foo', 'bar')
    print(r.get('foo'))
  3. 执行上述代码。

Spring Data Redis

本示例使用Maven方式进行构建,您也可以手动下载LettuceJedis客户端。

  1. 打开编译器,新建项目。

  2. 添加下述pom文件,并下载Lettuce或Jedis。若使用Lettuce,不建议使用Lettuce 6.3.0以下的版本。

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.aliyun.tair</groupId>
        <artifactId>spring-boot-example</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-example</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
            </dependency>
            <dependency>
                <groupId>io.lettuce</groupId>
                <artifactId>lettuce-core</artifactId>
                <version>6.3.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-transport-native-epoll</artifactId>
                <version>4.1.100.Final</version>
                <classifier>linux-x86_64</classifier>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
  3. 在Spring Data Redis编辑器中输入下述代码,然后根据注释提示修改代码。

    本示例的Spring Data Redis版本为2.4.2。

    • Spring Data Redis With Jedis

      @Bean
           JedisConnectionFactory redisConnectionFactory() {
               RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("host", port);
       
               JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
               // 最大连接数, 根据业务需要设置,不能超过实例规格规定的最大连接数。
               jedisPoolConfig.setMaxTotal(30);
               // 最大空闲连接数, 根据业务需要设置,不能超过实例规格规定的最大连接数。
               jedisPoolConfig.setMaxIdle(20);
               // 关闭 testOn[Borrow|Return],防止产生额外的PING。
               jedisPoolConfig.setTestOnBorrow(false);
               jedisPoolConfig.setTestOnReturn(false);
       
               JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling().poolConfig(
                   jedisPoolConfig).build();
       
               return new JedisConnectionFactory(config, jedisClientConfiguration);
           }
    • Spring Data Redis With Lettuce 

      @Configuration
      public class BeanConfig {
          /**
           *  TCP_KEEPALIVE打开,并且配置三个参数分别为:
           *  TCP_KEEPIDLE = 30
           *  TCP_KEEPINTVL = 10
           *  TCP_KEEPCNT = 3
           */
          private static final int TCP_KEEPALIVE_IDLE = 30;
      
          /**
           * TCP_USER_TIMEOUT参数可以避免在故障宕机场景下,Lettuce持续超时的问题。
           * refer: https://github.com/lettuce-io/lettuce-core/issues/2082
           */
          private static final int TCP_USER_TIMEOUT = 30;
      
          @Bean
          LettuceConnectionFactory redisConnectionFactory() {
              RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
              config.setHostName("r-bp1y4is8svonly****pd.redis.rds.aliyuncs.com");
              config.setPort(6379);
              config.setUsername("r-bp1y4is8svonly****");
              config.setPassword("Da****3");
      
              // Config TCP KeepAlive
              SocketOptions socketOptions = SocketOptions.builder()
                  .keepAlive(KeepAliveOptions.builder()
                      .enable()
                      .idle(Duration.ofSeconds(TCP_KEEPALIVE_IDLE))
                      .interval(Duration.ofSeconds(TCP_KEEPALIVE_IDLE / 3))
                      .count(3)
                      .build())
                  .tcpUserTimeout(TcpUserTimeoutOptions.builder()
                      .enable()
                      .tcpUserTimeout(Duration.ofSeconds(TCP_USER_TIMEOUT))
                      .build())
                  .build();
              LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().clientOptions(
                  ClientOptions.builder().socketOptions(socketOptions).build()).build();
              return new LettuceConnectionFactory(config, lettuceClientConfiguration);
          }
      
          @Bean
          RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
              RedisTemplate<String, Object> template = new RedisTemplate<>();
              template.setConnectionFactory(connectionFactory);
              return template;
          }
      }
  4. 执行上述代码。

C或C++

  1. 下载并安装C客户端

  2. 在C或C++编辑器中输入下述代码,然后根据注释提示修改代码。

    本示例的HiRedis版本为1.1.0。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <hiredis.h>
    int main(int argc, char **argv) {
        unsigned int j;
        redisContext *c;
        redisReply *reply;
        if (argc < 4) {
                printf("Usage: example r-bp10noxlhcoim2****.redis.rds.aliyuncs.com 6379 instance_id password\n");
                exit(0);
        }
        const char *hostname = argv[1];
        const int port = atoi(argv[2]);
        const char *instance_id = argv[3];
        const char *password = argv[4];
        struct timeval timeout = { 1, 500000 }; // 1.5 seconds
        c = redisConnectWithTimeout(hostname, port, timeout);
        if (c == NULL || c->err) {
        if (c) {
                printf("Connection error: %s\n", c->errstr);
                redisFree(c);
        } else {
            printf("Connection error: can't allocate redis context\n");
        }
        exit(1);
        }
        /* AUTH */
        reply = redisCommand(c, "AUTH %s", password);
        printf("AUTH: %s\n", reply->str);
        freeReplyObject(reply);
        /* PING server */
        reply = redisCommand(c,"PING");
        printf("PING: %s\n", reply->str);
        freeReplyObject(reply);
        /* Set a key */
        reply = redisCommand(c,"SET %s %s", "foo", "hello world");
        printf("SET: %s\n", reply->str);
        freeReplyObject(reply);
        /* Set a key using binary safe API */
        reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
        printf("SET (binary API): %s\n", reply->str);
        freeReplyObject(reply);
        /* Try a GET and two INCR */
        reply = redisCommand(c,"GET foo");
        printf("GET foo: %s\n", reply->str);
        freeReplyObject(reply);
        reply = redisCommand(c,"INCR counter");
        printf("INCR counter: %lld\n", reply->integer);
        freeReplyObject(reply);
        /* again ... */
        reply = redisCommand(c,"INCR counter");
        printf("INCR counter: %lld\n", reply->integer);
        freeReplyObject(reply);
        /* Create a list of numbers, from 0 to 9 */
        reply = redisCommand(c,"DEL mylist");
        freeReplyObject(reply);
        for (j = 0; j < 10; j++) {
                char buf[64];
                snprintf(buf,64,"%d",j);
                reply = redisCommand(c,"LPUSH mylist element-%s", buf);
                freeReplyObject(reply);
            }
        /* Let's check what we have inside the list */
        reply = redisCommand(c,"LRANGE mylist 0 -1");
        if (reply->type == REDIS_REPLY_ARRAY) {
                for (j = 0; j < reply->elements; j++) {
                printf("%u) %s\n", j, reply->element[j]->str);
        }
        }
        freeReplyObject(reply);
        /* Disconnects and frees the context */
        redisFree(c);
        return 0;
    }
  3. 编译上述代码。

    gcc -o example -g example.c -I /usr/local/include/hiredis -lhiredis
  4. 测试运行,完成连接。

     example r-bp10noxlhcoim2****.redis.rds.aliyuncs.com 6379 r-bp10noxlhcoim2**** password

.NET

  1. 请下载并安装StackExchange.Redis 2.7.20及以上版本客户端,更多信息请参见StackExchange.Redis升级公告

    重要

    不推荐使用ServiceStack.Redis或CSRedis客户端:

    • 若使用ServiceStack.Redis客户端时遇到客户端的相关问题,您需要向该公司购买相关技术支持服务。

    • CSRedis客户端的原开发者已停止维护。

  2. 在StackExchange.Redis编辑器中输入下述代码,然后根据注释提示修改下述示例代码。

    本示例的StackExchange.Redis版本为2.7.20。

    using StackExchange.Redis;
     // 分别设置实例的连接地址、端口号和密码。
     private static ConfigurationOptions configurationOptions = ConfigurationOptions.Parse("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379,password=testaccount:Rp829dlwa,connectTimeout=2000");
      //the lock for singleton
     private static readonly object Locker = new object();
      //singleton
     private static ConnectionMultiplexer redisConn;
     //singleton
     public static ConnectionMultiplexer getRedisConn()
     {
         if (redisConn == null)
         {
             lock (Locker)
             {
                 if (redisConn == null || !redisConn.IsConnected)
                 {
                     redisConn = ConnectionMultiplexer.Connect(configurationOptions);
                 }
             }
         }
         return redisConn;
     }
    说明
    • ConfigurationOptions是StackExchange.Redis的核心,它被整个应用程序共享和重用,应该设置为单例,相关参数设置说明,请参见ConfigurationOptions

    • 由于GetDatabase()返回的对象是轻量级的,每次用的时候从ConnectionMultiplexer对象中获取即可。

       redisConn = getRedisConn();
       var db = redisConn.GetDatabase();
  3. 通过客户端程序操作常见的数据结构,示例如下:

    String

    //set get
    string strKey = "hello";
    string strValue = "world";
    bool setResult = db.StringSet(strKey, strValue);
    Console.WriteLine("set " + strKey + " " + strValue + ", result is " + setResult);
    //incr
    string counterKey = "counter";
    long counterValue = db.StringIncrement(counterKey);
    Console.WriteLine("incr " + counterKey + ", result is " + counterValue);
    //expire
    db.KeyExpire(strKey, new TimeSpan(0, 0, 5));
    Thread.Sleep(5 * 1000);
    Console.WriteLine("expire " + strKey + ", after 5 seconds, value is " + db.StringGet(strKey));
    //mset mget
    KeyValuePair<RedisKey, RedisValue> kv1 = new KeyValuePair<RedisKey, RedisValue>("key1", "value1");
    KeyValuePair<RedisKey, RedisValue> kv2 = new KeyValuePair<RedisKey, RedisValue>("key2", "value2");
    db.StringSet(new KeyValuePair<RedisKey, RedisValue>[] {kv1,kv2});            
    RedisValue[] values = db.StringGet(new RedisKey[] {kv1.Key, kv2.Key});
    Console.WriteLine("mget " + kv1.Key.ToString() + " " + kv2.Key.ToString() + ", result is " + values[0] + "&&" + values[1]);

    Hash

    string hashKey = "myhash";
    //hset
    db.HashSet(hashKey,"f1","v1");
    db.HashSet(hashKey,"f2", "v2");
    HashEntry[] values = db.HashGetAll(hashKey);
    //hgetall
    Console.Write("hgetall " + hashKey + ", result is");
    for (int i = 0; i < values.Length;i++) 
    {
      HashEntry hashEntry = values[i];
      Console.Write(" " + hashEntry.Name.ToString() + " " + hashEntry.Value.ToString());
    }
    Console.WriteLine();

    List

    //list key
    string listKey = "myList";
    //rpush
    db.ListRightPush(listKey, "a");
    db.ListRightPush(listKey, "b");
    db.ListRightPush(listKey, "c");
    //lrange
    RedisValue[] values = db.ListRange(listKey, 0, -1);
    Console.Write("lrange " + listKey + " 0 -1, result is ");
    for (int i = 0; i < values.Length; i++)
    {
     Console.Write(values[i] + " ");
    }
    Console.WriteLine();

    Set

    //set key
    string setKey = "mySet";
    //sadd
    db.SetAdd(setKey, "a");
    db.SetAdd(setKey, "b");
    db.SetAdd(setKey, "c");
    //sismember
    bool isContains = db.SetContains(setKey, "a");
    Console.WriteLine("set " + setKey + " contains a is " + isContains );

    Sorted Set

    string sortedSetKey = "myZset";
    //sadd
    db.SortedSetAdd(sortedSetKey, "xiaoming", 85);
    db.SortedSetAdd(sortedSetKey, "xiaohong", 100);
    db.SortedSetAdd(sortedSetKey, "xiaofei", 62);
    db.SortedSetAdd(sortedSetKey, "xiaotang", 73);
    //zrevrangebyscore
    RedisValue[] names = db.SortedSetRangeByRank(sortedSetKey, 0, 2, Order.Ascending);
    Console.Write("zrevrangebyscore " + sortedSetKey + " 0 2, result is ");
    for (int i = 0; i < names.Length; i++)
    {
      Console.Write(names[i] + " ");
    }
    Console.WriteLine();

node-redis

  1. 下载并安装node-redis客户端。

  2. 在node-redis客户端中输入下述代码,然后根据注释提示修改代码。

    本示例的Node.js版本为19.4.0、node-redis版本为4.5.1。

    import { createClient } from 'redis';
    
    // 分别设置实例的端口号、连接地址、账号、密码
    const client = createClient({
      // redis[s]://[[username][:password]@][host][:port][/db-number]
      url: 'redis://testaccount:Rp829dlwa@r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379'
    });
    
    client.on('error', (err) => console.log('Redis Client Error', err));
    
    await client.connect();
    
    await client.set('foo', 'bar');
    const value = await client.get('foo');
    console.log("get foo: %s", value);
    await client.disconnect();
    说明

    若提示SyntaxError: Cannot use import statement outside a module,请将.js文件的后缀改为.mjs,并在调用时增加--experimental-modules选项,例如node --experimental-modules redis.mjs

  3. 执行上述代码。

Go-redis

  1. 下载并安装Go-Redis客户端。

  2. 在Go-redis编辑器中输入下述代码,然后根据注释提示修改代码。

    本示例的Go版本为1.18.5、Go-redis版本为8.11.5。

    package main
    
    import (
    	"github.com/go-redis/redis"
    	"fmt"
    )
    
    func ExampleClient() {
    	client := redis.NewClient(&redis.Options{
            // 替换为实例的连接地址和端口
    		Addr:     "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379",
            // 替换为实例的密码
    		Password: "testaccount:Rp829dlwa",
    		DB:       0,  // use default DB
    	})
        // 下述代码为您提供SET与GET的使用示例。
    	err := client.Set("foo", "bar", 0).Err()
    	if err != nil {
    		panic(err)
    	}
    
    	val, err := client.Get("foo").Result()
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println("set : foo -> ", val)
    }
    
    func main() {
    	ExampleClient()
    }
  3. 执行上述代码。

Lettuce

本示例使用Maven方式进行构建,您也可以手动下载Lettuce客户端。

  1. 打开编译器,新建项目。

  2. 添加下述pom文件,并下载Lettuce 6.3.0,不建议使用Lettuce 6.3.0以下的版本。

    本示例的Lettuce版本为6.3.0。

    <<dependencies>
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>6.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport-native-epoll</artifactId>
            <version>4.1.100.Final</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
    </dependencies>
  3. 在编辑器中输入下述代码,然后根据注释提示修改代码。

    import io.lettuce.core.ClientOptions;
    import io.lettuce.core.RedisClient;
    import io.lettuce.core.RedisURI;
    import io.lettuce.core.SocketOptions;
    import io.lettuce.core.SocketOptions.KeepAliveOptions;
    import io.lettuce.core.SocketOptions.TcpUserTimeoutOptions;
    import io.lettuce.core.api.StatefulRedisConnection;
    import io.lettuce.core.api.sync.RedisCommands;
    import java.time.Duration;
    
    public class LettuceExample {
        /**
         *  TCP_KEEPALIVE打开,并且配置三个参数分别为:
         *  TCP_KEEPIDLE = 30
         *  TCP_KEEPINTVL = 10
         *  TCP_KEEPCNT = 3
         */
        private static final int TCP_KEEPALIVE_IDLE = 30;
    
        /**
         * TCP_USER_TIMEOUT参数可以避免在故障宕机场景下,Lettuce持续超时的问题。
         * refer: https://github.com/lettuce-io/lettuce-core/issues/2082
         */
        private static final int TCP_USER_TIMEOUT = 30;
    
        private static RedisClient client = null;
        private static StatefulRedisConnection<String, String> connection = null;
    
        public static void main(String[] args) {
            // 分别将host、user、password和port的值替换为实际的实例信息。
            String host = "r-bp1s1bt2tlq3p1****.redis.rds.aliyuncs.com";
            String user = "r-bp1s1bt2tlq3p1****";
            String password = "Da****3";
            int port = 6379;
    
            // Config RedisURL
            RedisURI uri = RedisURI.Builder
                    .redis(host, port)
                    .withAuthentication(user, password)
                    .build();
    
            // Config TCP KeepAlive
            SocketOptions socketOptions = SocketOptions.builder()
                    .keepAlive(KeepAliveOptions.builder()
                            .enable()
                            .idle(Duration.ofSeconds(TCP_KEEPALIVE_IDLE))
                            .interval(Duration.ofSeconds(TCP_KEEPALIVE_IDLE/3))
                            .count(3)
                            .build())
                    .tcpUserTimeout(TcpUserTimeoutOptions.builder()
                            .enable()
                            .tcpUserTimeout(Duration.ofSeconds(TCP_USER_TIMEOUT))
                            .build())
                    .build();
    
            client = RedisClient.create(uri);
            client.setOptions(ClientOptions.builder()
                    .socketOptions(socketOptions)
                    .build());
            connection = client.connect();
            RedisCommands<String, String> commands = connection.sync();
    
            System.out.println(commands.set("foo", "bar"));
            System.out.println(commands.get("foo"));
    
            // 当应用退出,需销毁资源时,调用此方法。此方法会断开连接、释放资源。
            connection.close();
            client.shutdown();
        }
    }
  4. 执行上述代码,预期会返回如下结果:

    OK
    bar