java连接redis集群

来源:互联网 发布:pos数据 编辑:程序博客网 时间:2024/06/07 00:47
package redis;import java.util.HashSet;import java.util.Set;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;public class ClusterDemo {private static JedisCluster jedisCluster=null;private static Set<HostAndPort> hostAndPorts=null;public static  Set<HostAndPort> getHostAndPort(String hostAndPort){Set<HostAndPort> hap = new HashSet<HostAndPort>();String[] hosts = hostAndPort.split(",");String[] hs = null;for(String host:hosts){hs=host.split(":");hap.add(new HostAndPort(hs[0], Integer.parseInt(hs[1])));}return hap;}public static JedisCluster getJedisCluster(){GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();gopc.setMaxTotal(32);gopc.setMaxIdle(4);gopc.setMaxWaitMillis(6000);hostAndPorts = getHostAndPort("192.168.10.250:6379");    jedisCluster = new JedisCluster(hostAndPorts, 2000, 2000, 3,"redis123456",gopc);return jedisCluster;}public static void main(String[] args) {jedisCluster = getJedisCluster();System.out.println(jedisCluster.get("name"));}}