Access a Redis Cluster instance through Lettuce on an ECS in the same VPC. For more information on how to use other Redis clients, visit https://redis.io/clients.
If a password was set during DCS Redis instance creation, configure the password for connecting to Redis using Lettuce. Do not hard code the plaintext password.
To connect to a single-node, master/standby, or Proxy Cluster instance, use the RedisClient object of Lettuce. To connect to a Redis Cluster instance, use the RedisClusterClient object.
For details, see Viewing Details of a DCS Instance.
<dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>6.1.6.RELEASE</version> </dependency>
// password indicates the connection password. If there is no password, delete "password@". If there is a password and it contains special characters, conversion is required. RedisClient redisClient = RedisClient.create("redis://password@host:port"); StatefulRedisConnection<String, String> connection = redisClient.connect(); RedisCommands<String, String> syncCommands = connection.sync(); syncCommands.set("key", "value"); System.out.println("Connected to Redis:" + syncCommands.get("key")); // Close the connection. connection.close(); // Close the client. redisClient.shutdown();
// password indicates the connection password. If there is no password, delete "password@". If there is a password and it contains special characters, conversion is required. RedisClient clusterClient = RedisClient.create("redis://password@host:port"); GenericObjectPoolConfig<StatefulRedisConnection<String, String>> genericObjectPoolConfig = new GenericObjectPoolConfig(); // Connection pool parameters genericObjectPoolConfig.setMaxIdle(3); genericObjectPoolConfig.setMinIdle(2); genericObjectPoolConfig.setMaxTotal(3); genericObjectPoolConfig.setMaxWaitMillis(-1); GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport .createGenericObjectPool(() -> clusterClient.connect(), genericObjectPoolConfig); // Obtain a connection to perform operations. try (StatefulRedisConnection<String, String> con = pool.borrowObject()) { RedisCommands<String, String> sync = con.sync(); sync.set("key", "value"); System.out.println("Connected by pool:" + sync.get("key")); } catch (Exception e) { e.printStackTrace(); }finally { // Close the resources. pool.close(); clusterClient.shutdown(); }
// password indicates the connection password. If there is no password, delete "password@". If there is a password and it contains special characters, conversion is required. RedisClusterClient redisClient = RedisClusterClient.create("redis://password@host:port"); StatefulRedisClusterConnection<String, String> connection = redisClient.connect(); RedisAdvancedClusterCommands<String, String> syncCommands = connection.sync(); syncCommands.set("key", "value"); System.out.println("Connected to RedisCluster:"+syncCommands.get("key")); // Close the connection. connection.close(); // Close the client. redisClient.shutdown();