zoukankan      html  css  js  c++  java
  • redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool

    一,问题描述:

    (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool

    但是使用redis客户端可以连接集群(我使用的redis desktop manager)

    在java中通过jedis连接redis单机也成功,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool,

    我以命令行方式操作是没问题的,如下:

    先贴代码:

    <!-- 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);

    纠结了两天也是没sei了,就这么几行代码,看来看去没问题啊,甚至上github去看作者的例子,一模一样啊,一度怀疑人生啊;

    由于我的单机版用命令行和java代码访问都没问题,而且集群通过命令行方式也没问题,所以一直没怀疑我搭建的环境的问题,我怀疑代码,怀疑是否是工程依赖的jedis的版本的bug,换了几个版本还是报同样的错误,最后我才开始查环境,环境的话先关了防火墙,没用,然后再查配置文件,查到

    二:找到问题:这个地方IP的问题,以上是正确的版本,以前有问题的版本的Ip是127.0.0.1,

    原因是这个地方以前我没注释redis.conf文件中的bind 127.0.0.1 然后做集群时使用的命令是:

    ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

    三:解决问题:

    删除以上所有里面这个node.conf文件

    然后:

    重新做集群:

    ./redis-trib.rb create --replicas 1 192.168.246.128:7001 192.168.246.128:7002 192.168.246.128:7003 192.168.246.128:7004 192.168.246.128:7005 192.168.246.128:7006

    然后重新测试就解决了;

    还要注意一下每个redis下面的redis.conf的配置(以下是我的):

    port  7001                                        //端口7001,7002,7003        
    bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群,我直接注释掉了
    daemonize    yes                               //redis后台运行
    pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
    cluster-enabled  yes                           //开启集群  把注释#去掉
    cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7001,7002,7003
    cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
    appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志

  • 相关阅读:
    易用网页下载器V0.1
    重复造轮之权限管理系统
    网页格式化排版代码,专用信息采集后的内容整理
    随机点名软件
    PHP导入Excel表格,读取Excel的内容到数组。
    汉字的书写效果的实现
    php的一个断点续传下载实现
    sentry的安装和使用以及各种问题处理
    CentOS下安装Redis及Redis的PHP扩展
    用ASP.NET_Regsql.exe创建Session数据库
  • 原文地址:https://www.cnblogs.com/liuchuanfeng/p/6899023.html
Copyright © 2011-2022 走看看