redis 获取不到_从Redis连接池获取连接失败的原因说起

问题描述

其他业务线的同学在测试环境发现应用程序一直不能获取redis连接,我帮忙看了下。

首先看应用错误日志

Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:97)

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:143)

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:41)

at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:85)

at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:55)

at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169)

at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:149)

... 76 more

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at redis.clients.util.Pool.getResource(Pool.java:22)

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:90)

... 83 more

Caused by: java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed

at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:871)

at redis.clients.util.Pool.getResource(Pool.java:20)

... 84 more

问题调查

确定环境

发现是使用spring-data-redis通过jedis连接的redis服务端。

这个系统的代码很久没动,已经忘记了。先看看使用的jar版本吧。

查看应用程序使用的相关jar:

lsof -p 19377 | grep -E "jedis|pool|redis"

发现输出的jar包含:commons-pool-1.3.jar、spring-data-redis-1.1.1.RELEASE.jar、jedis-2.1.0.jar

翻了下commons pool相关代码

try {

_factory.activateObject(latch.getPair().value);

if(_testOnBorrow &&

!_factory.validateObject(latch.getPair().value)) {

throw new Exception("ValidateObject failed");

}

synchronized(this) {

_numInternalProcessing--;

_numActive++;

}

return latch.getPair().value;

}

catch (Throwable e) {

PoolUtils.checkRethrow(e);

// object cannot be activated or is invalid

try {

_factory.destroyObject(latch.getPair().value);

} catch (Throwable e2) {

PoolUtils.checkRethrow(e2);

// cannot destroy broken object

}

synchronized (this) {

_numInternalProcessing--;

if (!newlyCreated) {

latch.reset();

_allocationQueue.add(0, latch);

}

allocate();

}

if(newlyCreated) {

throw new NoSuchElementException("Could not create a validated object, cause: " + e.getMessage());

}

else {

continue; // keep looping

}

}

可见客户端应该是配置了testOnBorrow,在校验连接时失败了。

java操作redis有多种客户端,项目使用spring-data-redis操作redis,在spring-data-redis中也有不同的客户端实现如jedis,lettuce等。根据错误日志推断使用的redis客户端实现为jedis。

查看JedisConnectionFactory源码

在JedisPool中定义了校验对象的代码。

public boolean validateObject(final Object obj) {

if (obj instanceof Jedis) {

final Jedis jedis = (Jedis) obj;

try {

return jedis.isConnected() && jedis.ping().equals("PONG");

} catch (final Exception e) {

return false;

}

} else {

return false;

}

}

通过wireshark查看TCP包并确定问题原因

熟悉redis的同学都知道,redis客户端发送“PING”后服务端会返回一个“PONG“作为回应,一般会作为连接的检验方法。

既然校验报错,那抓包看看请求和响应吧!

<
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值