之前有用过redis集群,现在做一个总结
import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
public class RedisCluster {
public static void cluster() {
Set<HostAndPort> nodes=new HashSet<HostAndPort>();
HostAndPort node7001=new HostAndPort("192.168.3.153", 7001);
HostAndPort node7002=new HostAndPort("192.168.3.153", 7002);
HostAndPort node7003=new HostAndPort("192.168.3.153", 7003);
HostAndPort node7004=new HostAndPort("192.168.3.153", 7004);
HostAndPort node7005=new HostAndPort("192.168.3.153", 7005);
HostAndPort node7006=new HostAndPort("192.168.3.153", 7006);
nodes.add( node7001 );
nodes.add( node7002 );
nodes.add( node7003 );
nodes.add( node7004 );
nodes.add( node7005 );
nodes.add( node7006 );
JedisPoolConfig pool=new JedisPoolConfig();
JedisCluster cluster=new JedisCluster( nodes,1000,1000,6,"123456",pool);
cluster.set("aa", "abc");
System.out.println(cluster.get("aa")); ;
try {
cluster.close();
} catch ( Exception e) {
e.printStackTrace();
}
}
public static void sentinel() {
String masterName="mymaster";
//sentinel地址集合
Set<String>set=new HashSet<String>();
set.add("192.168.3.153:12001");
set.add("192.168.3.153:12002");
set.add("192.168.3.153:12003");
JedisPoolConfig pool=new JedisPoolConfig();
pool.setMaxIdle(10);
pool.setMaxTotal(10);
pool.setMaxWaitMillis(10);
pool.setJmxEnabled(true);
JedisSentinelPool jSentinelPool =new JedisSentinelPool(masterName,set,pool,10,"123456");
jSentinelPool.getResource().auth("123456");
jSentinelPool.getResource().set("aa", "111111111111111");
System.out.println( jSentinelPool.getResource().get("aa"));
jSentinelPool.close();
}
}