亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Redis連接超時(shí)異常的處理方法

 更新時(shí)間:2020年07月16日 09:00:30   作者:貧貧貧貧僧  
這篇文章主要給大家介紹了關(guān)于Redis連接超時(shí)異常的處理方法,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

0、問(wèn)題描述

使用Jedis連接redis進(jìn)行數(shù)據(jù)查詢(xún)操作,正常的代碼運(yùn)行沒(méi)有問(wèn)題,但是時(shí)不時(shí)會(huì)報(bào)出如下錯(cuò)誤:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
 at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:202)
 at redis.clients.util.RedisInputStream.read(RedisInputStream.java:181)
 at redis.clients.jedis.Protocol.processBulkReply(Protocol.java:181)
 at redis.clients.jedis.Protocol.process(Protocol.java:155)
 at redis.clients.jedis.Protocol.processMultiBulkReply(Protocol.java:206)
 at redis.clients.jedis.Protocol.process(Protocol.java:157)
 at redis.clients.jedis.Protocol.processMultiBulkReply(Protocol.java:206)
 at redis.clients.jedis.Protocol.process(Protocol.java:157)
 at redis.clients.jedis.Protocol.read(Protocol.java:215)
 at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
 at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:285)
 at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:291)
 at redis.clients.jedis.BinaryJedis.hscan(BinaryJedis.java:3390)
 at com.ict.mcg.filter.DuplicateClueFilterV2.hscan(DuplicateClueFilterV2.java:867)
 at com.ict.mcg.filter.DuplicateClueFilterV2.collectRecentCluekeywords(DuplicateClueFilterV2.java:487)
 at com.ict.mcg.main.GetCluesMain.run_online(GetCluesMain.java:208)
 at com.ict.mcg.main.GetCluesMain.main(GetCluesMain.java:1685)
Caused by: java.net.SocketTimeoutException: Read timed out
 at java.net.SocketInputStream.socketRead0(Native Method)
 at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
 at java.net.SocketInputStream.read(SocketInputStream.java:171)
 at java.net.SocketInputStream.read(SocketInputStream.java:141)
 at java.net.SocketInputStream.read(SocketInputStream.java:127)
 at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196)
 ... 16 more

究其原因,可以定位為java.net.SocketTimeoutException: Read timed out,即網(wǎng)絡(luò)連接異常;

1、 可能的原因

1.1 服務(wù)器資源包括內(nèi)存、磁盤(pán)、cpu等利用率高

經(jīng)過(guò)查看redis部署機(jī)器的狀態(tài)信息,發(fā)現(xiàn)整體機(jī)器運(yùn)行狀態(tài)良好

1.2 服務(wù)器設(shè)置防火墻,導(dǎo)致連接失敗

因?yàn)檎5拇a流程都可以跑通,所以防火墻設(shè)置沒(méi)有問(wèn)題;

1.3 redis配置文件bind監(jiān)聽(tīng)host配置不當(dāng)

redis的配置文件中bind對(duì)應(yīng)host的配置如下:

# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1

默認(rèn)的bind綁定的host為0.0.0.0,即可以監(jiān)聽(tīng)每一個(gè)可用的網(wǎng)絡(luò)接口;相當(dāng)于配置為:

bind 0.0.0.0

我們的配置文件也配置正常,而且正常的代碼流程運(yùn)行正常,也可以佐證這一點(diǎn);

1.4 Jedis使用配置問(wèn)題

目前Jedis的連接池配置如下:

