redis jedis连接redis集群 cluster模式代码
redis集群模式支持failover,并且自动支持数据的sharding, 如果数据量很大单个redis实例放不下时只能使用这种模式, 集群模式需要安装ruby环境,集群的配置用的是ruby脚本。
集群模式的配置是:("192.168.159.90", 6279), ("192.168.159.91", 6279),("192.168.159.92", 6279);("192.168.159.90", 6280), ("192.168.159.91", 6280),("192.168.159.92", 6280); 搭建在90, 91, 92 三台服务器上,每台服务器上各两个实例。
import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import java.util.HashSet; import java.util.Set; /** * Created by outofmemory.cn on 2015/11/6. */ public class HelloJedisCluster { public static void main(String[] args) { Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>(); //Jedis Cluster will attempt to discover cluster nodes automatically jedisClusterNodes.add(new HostAndPort("192.168.159.90", 6279)); jedisClusterNodes.add(new HostAndPort("192.168.159.91", 6279)); jedisClusterNodes.add(new HostAndPort("192.168.159.92", 6279)); jedisClusterNodes.add(new HostAndPort("192.168.159.90", 6280)); jedisClusterNodes.add(new HostAndPort("192.168.159.91", 6280)); jedisClusterNodes.add(new HostAndPort("192.168.159.92", 6280)); JedisCluster jc = new JedisCluster(jedisClusterNodes); jc.set("foo", "bar"); String value = jc.get("foo"); System.out.println(value); } }