Jedis访问Redis集群

亦凉 2022-03-09 19:43 335阅读 0赞

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
  1. <dependencies>
  2. <dependency>
  3. <groupId>redis.clients</groupId>
  4. <artifactId>jedis</artifactId>
  5. <version>2.9.0</version>
  6. </dependency>
  7. </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
  • 检查是否开启集群

    • Xh4t8x0.png

连接测试

  1. public static void main(String[] args) throws Exception {
  2. HostAndPort hp1 = new HostAndPort("centos01", 7001);
  3. HostAndPort hp2 = new HostAndPort("centos01", 7002);
  4. HostAndPort hp3 = new HostAndPort("centos01", 7003);
  5. HostAndPort hp4 = new HostAndPort("centos01", 7004);
  6. HostAndPort hp5 = new HostAndPort("centos01", 7005);
  7. HostAndPort hp6 = new HostAndPort("centos01", 7006);
  8. Set hs = new HashSet();
  9. hs.add(hp1);
  10. hs.add(hp2);
  11. hs.add(hp3);
  12. hs.add(hp4);
  13. hs.add(hp5);
  14. hs.add(hp6);
  15. JedisCluster jc = new JedisCluster(hs);
  16. jc.set("a", "test");
  17. jc.close();
  18. }
  • centos查看key为a的值

    • eKaiomF.png

以上操作都没问题,那么测试成功

常见的错误

  • java.net.ConnectException: Connection refused: connect
  • cluster is down

    • 连接失败—通常是redis集群没有完全起起来
    • UZ6Ujcb.png
    • 解决方案-重新启动shutdown的redis服务

发表评论

表情:
评论列表 (有 0 条评论,335人围观)

还没有评论,来说两句吧...

相关阅读