소스 검색

修正RedisUtil支持密码验证

shuzheng 9 년 전
부모
커밋
3795a08221
1개의 변경된 파일13개의 추가작업 그리고 21개의 파일을 삭제
  1. 13 21
      zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java

+ 13 - 21
zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java

@@ -18,7 +18,7 @@ public class RedisUtil {
 	protected static ReentrantLock lockPool = new ReentrantLock();
 	protected static ReentrantLock lockJedis = new ReentrantLock();
 
-	private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
+	private static Logger _log = LoggerFactory.getLogger(RedisUtil.class);
 
 	// Redis服务器IP
 	private static String IP = PropertiesFileUtil.getInstance("redis").get("master.redis.ip");
@@ -42,7 +42,7 @@ public class RedisUtil {
 	private static int TIMEOUT = PropertiesFileUtil.getInstance("redis").getInt("master.redis.timeout");
 
 	// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
-	private static boolean TEST_ON_BORROW = true;
+	private static boolean TEST_ON_BORROW = false;
 
 	private static JedisPool jedisPool = null;
 
@@ -63,22 +63,9 @@ public class RedisUtil {
 			config.setMaxIdle(MAX_IDLE);
 			config.setMaxWaitMillis(MAX_WAIT);
 			config.setTestOnBorrow(TEST_ON_BORROW);
-			//jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT, PASSWORD);
-			jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT);
+			jedisPool = new JedisPool(config, IP, 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, IP.split(",")[1], PORT, TIMEOUT, PASSWORD);
-				jedisPool = new JedisPool(config, IP.split(",")[1], PORT, TIMEOUT);
-			} catch (Exception e2) {
-				logger.error("Second create JedisPool error : " + e2);
-			}
+			_log.error("First create JedisPool error : " + e);
 		}
 	}
 
@@ -104,9 +91,14 @@ public class RedisUtil {
 		try {
 			if (jedisPool != null) {
 				jedis = jedisPool.getResource();
+				try {
+					jedis.auth(PASSWORD);
+				} catch (Exception e) {
+					_log.error("jedis password error : " + e);
+				}
 			}
 		} catch (Exception e) {
-			logger.error("Get jedis error : " + e);
+			_log.error("Get jedis error : " + e);
 		} finally {
 			returnResource(jedis);
 		}
@@ -133,7 +125,7 @@ public class RedisUtil {
 			value = StringUtils.isEmpty(value) ? "" : value;
 			getJedis().set(key, value);
 		} catch (Exception e) {
-			logger.error("Set key error : " + e);
+			_log.error("Set key error : " + e);
 		}
 	}
 
@@ -148,7 +140,7 @@ public class RedisUtil {
 			value = StringUtils.isEmpty(value) ? "" : value;
 			getJedis().setex(key, seconds, value);
 		} catch (Exception e) {
-			logger.error("Set keyex error : " + e);
+			_log.error("Set keyex error : " + e);
 		}
 	}
 
@@ -172,7 +164,7 @@ public class RedisUtil {
 		try {
 			getJedis().del(key);
 		} catch (Exception e) {
-			logger.error("Remove keyex error : " + e);
+			_log.error("Remove keyex error : " + e);
 		}
 	}