private static JedisPool getPool() {
    if (pool == null) {
      JedisPoolConfig config = new JedisPoolConfig();
      //控制一個(gè)pool可分配多少個(gè)jedis實(shí)例,通過(guò)pool.getResource()來(lái)獲取;
      //如果賦值為-1,則表示不限制;如果pool已經(jīng)分配了maxActive個(gè)jedis實(shí)例,則此時(shí)pool的狀態(tài)為exhausted(耗盡)。
      config.setMaxActive(10);
      //控制一個(gè)pool最多有多少個(gè)狀態(tài)為idle(空閑的)的jedis實(shí)例。
      config.setMaxIdle(2);
      //表示當(dāng)borrow(引入)一個(gè)jedis實(shí)例時(shí),最大的等待時(shí)間,如果超過(guò)等待時(shí)間,則直接拋出JedisConnectionException;
      config.setMaxWait(1000 * 200000);
      //在borrow一個(gè)jedis實(shí)例時(shí),是否提前進(jìn)行validate操作;如果為true,則得到的jedis實(shí)例均是可用的;
      config.setTestOnBorrow(true);
      config.setTestOnReturn(true);

      //目前redis只有一個(gè)服務(wù)器
      pool = new JedisPool(config, "localhost", 6379);
    }
    return pool;
  }

  private static Jedis getJedis() {
    Jedis jedis = null;
    int count = 0;
    do {
      try {
        pool = getPool();
        jedis = pool.getResource();
      } catch(Exception e) {
//    System.out.println(e.getMessage());
        e.printStackTrace();
        pool.returnBrokenResource(jedis);
      }

      count++;
    } while (jedis==null && count < 3);

    return jedis;
  }

構(gòu)建JedisPool的邏輯中,只是設(shè)置了config.setMaxWait(1000 * 200000);,這個(gè)是引入新的jedis實(shí)例的最大等待時(shí)間,并沒(méi)有進(jìn)行其他相關(guān)的連接超時(shí)的配置;于是查看JedisPool的源代碼,發(fā)現(xiàn)如下:

public JedisPool(final Config poolConfig, final String host) {
    this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
  }

  public JedisPool(String host, int port) {
    this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
  }

  public JedisPool(final String host) {
    this(host, Protocol.DEFAULT_PORT);
  }

  public JedisPool(final Config poolConfig, final String host, int port,
      int timeout, final String password) {
    this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
  }

  public JedisPool(final Config poolConfig, final String host, final int port) {
    this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
  }

  public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
    this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);
  }

  public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
          final int database) {
    super(poolConfig, new JedisFactory(host, port, timeout, password, database));
  }

由上述代碼可以看到,JedisPool有多個(gè)重載的構(gòu)造函數(shù),并且構(gòu)造函數(shù)中需要傳入一個(gè)timeout參數(shù)作為連接的超時(shí)時(shí)間,如果沒(méi)有傳,則采用Protocol.DEFAULT_TIMEOUT作為默認(rèn)的超時(shí)時(shí)間,繼續(xù)跟蹤源碼:

public final class Protocol {

  public static final int DEFAULT_PORT = 6379;
  public static final int DEFAULT_TIMEOUT = 2000;
  public static final int DEFAULT_DATABASE = 0;

  public static final String CHARSET = "UTF-8";

  public static final byte DOLLAR_BYTE = '$';
  public static final byte ASTERISK_BYTE = '*';
  public static final byte PLUS_BYTE = '+';
  public static final byte MINUS_BYTE = '-';
  public static final byte COLON_BYTE = ':';

  private Protocol() {
 // this prevent the class from instantiation
  }

可以得出結(jié)論,默認(rèn)JedisPool中連接的默認(rèn)超時(shí)時(shí)間為2秒,而我們調(diào)用的JedisPool構(gòu)造函數(shù),恰恰采用的是這個(gè)配置,只要兩秒鐘沒(méi)有連接成功,redis的連接就斷開(kāi),從而報(bào)錯(cuò),這在數(shù)據(jù)庫(kù)請(qǐng)求并發(fā)量比較大的時(shí)候是有可能發(fā)生的,遂做如下更改,在創(chuàng)建JedisPool的時(shí)候,傳入一個(gè)較大的超時(shí)時(shí)間:

pool = new JedisPool(config, ParamUtil.REDIS_ADDRESS[0], ParamUtil.REDIS_PORT, 1000 * 10);

2、總結(jié)

遇到問(wèn)題還是多查,多看源碼,多看源碼中的配置,仔細(xì)一項(xiàng)一項(xiàng)地排查問(wèn)題!

到此這篇關(guān)于Redis連接超時(shí)異常處理的文章就介紹到這了,更多相關(guān)Redis連接超時(shí)異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis連接錯(cuò)誤的情況總結(jié)分析

