1.导入依赖
<dependencies>
<!-- 1、 Jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 2、 Junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 3、 Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
使用JedisCluster对象连接Redis集群
public class ClusterTest {
public static void main(String[] args) {
Set<HostAndPort> 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<HostAndPort> nodes
}
记得要配置下图