Java side when using Jedispool to connect Redis, when high concurrency often die, or report connection anomalies,jedisconnectionexception, or getresource anomalies and other problems
Be sure to pay attention to two points when using Jedispool.
1. Add thread synchronization when acquiring Jedispool and Jedis, ensuring that you do not create too many jedispool and Jedis
2. Need to return to Jedispool after Jedis instance is exhausted
Sort out the Redis tool class, with lots of tests and high concurrency tests
Package Com.caspar.util;import Org.apache.log4j.logger;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import redis.clients.jedis.jedispoolconfig;/** * Redis Tool class * @author Caspar */http Blog.csdn.net/tuposky */public class Redisutil {protected static Logger Logger = Logger.getlogger (Redisutil.class);// Redis server IP private static String Addr_array = Fileutil.getpropertyvalue ("/properties/redis.properties", "Server"); The port number of the Redis private static int port = Fileutil.getpropertyvalueint ("/properties/redis.properties", "Port"); Access password//private static String AUTH = Fileutil.getpropertyvalue ("/properties/redis.properties", "AUTH"); The maximum number of available connection instances, the default value is 8, and//If the assignment is 1, it is unrestricted, and if the pool is already assigned a maxactive Jedis instance, the state of the pool is exhausted (exhausted) at this time. private static int max_active = Fileutil.getpropertyvalueint ("/properties/redis.properties", "max_active");; Controls the maximum number of Jedis instances in a pool that have an idle (idle) state, and the default value is 8. private static int max_idle = Fileutil.getpropertyvaLueint ("/properties/redis.properties", "max_idle");; The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means that never times out. If the wait time is exceeded, the jedisconnectionexception is thrown directly; private static int max_wait = Fileutil.getpropertyvalueint ("/properties/ Redis.properties "," max_wait ");; Timeout time private static int timeout = Fileutil.getpropertyvalueint ("/properties/redis.properties", "timeout");; Whether the validate operation is performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instance is available; private static Boolean Test_on_borrow = Fileutil.getpropertyvalueboolean ("/properties/redis.properties", "Test_on_borrow");; private static Jedispool Jedispool = null; /** * Redis expiration, in seconds */public final static int exrp_hour = 60*60;//One hour public final static int exrp_day = 60*60*24;//Day public final static int exrp_month = 60*60*24*30;//one month/** * Initializing Redis Connection pool */Private STA tic 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{//If the first IP exception, access the second 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); }}}/** * Synchronous initialization in multithreaded environment */private static synchronized void Poolinit () {if (Jedispool = = null) {Initialpool (); }}/** * Synchronize get Jedis instance *@return Jedis */public synchronized static Jedis Getjedis () {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); } return Jedis; }/** * Frees Jedis resource * @param Jedis */public static void Returnresource (final Jedis Jedis) { if (Jedis! = null && jedispool!=null) {jedispool.returnresource (Jedis); }}/** * Set String * @param key * @param value */public 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);}} /** * Set Expiration time * @param key * @param seconds in seconds * @param value */public 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);}} /** * Get String Value * @param key * @return value */public static String getString (string key) {if (Getjedis () = = NULL | |!getjed Is (). Exists (key)) {return null;} Return Getjedis (). get (key);}}
Jedispool even Redis high concurrent card dead