到这边 发现lock字眼,开始怀疑所有请求api都被阻塞了
于是再次ssh 服务器 安装 arthas ,(Arthas 是Alibaba开源的JAVA诊断工具)
执行thread命令
文章插图
图片
发现大量http-nio的线程waiting状态,http-nio-8083-exec-这个线程其实就是出来http请求的tomcat线程
arthas随意找一个线程查看堆内存
thread -428
文章插图
图片
这是能确认就是api一直转圈的问题,就是这个redis获取连接的代码导致的,
解读这段内存代码 所有线程都在等 @53e5504e这个对象释放锁 。于是jstack 全局搜了一把53e5504e ,没有找到这个对象所在线程 。
自此 。问题原因能确定是 redis连接获取的问题 。但是什么原因造成获取不到连接的还不能确定
再次执行 arthas 的thread -b (thread -b, 找出当前阻塞其他线程的线程)
文章插图
图片
没有结果 。这边和想的不一样,应该是能找到一个阻塞线程的,于是看了下这个命令的文档,发现有下面的一句话
文章插图
图片
好吧,我们刚好是后者 。。。。
减短连接超时时间再次整理下思路 。这次修改redis pool 配置,将获取连接超时时间设置为2s,然后等问题再次复现时观察应用最后正常时干过什么 。
添加一下配置
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();.......JedisPoolConfig config = new JedisPoolConfig();config.setMaxWaitMillis(2000);.......jedisConnectionFactory.afterPropertiesSet();
重启服务,等待 。。。。又过一天,再次复现
ssh 服务器,检查tomcat accesslog ,发现大量api 请求出现500,
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolat org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:140)at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:229)at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:57)at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177)at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:85)at org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:48)
找到源头第一次出现500地方,发现以下代码
.......Cursor c = stringRedisTemplate.getConnectionFactory().getConnection().scan(options);while (c.hasNext()) {.....,,}
分析这个代码,stringRedisTemplate.getConnectionFactory().getConnection()获取pool中的redisConnection后,并没有后续操作,也就是说此时redis 连接池中的链接被租赁后并没有释放或者退还到链接池中,虽然业务已处理完毕 redisConnection 已经空闲,但是pool中的redisConnection的状态还没有回到idle状态
文章插图
图片
正常应为
文章插图
图片
自此问题已经找到 。
小小总结总结:spring stringRedisTemplate 对redis常规操作做了一些封装,但还不支持像 Scan SetNx等命令,这时需要拿到jedis Connection进行一些特殊的Commands
使用
stringRedisTemplate.getConnectionFactory().getConnection()
是不被推荐的我们可以使用
stringRedisTemplate.execute(new RedisCallback<Cursor>() {@Overridepublic Cursor doInRedis(RedisConnection connection) throws DataAccessException {return connection.scan(options);}});
来执行,或者使用完connection后 ,用
RedisConnectionUtils.releaseConnection(conn, factory);
来释放connection.同时,redis中也不建议使用keys命令,redis pool的配置应该合理配上,否则出现问题无错误日志,无报错,定位相当困难 。
推荐阅读
- 使用Redis时要避免的5个错误
- |在职场中如何做才能让领导和同事觉得「这年轻人靠谱、有前途」?
- redis主从同步参数repl_backlog_size测算
- 华为架构师整理Redis数据结构的大厂最佳实践
- 极简Redis使用
- Redis常用的数据结构
- 泡面|防晒品可别乱用!
- 超详细Redis内容整理
- 5分钟掌握NIO底层原理
- nginx + lua + redis实现限流