SpringBoot項目引入Redis后發(fā)現(xiàn)偶爾會出現(xiàn)連接會超時Redis command timed out,看了博客上寫的很多文章,都說可以通過設(shè)置超時時間解決問題,嘗試的一下還是會出現(xiàn)這個問題,其實不管你設(shè)置多久都還是會超時。
原因是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就可以了
org.springframework.boot spring-boot-starter-data-redis io.lettuce lettuce-core redis.clients jedis 二、keys命令會遍歷redis集合,是一個非常耗時的操作,線上應(yīng)該禁止使用此操作,通過業(yè)務(wù)排查發(fā)現(xiàn)有一個業(yè)務(wù)還使用了keys 命令來過濾數(shù)據(jù),線上代碼中使用了keys 命令來過濾key keys命令類似與數(shù)據(jù)庫的全表掃描,會遍歷redis中的所有數(shù)據(jù),而redis的work線程又是單線程的,這個命令執(zhí)行時間過長會阻塞其他正常命令的執(zhí)行,導(dǎo)致其他命令執(zhí)行超時,出現(xiàn)前面問題中的timed out 異常。
解決:
了解到keys 命令的影響,禁止使用keys 命令,特別是線上環(huán)境,禁止使用redis desktop manager 這樣的redis 界面工具連接線上環(huán)境(因為這類工具會通過keys * 來加載全量數(shù)據(jù)到本地),排查代碼中使用keys 命令的情況,必須在redis 服務(wù)器上禁止keys 這樣不安全的命令的使用,還有flushdb flushall等操作。
三、spring-boot-starter-data-redis有兩種實現(xiàn):lettuce 和 jedis 區(qū)別
1.Jedis:
Jedis是同步的,不支持異步,Jedis客戶端實例不是線程安全的,需要每個線程一個Jedis實例,所以一般通過連接池來使用Jedis。
優(yōu)點:
缺點:
springboot鏈接Redis客戶端Jedis的pom.xml配置
org.springframework.boot spring-boot-starter-data-redis io.lettuce lettuce-core redis.clients jedis 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
2.Lettuce:
Lettuce是基于Netty框架的事件驅(qū)動的Redis客戶端,其方法調(diào)用是異步的,Lettuce的API也是線程安全的,所以多個線程可以操作單個Lettuce連接來完成各種操作,同時Lettuce也支持連接池.
優(yōu)點:
缺點:
org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2 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
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站標題:Rediscommandtimedout兩種異常情況-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article40/diipeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、手機網(wǎng)站建設(shè)、企業(yè)建站、定制網(wǎng)站、外貿(mào)建站、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容