- <dependencies>
-
- <dependency>
- <groupId>redis.clientsgroupId>
- <artifactId>jedisartifactId>
- <version>2.9.0version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.16.20version>
- dependency>
- dependencies>
使用JedisCluster对象连接Redis集群
- public class ClusterTest {
-
- public static void main(String[] args) {
-
- Set
nodes = new HashSet<>(); -
- nodes.add(new HostAndPort("192.168.59.12",7001));
- nodes.add(new HostAndPort("192.168.59.12",7002));
- nodes.add(new HostAndPort("192.168.59.12",7003));
- nodes.add(new HostAndPort("192.168.59.12",7004));
- nodes.add(new HostAndPort("192.168.59.12",7005));
- nodes.add(new HostAndPort("192.168.59.12",7006));
-
- // 创建JedisCluster对象
- JedisCluster jedisCluster = new JedisCluster(nodes);
-
- // 操作
- String value = jedisCluster.set("k1","aaa");
- String k1 = jedisCluster.get("k1");
- System.out.println(k1);
-
- }
- // 创建Set
nodes -
- }
记得要配置下图