    Redis連接錯(cuò)誤的情況總結(jié)分析

    這篇文章主要給大家總結(jié)介紹了關(guān)于Redis連接錯(cuò)誤的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • redis的string類(lèi)型及bitmap介紹

    redis的string類(lèi)型及bitmap介紹

    這篇文章主要介紹了redis的string類(lèi)型及bitmap介紹,redis有很多的客戶(hù)端連接進(jìn)來(lái),站在redis所在機(jī)器的角度來(lái)說(shuō),就是有很多socket的連接
    2022-07-07
  • Redis 執(zhí)行性能測(cè)試

    Redis 執(zhí)行性能測(cè)試

    這篇文章主要介紹了Redis 執(zhí)行性能測(cè)試的方法,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí)redis,感興趣的朋友可以了解下
    2020-08-08
  • Redis安全策略詳解

    Redis安全策略詳解

    緩存穿透是指當(dāng)用戶(hù)在查詢(xún)一條數(shù)據(jù)的時(shí)候,而此時(shí)數(shù)據(jù)庫(kù)和緩存卻沒(méi)有關(guān)于這條數(shù)據(jù)的任何記錄,而這條數(shù)據(jù)在緩存中沒(méi)找到就會(huì)向數(shù)據(jù)庫(kù)請(qǐng)求獲取數(shù)據(jù)。用戶(hù)拿不到數(shù)據(jù)時(shí),就會(huì)一直發(fā)請(qǐng)求,查詢(xún)數(shù)據(jù)庫(kù),這樣會(huì)對(duì)數(shù)據(jù)庫(kù)的訪(fǎng)問(wèn)造成很大的壓力
    2022-07-07
  • redis3.2配置文件redis.conf詳細(xì)說(shuō)明

    redis3.2配置文件redis.conf詳細(xì)說(shuō)明

    redis3.2配置詳解,Redis啟動(dòng)的時(shí)候,可以指定配置文件,詳細(xì)說(shuō)明請(qǐng)看本文說(shuō)明
    2018-03-03
  • Redis可視化工具Redis?Desktop?Manager的具體使用

    Redis可視化工具Redis?Desktop?Manager的具體使用

    本文主要介紹了Redis可視化工具Redis?Desktop?Manager的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Redis內(nèi)存碎片處理實(shí)例詳解

    Redis內(nèi)存碎片處理實(shí)例詳解

    內(nèi)存碎片是redis服務(wù)中分配器分配存儲(chǔ)對(duì)象內(nèi)存的時(shí)產(chǎn)生的,下面這篇文章主要給大家介紹了關(guān)于Redis內(nèi)存碎片處理的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Redis數(shù)據(jù)結(jié)構(gòu)SortedSet的底層原理解析

    Redis數(shù)據(jù)結(jié)構(gòu)SortedSet的底層原理解析

    這篇文章主要介紹了Redis數(shù)據(jù)結(jié)構(gòu)SortedSet的底層原理解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • redis-copy使用6379端口無(wú)法連接到Redis服務(wù)器的問(wèn)題

    redis-copy使用6379端口無(wú)法連接到Redis服務(wù)器的問(wèn)題

    這篇文章主要介紹了redis-copy使用6379端口無(wú)法連接到Redis服務(wù)器的問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Redis緩存IO模型的演進(jìn)教程示例精講

    Redis緩存IO模型的演進(jìn)教程示例精講

    這篇文章主要為大家介紹了Redis線(xiàn)程IO模型演進(jìn)的教程示例精講,有需要朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-11-11

最新評(píng)論