Java code to connect to Redis Cluster

1,489 views
Skip to first unread message

My Email

unread,
Aug 13, 2015, 2:14:02 PM8/13/15
to Redis DB
Hi,

I am using Jedis to connect one of the Redis nodes. no issues. 

Now I am trying to connect to Redis Cluster. Not sure how..

Any Sample would help.. 

Thx

Mark Paluch

unread,
Aug 14, 2015, 6:53:11 AM8/14/15
to Redis DB

Here you go:

Jedis:
Set<HostAndPort> connectionPoints = new HashSet<HostAndPort>();
connectionPoints
.add(new HostAndPort("localhost", 7379));

JedisCluster cluster = new JedisCluster(connectionPoints);

cluster
.set("key", "value");
System.out.println(cluster.get("key"));

cluster
.close();

Lettuce:
RedisClusterClient clusterClient = new RedisClusterClient(RedisURI.create("redis://localhost:7379"));
RedisAdvancedClusterConnection<String, String> cluster = clusterClient.connectCluster();

cluster
.set("key", "value");
System.out.println(cluster.get("key"));

cluster
.close();
clusterClient
.shutdown();


You need to specify one more initial connection points to connect the cluster. You can find the complete code at https://gist.github.com/mp911de/8369b765c2fa8586d48c

HTH, Mark
Reply all
Reply to author
Forward
0 new messages