先贴代码:
<!-- redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
//相关代码如下:
JedisPoolConfig config = new JedisPoolConfig();
config =new JedisPoolConfig();
config.setMaxTotal(60000);//设置最大连接数
config.setMaxIdle(1000); //设置最大空闲数
config.setMaxWaitMillis(3000);//设置超时时间
config.setTestOnBorrow(true);
// 集群结点
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7001")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7002")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7003")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7004")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7005")));
jedisClusterNode.add(new HostAndPort("192.168.246.128", Integer.parseInt("7006")));
JedisCluster jc = new JedisCluster(jedisClusterNode, config);
//JedisCluster jc = new JedisCluster(jedisClusterNode);
jc.set("name", "zhangsan");
String value = jc.get("name");
System.out.println(value);
注意事项
redis配置文件中 bind配置注释掉 或者bind 0.0.0.0 表示所有的服务器都可以连接
如果配置为 bind 127.0.0.1 会报Could not get a resource from the pool 错误
创建集群命令为./redis-trib.rb create --replicas 1 45.78.76.17:7001 45.78.76.17:7002 45.78.76.17:7003 45.78.76.17:7004 45.78.76.17:7005 45.78.76.17:7006 45.78.76.17:7007 45.78.76.17:7008
其中 45.78.76.17为本机IP