Redis链接中JedisPool报错Could not get a resource from the pool(无法从连接池中获取资源)
报错如图:
解决方案:
一、从redis服务端入手检查
1、将redis.conf中的bind:127.0.0.1注释掉;
2、将redis.conf中的protected-mode yes改为protected-mode no。
二、检查代码和配置文件
1、检查配置文件
# Redis服务器地址redis.host=127.0.0.1# Redis服务器连接端口redis.port=6379# Redis服务器连接密码(默认为空)redis.password=# 连接超时时间(毫秒)redis.timeout=10000# 连接池最大连接数(使用负值表示没有限制)redis.pool.max-active=200# 连接池最大阻塞等待时间(使用负值表示没有限制)redis.pool.max-wait=-1# 连接池中的最大空闲连接redis.pool.max-idle=8# 连接池中的最小空闲连接redis.pool.min-idle=0
2、检查代码
可以尝试使用SpringBoot集成的Redis的Starter组件,配置类随便百度搜一下就有,下面的yml里面的配置
spring: #redis 配置 redis: database: 1 host: 127.0.0.1 lettuce: pool: max-active: 8 #最大连接数据库连接数,设 0 为没有限制 max-idle: 8 #最大等待连接中的数量,设 0 为没有限制 max-wait: -1ms #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 min-idle: 0 #最小等待连接中的数量,设 0 为没有限制 shutdown-timeout: 100ms password: '' port: 6379
如果你还是想用Jedis自己来操作的话,那可能就是你的JedisPool配置有问题了,因为我就是踩的这个坑,
我的Redis是没有配置密码的,我相信很多人自己调试的也是没有密码的,这里不能写空字符串,要写null,要么你就用其他不用配置密码的构造方法JedisPool(final URI uri)、JedisPool(final URI uri, final int timeout)等等找合适自己的。
下面是我的JedisPool对象
@Bean public JedisPool redisPoolFactory() { try { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWait); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMinIdle(minIdle); // 密码为空设置为null if (StringUtil.isBlank(password)) { password = null; } JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); logger.info("初始化Redis连接池JedisPool成功!地址: {}:{}", host, port); return jedisPool; } catch (Exception e) { logger.error("初始化Redis连接池JedisPool异常:{}", e.getMessage()); } return null; }
解决!