jedispool連redis高并發(fā)卡死的問(wèn)題
java端在使用jedispool 連接redis的時(shí)候,在高并發(fā)的時(shí)候經(jīng)常死鎖,或報(bào)連接異常,JedisConnectionException,或者getResource 異常等各種問(wèn)題
在使用jedispool 的時(shí)候一定要注意兩點(diǎn)
1。 在獲取 jedisPool和jedis的時(shí)候加上線程同步,保證不要?jiǎng)?chuàng)建過(guò)多的jedispool 和 jedis
2。 用完Jedis實(shí)例后需要返還給JedisPool
整理了一下redis工具類,通過(guò)大量測(cè)試和高并發(fā)測(cè)試的
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服務(wù)器IP
private static String ADDR_ARRAY = FileUtil.getPropertyValue("/properties/redis.properties", "server");
//Redis的端口號(hào)
private static int PORT = FileUtil.getPropertyValueInt("/properties/redis.properties", "port");
//訪問(wèn)密碼
// private static String AUTH = FileUtil.getPropertyValue("/properties/redis.properties", "auth");
//可用連接實(shí)例的最大數(shù)目,默認(rèn)值為8;
//如果賦值為-1,則表示不限制;如果pool已經(jīng)分配了maxActive個(gè)jedis實(shí)例,則此時(shí)pool的狀態(tài)為exhausted(耗盡)。
private static int MAX_ACTIVE = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_active");;
//控制一個(gè)pool最多有多少個(gè)狀態(tài)為idle(空閑的)的jedis實(shí)例,默認(rèn)值也是8。
private static int MAX_IDLE = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_idle");;
//等待可用連接的最大時(shí)間,單位毫秒,默認(rèn)值為-1,表示永不超時(shí)。如果超過(guò)等待時(shí)間,則直接拋出JedisConnectionException;
private static int MAX_WAIT = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_wait");;
//超時(shí)時(shí)間
private static int TIMEOUT = FileUtil.getPropertyValueInt("/properties/redis.properties", "timeout");;
//在borrow一個(gè)jedis實(shí)例時(shí),是否提前進(jìn)行validate操作;如果為true,則得到的jedis實(shí)例均是可用的;
private static boolean TEST_ON_BORROW = FileUtil.getPropertyValueBoolean("/properties/redis.properties", "test_on_borrow");;
private static JedisPool jedisPool = null;
/**
* redis過(guò)期時(shí)間,以秒為單位
*/
public final static int EXRP_HOUR = 60*60; //一小時(shí)
public final static int EXRP_DAY = 60*60*24; //一天
public final static int EXRP_MONTH = 60*60*24*30; //一個(gè)月
/**
* 初始化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{
//如果第一個(gè)IP異常,則訪問(wèn)第二個(gè)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);
}
}
}
/**
* 在多線程環(huán)境同步初始化
*/
private static void poolInit() {
//斷言 ,當(dāng)前鎖是否已經(jīng)鎖住,如果鎖住了,就啥也不干,沒(méi)鎖的話就執(zhí)行下面步驟
assert ! lockPool.isHeldByCurrentThread();
lockPool.lock();
try {
if (jedisPool == null) {
initialPool();
}
}catch(Exception e){
e.printStackTrace();
} finally {
lockPool.unlock();
}
}
public static Jedis getJedis() {
//斷言 ,當(dāng)前鎖是否已經(jīng)鎖住,如果鎖住了,就啥也不干,沒(méi)鎖的話就執(zhí)行下面步驟
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);
}
}
/**
* 設(shè)置 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);
}
}
/**
* 設(shè)置 過(guò)期時(shí)間
* @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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot2以代碼的方式統(tǒng)一配置Jackson教程
這篇文章主要介紹了Springboot2以代碼的方式統(tǒng)一配置Jackson教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot 動(dòng)態(tài)配置郵箱發(fā)件人過(guò)程解析
這篇文章主要介紹了SpringBoot 動(dòng)態(tài)配置郵箱發(fā)件人過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
解決springboot bean中大寫(xiě)的字段返回變成小寫(xiě)的問(wèn)題
這篇文章主要介紹了解決springboot bean中大寫(xiě)的字段返回變成小寫(xiě)的問(wèn)題,具有很好的參考價(jià)值希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
在RedHat系統(tǒng)上安裝JDK與Tomcat的步驟
這篇文章主要介紹了在RedHat系統(tǒng)上安裝Java與Tomcat的步驟,同樣適用于CentOS等RedHat系的Linux系統(tǒng),需要的朋友可以參考下2015-11-11
SpringCloud搭建Eureka服務(wù)模塊的過(guò)程
Eureka在分布式系統(tǒng)中起到了連接各個(gè)微服務(wù)的紐帶作用,使得服務(wù)之間的交互變得更加靈活、可靠,本文將深入探討如何使用Spring?Cloud,逐步引導(dǎo)讀者完成Eureka服務(wù)模塊的搭建,感興趣的朋友跟隨小編一起看看吧2024-02-02
SpringCloud Config配置中心原理以及環(huán)境切換方式
這篇文章主要介紹了SpringCloud Config配置中心原理以及環(huán)境切換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
JDK都出到14了,你有什么理由不會(huì)函數(shù)式編程(推薦)
這篇文章主要介紹了JDK都出到14了,你有什么理由不會(huì)函數(shù)式編程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Java基礎(chǔ)第二篇方法與數(shù)據(jù)成員
在上一篇文章中介紹了Java基礎(chǔ) 從HelloWorld到面向?qū)ο螅覀兂醪搅私饬藢?duì)象(object)。對(duì)象中的數(shù)據(jù)成員表示對(duì)象的狀態(tài)。對(duì)象可以執(zhí)行方法,表示特定的動(dòng)作。這篇文章我們進(jìn)一步深入到對(duì)象。了解Java中方法與數(shù)據(jù)成員的一些細(xì)節(jié)。2021-09-09
MyBatis-Plus使用ActiveRecord(AR)實(shí)現(xiàn)CRUD
本文將結(jié)合實(shí)例代碼,介紹MyBatis-Plus使用ActiveRecord(AR)實(shí)現(xiàn)CRUD,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07

