代码:
package com.cn.until;
import redis.clients.jedis.*;
import java.util.LinkedHashSet;
import java.util.Set;
public class RedisClusterUtil {
private static volatile JedisCluster cluster;
private RedisClusterUtil() {
}
public static JedisCluster getInstance() {
if (cluster == null) {
synchronized (RedisClusterUtil.class) {
if (cluster == null) {
Set<HostAndPort> nodes = new LinkedHashSet<>();
nodes.add(new HostAndPort("master", 7001));
nodes.add(new HostAndPort("master", 7002));
nodes.add(new HostAndPort("master", 7003));
nodes.add(new HostAndPort("master", 8001));
nodes.add(new HostAndPort("master", 8002));
nodes.add(new HostAndPort("master", 8003));
cluster = new JedisCluster(nodes, 5000, 3000, 10, "123456", new JedisPoolConfig());
return cluster;
}
}
}
return cluster;
}
public static void main(String[] args) {
JedisCluster cluster = RedisClusterUtil.getInstance();
cluster.set("aaa","AAA");
cluster.set("str","STR");
System.out.println(cluster.get("aaa"));
System.out.println(cluster.get("str"));
}
}