Jedis访问Redis集群
环境
开启集群
- 开启集群命令,开启相应的配置文件
- redis-server 7001/redis.conf
- redis-server 7002/redis.conf
- redis-server 7003/redis.conf
- redis-server 7004/redis.conf
- redis-server 7005/redis.conf
- redis-server 7006/redis.conf
- 检查是否开启集群
连接测试
public static void main(String[] args) throws Exception {
HostAndPort hp1 = new HostAndPort("centos01", 7001);
HostAndPort hp2 = new HostAndPort("centos01", 7002);
HostAndPort hp3 = new HostAndPort("centos01", 7003);
HostAndPort hp4 = new HostAndPort("centos01", 7004);
HostAndPort hp5 = new HostAndPort("centos01", 7005);
HostAndPort hp6 = new HostAndPort("centos01", 7006);
Set hs = new HashSet();
hs.add(hp1);
hs.add(hp2);
hs.add(hp3);
hs.add(hp4);
hs.add(hp5);
hs.add(hp6);
JedisCluster jc = new JedisCluster(hs);
jc.set("a", "test");
jc.close();
}
以上操作都没问题,那么测试成功
常见的错误
- java.net.ConnectException: Connection refused: connect
- cluster is down
- 连接失败--通常是redis集群没有完全起起来
![](https://i.imgur.com/UZ6Ujcb.png)
- 解决方案-重新启动shutdown的redis服务