欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Redis?command?timed?out兩種異常情況的解決方式

 更新時間:2023年04月04日 09:26:15   作者:NPE1  
Redis是我們開發(fā)中常用的數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Redis?command?timed?out兩種異常情況的解決方式,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下

Redis command timed out

SpringBoot項目引入Redis后發(fā)現(xiàn)偶爾會出現(xiàn)連接會超時Redis command timed out,看了博客上寫的很多文章,都說可以通過設(shè)置超時時間解決問題,嘗試的一下還是會出現(xiàn)這個問題,其實不管你設(shè)置多久都還是會超時。

原因是springboot2.x之后,springboot默認(rèn)使用的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包,這個包會默認(rèn)使用 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集合,是一個非常耗時的操作,線上應(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)點:

  • 提供了比較全面的 Redis 操作特性的 API
  • API 基本與 Redis 的指令一一對應(yīng),使用簡單易理解

缺點:

  • 同步阻塞 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
     

2.Lettuce:

Lettuce是基于Netty框架的事件驅(qū)動的Redis客戶端,其方法調(diào)用是異步的,Lettuce的API也是線程安全的,所以多個線程可以操作單個Lettuce連接來完成各種操作,同時Lettuce也支持連接池.

優(yōu)點:

  • 線程安全
  • 基于 Netty 框架的事件驅(qū)動的通信,可異步調(diào)用
  • 適用于分布式緩存

缺點:

  • API 更抽象,學(xué)習(xí)使用成本高

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
     

總結(jié)

到此這篇關(guān)于Redis command timed out兩種異常情況的解決方式的文章就介紹到這了,更多相關(guān)Redis command timed out異常解決內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑

    詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑

    這篇文章主要介紹了詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 啟動springboot項目時報錯:無法訪問org.springframework.web.bind.annotation.GetMapping …具有錯誤的版本 61.0,應(yīng)為52.0?的解決方案

    啟動springboot項目時報錯:無法訪問org.springframework.web.bind.annotatio

    這篇文章給大家分享了啟動springboot項目時報錯:?無法訪問org.springframework.web.bind.annotation.GetMapping …具有錯誤的版本 61.0,應(yīng)為52.0?的解決方案,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 深入了解MyBatis二級緩存

    深入了解MyBatis二級緩存

    今天小編就為大家分享一篇關(guān)于深入了解MyBatis二級緩存,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解

    Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解

    這篇文章主要介紹了Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • java實現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)漢字?jǐn)?shù)字

    java實現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)漢字?jǐn)?shù)字

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)換為漢字?jǐn)?shù)字源代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • SpringBoot讀取資源目錄中JSON文件的方法實例

    SpringBoot讀取資源目錄中JSON文件的方法實例

    最近做項目遇到需要將json類型的配置文件引用到項目中,已經(jīng)將讀取json文件的方法封裝成工具類,下面這篇文章主要給大家介紹了關(guān)于SpringBoot讀取資源目錄中JSON文件的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • SpringMvc實現(xiàn)簡易計算器功能

    SpringMvc實現(xiàn)簡易計算器功能

    這篇文章主要為大家詳細(xì)介紹了SpringMvc實現(xiàn)簡易計算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Java開源工具iText生成PDF簡單實例

    Java開源工具iText生成PDF簡單實例

    這篇文章主要介紹了Java開源工具iText生成PDF簡單實例,本文給出了3段代碼實例,講解創(chuàng)建一個簡單PDF文件,在PDF中添加表格以及在PDF中添加圖片,需要的朋友可以參考下
    2015-07-07
  • springboot相關(guān)面試題匯總詳解

    springboot相關(guān)面試題匯總詳解

    這篇文章主要介紹了springboot相關(guān)面試題匯總詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • spring boot中的靜態(tài)資源加載處理方式

    spring boot中的靜態(tài)資源加載處理方式

    這篇文章主要介紹了spring boot中的靜態(tài)資源加載處理方式,需要的朋友可以參考下
    2017-04-04

最新評論