redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool ...

做JedisPool时遇到的问题还真不少,遇到了入下问题,这里提供一个解决办法,不一定使用所有人遇到的情况,因为可能有很多人使用的是在虚拟机里配置的redis,我用的配置是centos服务器上的redis,问题的原因可能就是出自这里,因为如果是本地的话,redis.conf里面的配置默认是 bind 127.0.0.1,而远程连接的话要将这句配置注释掉,并且非本地连接时会默认进入保护模式?需要设置密码或者修改其他的配置以达到访问的目的。

比如以下的提示

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:

1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 
    
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
    
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
    
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

 

本次要解决的错误:

redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
    at redis.clients.jedis.util.Pool.getResource(Pool.java:53)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:234)
    at com.jedis.jedistest.PoolTest.main(PoolTest.java:15)
Caused by: java.util.NoSuchElementException: Unable to validate object
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:479)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:346)
    at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
    ... 2 more

 

百度Google找到的解决办法都不适用,后来想到,可能是由于远程访问时没设置密码导致的?刚好看到这里的讨论,https://www.oschina.net/question/579073_113004

于是就将poolConfig.setTestOnBorrow(false); 从原来的true改为false,并给redis设置了密码123,访问可以进行。

 

 

以下给出原视频链接:https://www.bilibili.com/video/av51989396/?p=28

代码是照着里面敲的,不过有些包的版本不同,所以有些方法改变了。

JedisPoolUtil.java

package com.jedis.jedistest;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

//Double Chcek Lock
public class JedisPoolUtil {

    private static volatile JedisPool jedisPool = null;

    // 构造方法私有化
    private JedisPoolUtil() {
    }

    // 通过一个方法返回池的实例
    public static JedisPool getJedisPoolInstance() {

        if (null == jedisPool) {
            // 锁定对象
            synchronized (JedisPoolUtil.class) {
                if (null == jedisPool) {

                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    // poolConfig.setMaxTotal(1000);
                    poolConfig.setMaxTotal(1000);
                    poolConfig.setMaxIdle(32);
                    poolConfig.setMaxWaitMillis(100 * 1000);
                    poolConfig.setTestOnBorrow(false);
                //    poolConfig.setTestOnBorrow(true);

                    jedisPool = new JedisPool(poolConfig, "233.233.223.223", 6666);
                }
            }
        }

        return jedisPool;

    }

    public static void release(JedisPool jedispool,Jedis jedis) {
        if(null!=jedis) {
            //jedisPool.returnResource(jedis) ---> jedis.close();
            //升级版的jedis用close来替代returnResource?
            //jedispool.getResource();
            jedis.close();
        }
    }
}

 

PoolTest.java

 1 package com.jedis.jedistest;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 
 6 public class PoolTest {
 7 
 8     public static void main(String[] args) {
 9     
10         JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance();
11         Jedis jedis = null;
12             
13         
14         try {
15             jedis = jedisPool.getResource();
16             jedis.auth("123");
17             jedis.set("k1", "siyi");
18         } catch (Exception e) {
19             e.printStackTrace();
20         }finally {
21             JedisPoolUtil.release(jedisPool, jedis);
22         }
23 
24     }
25 
26 }

 

转载于:https://www.cnblogs.com/Guhongying/p/11222314.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用[1]、[2]和[3]中都提到了redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted的错误。这个错误表示Redis连接池中的资源已经用尽,无法获取到可用的资源。这个错误通常发生在并发请求较多或者连接池配置不合理的情况下。 要解决这个错误,可以考虑以下几个方面: 1. 增加连接池的最大连接数:可以通过增加连接池的最大连接数来提高连接池的容量,以满足更多的并发请求。可以通过修改连接池的配置文件或者代码来实现。 2. 调整连接池的配置参数:可以根据实际情况调整连接池的配置参数,如最大空闲连接数、最小空闲连接数、最大等待时间等,以优化连接池的性能和资源利用率。 3. 检查代码中的连接使用和释放:确保在使用完连接后及时释放连接,避免连接被长时间占用而无法回收。 4. 检查Redis服务器的性能:如果连接池配置合理,但仍然出现连接池耗尽的情况,可能是Redis服务器的性能瓶颈导致的。可以检查Redis服务器的配置和性能指标,如内存使用情况、CPU负载等,以确定是否需要升级硬件或优化Redis配置。 综上所述,当出现redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted的错误时,可以通过增加连接池的最大连接数、调整连接池的配置参数、检查代码中的连接使用和释放以及检查Redis服务器的性能等方式来解决该问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值