Skip to content

Commit a2f70e6

Browse files
authored
Merge pull request #1300 from uglide/lettuce_prod_usage
Lettuce: how to connect and configure Cluster connections
2 parents 822c0ab + 6ef4447 commit a2f70e6

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

content/develop/clients/lettuce/connect.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ public class ConnectBasicTest {
4646
}
4747
```
4848

49+
## Connect to a Redis cluster
50+
51+
To connect to a Redis cluster, use `RedisClusterClient`.
52+
53+
```java
54+
import io.lettuce.core.cluster.RedisClusterClient;
55+
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
56+
57+
//...
58+
try (RedisClusterClient clusterClient = RedisClusterClient.create(redisURI)) {
59+
StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
60+
61+
//...
62+
63+
connection.close();
64+
}
65+
```
66+
67+
Learn more about Cluster connections and how to configure them in [the reference guide](https://redis.github.io/lettuce/ha-sharding/#redis-cluster).
68+
4969
## Asynchronous connection
5070

5171
```java

content/develop/clients/lettuce/produsage.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,58 @@ try (RedisClient client = RedisClient.create(redisURI)) {
6868
}
6969
```
7070

71+
## Cluster topology refresh
72+
The Redis Cluster configuration is dynamic and can change at runtime.
73+
New nodes may be added, and the primary node for a specific slot can shift.
74+
Lettuce automatically handles [MOVED]({{< relref "/operate/oss_and_stack/reference/cluster-spec#moved-redirection" >}}) and [ASK]({{< relref "/operate/oss_and_stack/reference/cluster-spec#ask-redirection" >}}) redirects, but to enhance your application's resilience, you should enable adaptive topology refreshing:
75+
76+
```java
77+
RedisURI redisURI = RedisURI.Builder
78+
.redis("localhost")
79+
// set the global default from the default 60 seconds to 30 seconds
80+
.withTimeout(Duration.ofSeconds(30))
81+
.build();
82+
83+
// Create a RedisClusterClient with adaptive topology refresh
84+
try (RedisClusterClient clusterClient = RedisClusterClient.create(redisURI)) {
85+
// Enable TCP keep-alive and TCP user timeout just like in the standalone example
86+
SocketOptions.TcpUserTimeoutOptions tcpUserTimeout = SocketOptions.TcpUserTimeoutOptions.builder()
87+
.tcpUserTimeout(Duration.ofSeconds(20))
88+
.enable()
89+
.build();
90+
91+
SocketOptions.KeepAliveOptions keepAliveOptions = SocketOptions.KeepAliveOptions.builder()
92+
.interval(Duration.ofSeconds(5))
93+
.idle(Duration.ofSeconds(5))
94+
.count(3)
95+
.enable()
96+
.build();
97+
98+
SocketOptions socketOptions = SocketOptions.builder()
99+
.tcpUserTimeout(tcpUserTimeout)
100+
.keepAlive(keepAliveOptions)
101+
.build();
102+
103+
// Enable adaptive topology refresh
104+
// Configure adaptive topology refresh options
105+
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
106+
.enableAllAdaptiveRefreshTriggers()
107+
.adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30))
108+
.build();
109+
110+
ClusterClientOptions options = ClusterClientOptions.builder()
111+
.topologyRefreshOptions(topologyRefreshOptions)
112+
.socketOptions(socketOptions).build();
113+
114+
clusterClient.setOptions(options);
115+
116+
StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
117+
System.out.println(connection.sync().ping());
118+
connection.close();
119+
}
120+
```
121+
Learn more about topology refresh configuration settings in [the reference guide](https://redis.github.io/lettuce/ha-sharding/#redis-cluster).
122+
71123

72124
## DNS cache and Redis
73125

0 commit comments

Comments
 (0)