Jedis访问Redis集群
Jedis访问Redis集群
环境
centos6.5中配置了6个redis服务,ip映射到centos01上,端口分别是7001~7006
- centos01 7001
- centos01 7002
- centos01 7003
- centos01 7004
- centos01 7005
- centos01 7006
- idea中新建maven工程,pom依赖文件引入jedis
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
开启集群
开启集群命令,开启相应的配置文件
- 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();
}
centos查看key为a的值
以上操作都没问题,那么测试成功
常见的错误
- java.net.ConnectException: Connection refused: connect
cluster is down
- 连接失败—通常是redis集群没有完全起起来
- 解决方案-重新启动shutdown的redis服务
还没有评论,来说两句吧...