iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Redis command timed out两种异常情况怎么解决
  • 748
分享到

Redis command timed out两种异常情况怎么解决

2023-07-05 21:07:38 748人浏览 泡泡鱼
摘要

这篇文章主要讲解了“Redis command timed out两种异常情况怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Redis com

这篇文章主要讲解了“Redis command timed out两种异常情况怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Redis command timed out两种异常情况怎么解决”吧!

Redis command timed out

SpringBoot项目引入Redis后发现偶尔会出现连接会超时Redis command timed out,看了博客上写的很多文章,都说可以通过设置超时时间解决问题,尝试的一下还是会出现这个问题,其实不管你设置多久都还是会超时。

原因是springboot2.x之后,springboot默认使用的Redis的客户端是lettuce,而不是jedis,lettuce连接池。

org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 5 second(s)    at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:70)    at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)    at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)    at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)    at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:273)    at org.springframework.data.redis.connection.lettuce.LettuceStrinGCommands.convertLettuceAccessException(LettuceStringCommands.java:799)    at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.get(LettuceStringCommands.java:68)    at org.springframework.data.redis.connection.DefaultedRedisConnection.get(DefaultedRedisConnection.java:266)    at org.springframework.data.redis.core.DefaultValueOperations$1.inRedis(DefaultValueOperations.java:57)    at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:60)

解决:

引入spring-boot-starter-data-redis包,这个包会默认使用 lettuce ,这个问题就lettuce引起的,我们只需要把io.lettuce包移除,换成jedis就可以了

        <!-- redis -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>            <exclusions>                <!-- 过滤lettuce,使用jedis作为redis客户端 -->                <exclusion>                    <groupId>io.lettuce</groupId>                    <artifactId>lettuce-core</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>        </dependency>

二、keys命令会遍历redis集合,是一个非常耗时的操作,线上应该禁止使用此操作,通过业务排查发现有一个业务还使用了keys 命令来过滤数据,线上代码中使用了keys 命令来过滤key keys命令类似与数据库的全表扫描,会遍历redis中的所有数据,而redis的work线程又是单线程的,这个命令执行时间过长会阻塞其他正常命令的执行,导致其他命令执行超时,出现前面问题中的timed out 异常。

Redis command timed out两种异常情况怎么解决

解决:

了解到keys 命令的影响,禁止使用keys 命令,特别是线上环境,禁止使用redis desktop manager 这样的redis 界面工具连接线上环境(因为这类工具会通过keys * 来加载全量数据到本地),排查代码中使用keys 命令的情况,必须在redis 服务器上禁止keys 这样不安全的命令的使用,还有flushdb flushall等操作。

三、spring-boot-starter-data-redis有两种实现方式:lettuce 和 jedis 区别

Jedis:

Jedis是同步的,不支持异步,Jedis客户端实例不是线程安全的,需要每个线程一个Jedis实例,所以一般通过连接池来使用Jedis。

优点:

  • 提供了比较全面的 Redis 操作特性的 api

  • API 基本与 Redis 的指令一一对应,使用简单易理解

缺点:

  • 同步阻塞 IO

  • 不支持异步

  • 线程不安全

springboot链接Redis客户端Jedis的pom.xml配置

        <!-- redis -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>            <exclusions>                <!-- 过滤lettuce,使用jedis作为redis客户端 -->                <exclusion>                    <groupId>io.lettuce</groupId>                    <artifactId>lettuce-core</artifactId>                </exclusion>            </exclusions>        </dependency>        <!-- jedis-->        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>        </dependency>

jedis的yml文件配置如下:

#Redis哨兵模式spring:  redis:    database: 1    passWord: 123456    jedis:      pool:        max-active: 8        min-idle: 0        max-idle: 8    sentinel:      master: mymaster      nodes: 192.168.111.10:26379,192.168.111.11:26379,192.168.111.12:26379

Lettuce:

Lettuce是基于Netty框架的事件驱动的Redis客户端,其方法调用是异步的,Lettuce的API也是线程安全的,所以多个线程可以操作单个Lettuce连接来完成各种操作,同时Lettuce也支持连接池.

优点:

  • 线程安全

  • 基于 Netty 框架的事件驱动的通信,可异步调用

  • 适用于分布式缓存

缺点:

  • API 更抽象,学习使用成本高

springboot链接Redis客户端Jedis的pom.xml配置

        <!-- redis -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>        </dependency><!-- spring2.X集成redis需要common-pool2依赖,如果使用Lettuce作为连接池,需要引入commons-pool2依赖,否则会报错bean注入失败 --><dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-pool2</artifactId></dependency>

jedis的yml文件配置如下:

#Redis哨兵模式spring:   redis:    database: 1    lettuce:      pool:        max-active: 20        max-idle: 10        max-wait: 10000        min-idle: 0      shutdown-timeout: 100    password: 123456    sentinel:      master: mymaster      nodes: 192.168.111.10:26379,192.168.111.11:26379,192.168.111.12:26379    timeout: 3000

感谢各位的阅读,以上就是“Redis command timed out两种异常情况怎么解决”的内容了,经过本文的学习后,相信大家对Redis command timed out两种异常情况怎么解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: Redis command timed out两种异常情况怎么解决

本文链接: https://www.lsjlt.com/news/355363.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作