Skip to content

Commit 049d6df

Browse files
committed
docs: add example use case
1 parent 75c16af commit 049d6df

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

docs/advanced-usage.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ When running Jedis in certain network environments, such as behind a NAT gateway
217217

218218
This allows you to dynamically map the address reported by a Redis node to a different address that the client can actually reach. You can implement this either by creating a dedicated class or by using a concise lambda expression.
219219

220+
### Example use case: NAT or Docker with different advertised ports
221+
Suppose you run a Redis cluster inside Docker on a remote host.
222+
Inside the cluster config, nodes announce addresses like:
223+
```
224+
172.18.0.2:6379
225+
172.18.0.3:6379
226+
172.18.0.4:6379
227+
```
228+
But externally, you reach them through the host IP with mapped ports:
229+
```
230+
my-redis.example.com:7001
231+
my-redis.example.com:7002
232+
my-redis.example.com:7003
233+
```
220234
### Implementing with a Dedicated Class
221235

222236
You can provide your mapping logic by creating a class that implements the `HostAndPortMapper` interface. This approach is useful for more complex mapping logic or for reusability.
@@ -245,11 +259,13 @@ Then, instantiate this class and pass it to the JedisCluster constructor:
245259

246260
```java
247261
Map<HostAndPort, HostAndPort> nodeMapping = new HashMap<>();
248-
nodeMapping.put(new HostAndPort("172.18.0.2", 6379), new HostAndPort("localhost", 7001));
249-
nodeMapping.put(new HostAndPort("172.18.0.3", 6379), new HostAndPort("localhost", 7002));
262+
nodeMapping.put(new HostAndPort("172.18.0.2", 6379), new HostAndPort("my-redis.example.com", 7001));
263+
nodeMapping.put(new HostAndPort("172.18.0.3", 6379), new HostAndPort("my-redis.example.com", 7002));
264+
nodeMapping.put(new HostAndPort("172.18.0.4", 6379), new HostAndPort("my-redis.example.com", 7002));
250265

251266
Set<HostAndPort> initialNodes = new HashSet<>();
252-
initialNodes.add(new HostAndPort("localhost", 7001));
267+
// seed node
268+
initialNodes.add(new HostAndPort("my-redis.example.com", 7001));
253269

254270
HostAndPortMapper mapper = new DockerNATMapper(nodeMapping);
255271

@@ -269,11 +285,12 @@ Since HostAndPortMapper is a functional interface (it has only one abstract meth
269285

270286
```java
271287
Map<HostAndPort, HostAndPort> nodeMapping = new HashMap<>();
272-
nodeMapping.put(new HostAndPort("172.18.0.2", 6379), new HostAndPort("localhost", 7001));
273-
nodeMapping.put(new HostAndPort("172.18.0.3", 6379), new HostAndPort("localhost", 7002));
288+
nodeMapping.put(new HostAndPort("172.18.0.2", 6379), new HostAndPort("my-redis.example.com", 7001));
289+
nodeMapping.put(new HostAndPort("172.18.0.3", 6379), new HostAndPort("my-redis.example.com", 7002));
290+
nodeMapping.put(new HostAndPort("172.18.0.4", 6379), new HostAndPort("my-redis.example.com", 7002));
274291

275292
Set<HostAndPort> initialNodes = new HashSet<>();
276-
initialNodes.add(new HostAndPort("localhost", 7001));
293+
initialNodes.add(new HostAndPort("my-redis.example.com", 7001));
277294

278295
HostAndPortMapper mapper = internalAddress -> nodeMapping.getOrDefault(internalAddress, internalAddress);
279296

0 commit comments

Comments
 (0)