国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

jedispool連redis高并發卡死的問題

2024-07-14 08:40:10
字體:
來源:轉載
供稿:網友

java端在使用jedispool 連接redis的時候,在高并發的時候經常死鎖,或報連接異常,JedisConnectionException,或者getResource 異常等各種問題

在使用jedispool 的時候一定要注意兩點

1。 在獲取 jedisPool和jedis的時候加上線程同步,保證不要創建過多的jedispool 和 jedis

2。 用完Jedis實例后需要返還給JedisPool

整理了一下redis工具類,通過大量測試和高并發測試的

package com.caspar.util; import java.util.concurrent.locks.ReentrantLock;  import org.apache.log4j.Logger; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig;  /**  * Redis 工具類  * @author caspar  *  */ public class RedisUtil {      protected static ReentrantLock lockPool = new ReentrantLock();   protected static ReentrantLock lockJedis = new ReentrantLock();      protected static Logger logger = Logger.getLogger(RedisUtil.class);      //Redis服務器IP   private static String ADDR_ARRAY = FileUtil.getPropertyValue("/properties/redis.properties", "server");      //Redis的端口號   private static int PORT = FileUtil.getPropertyValueInt("/properties/redis.properties", "port");      //訪問密碼 //  private static String AUTH = FileUtil.getPropertyValue("/properties/redis.properties", "auth");      //可用連接實例的最大數目,默認值為8;   //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。   private static int MAX_ACTIVE = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_active");;      //控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例,默認值也是8。   private static int MAX_IDLE = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_idle");;      //等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException;   private static int MAX_WAIT = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_wait");;    //超時時間   private static int TIMEOUT = FileUtil.getPropertyValueInt("/properties/redis.properties", "timeout");;      //在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的;   private static boolean TEST_ON_BORROW = FileUtil.getPropertyValueBoolean("/properties/redis.properties", "test_on_borrow");;      private static JedisPool jedisPool = null;      /**    * redis過期時間,以秒為單位    */   public final static int EXRP_HOUR = 60*60;     //一小時   public final static int EXRP_DAY = 60*60*24;    //一天   public final static int EXRP_MONTH = 60*60*24*30;  //一個月      /**    * 初始化Redis連接池    */   private static void initialPool(){     try {       JedisPoolConfig config = new JedisPoolConfig();       config.setMaxTotal(MAX_ACTIVE);       config.setMaxIdle(MAX_IDLE);       config.setMaxWaitMillis(MAX_WAIT);       config.setTestOnBorrow(TEST_ON_BORROW);       jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[0], PORT, TIMEOUT);     } catch (Exception e) {       logger.error("First create JedisPool error : "+e);       try{         //如果第一個IP異常,則訪問第二個IP         JedisPoolConfig config = new JedisPoolConfig();         config.setMaxTotal(MAX_ACTIVE);         config.setMaxIdle(MAX_IDLE);         config.setMaxWaitMillis(MAX_WAIT);         config.setTestOnBorrow(TEST_ON_BORROW);         jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[1], PORT, TIMEOUT);       }catch(Exception e2){         logger.error("Second create JedisPool error : "+e2);       }     }   }         /**    * 在多線程環境同步初始化    */   private static void poolInit() {     //斷言 ,當前鎖是否已經鎖住,如果鎖住了,就啥也不干,沒鎖的話就執行下面步驟     assert ! lockPool.isHeldByCurrentThread();      lockPool.lock();     try {        if (jedisPool == null) {          initialPool();       }     }catch(Exception e){       e.printStackTrace();     } finally {        lockPool.unlock();     }   }         public static Jedis getJedis() {     //斷言 ,當前鎖是否已經鎖住,如果鎖住了,就啥也不干,沒鎖的話就執行下面步驟     assert ! lockJedis.isHeldByCurrentThread();      lockJedis.lock();          if (jedisPool == null) {        poolInit();     }     Jedis jedis = null;     try {        if (jedisPool != null) {          jedis = jedisPool.getResource();        }     } catch (Exception e) {        logger.error("Get jedis error : "+e);     }finally{       returnResource(jedis);       lockJedis.unlock();     }     return jedis;   }          /**    * 釋放jedis資源    * @param jedis    */   public static void returnResource(final Jedis jedis) {     if (jedis != null && jedisPool !=null) {       jedisPool.returnResource(jedis);     }   }    /**    * 設置 String    * @param key    * @param value    */   public synchronized static void setString(String key ,String value){     try {       value = StringUtil.isEmpty(value) ? "" : value;       getJedis().set(key,value);     } catch (Exception e) {       logger.error("Set key error : "+e);     }   }      /**    * 設置 過期時間    * @param key    * @param seconds 以秒為單位    * @param value    */   public synchronized static void setString(String key ,int seconds,String value){     try {       value = StringUtil.isEmpty(value) ? "" : value;       getJedis().setex(key, seconds, value);     } catch (Exception e) {       logger.error("Set keyex error : "+e);     }   }      /**    * 獲取String值    * @param key    * @return value    */   public synchronized static String getString(String key){     if(getJedis() == null || !getJedis().exists(key)){       return null;     }     return getJedis().get(key);   }    } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 启东市| 昌都县| 武穴市| 分宜县| 新营市| 石台县| 乌鲁木齐市| 盐边县| 皋兰县| 惠安县| 德江县| 柏乡县| 固始县| 济南市| 鄂尔多斯市| 都江堰市| 若尔盖县| 阳信县| 新野县| 郎溪县| 梅河口市| 吴堡县| 永仁县| 德庆县| 垣曲县| 衡南县| 谢通门县| 高邮市| 吉安县| 都江堰市| 达日县| 连云港市| 淄博市| 英山县| 安义县| 淮滨县| 晋宁县| 芜湖县| 襄樊市| 陕西省| 五莲县|