|
|
package com.monitor.common.util;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Random;
|
|
|
|
|
|
public class RedisCommonUtil {
|
|
|
|
|
|
static Random random = new Random();
|
|
|
static Logger logger = LoggerFactory.getLogger(RedisCommonUtil.class);
|
|
|
|
|
|
public static long getRedisMaxMemory(String host,int port){
|
|
|
Map<String,Object> result=null;
|
|
|
Jedis client =null;
|
...
|
...
|
@@ -22,6 +28,7 @@ public class RedisCommonUtil { |
|
|
}
|
|
|
return maxmemory;
|
|
|
}catch (Exception e){
|
|
|
logger.error(e.toString());
|
|
|
return 0;
|
|
|
}finally {
|
|
|
if(client!=null){
|
...
|
...
|
@@ -53,6 +60,7 @@ public class RedisCommonUtil { |
|
|
}
|
|
|
return result;
|
|
|
}catch (Exception e){
|
|
|
logger.error(e.toString());
|
|
|
return null;
|
|
|
}finally {
|
|
|
if(client!=null){
|
...
|
...
|
@@ -64,15 +72,19 @@ public class RedisCommonUtil { |
|
|
public static boolean getRedisIsSlave(String masterHost,int masterPort ) {
|
|
|
Jedis testClient = null;
|
|
|
try {
|
|
|
String testStr = "yoho_redis_test" + random.nextInt(1000);
|
|
|
//主redis塞值
|
|
|
testClient=new Jedis(masterHost, masterPort);
|
|
|
testClient.set("yoho_redis_monitor_test", "test1");
|
|
|
if("test1".equals(testClient.get("yoho_redis_monitor_test"))){
|
|
|
testClient.set("yoho_redis_test", testStr);
|
|
|
String getTestStr = testClient.get("yoho_redis_test");
|
|
|
if(testStr.equals(getTestStr)){
|
|
|
return true;
|
|
|
}else {
|
|
|
logger.error("RedisIsSlave testStr:{}; getTestStr:{};", testStr, getTestStr);
|
|
|
return false;
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
logger.error(e.toString());
|
|
|
return false;
|
|
|
}finally {
|
|
|
if(testClient!=null){
|
...
|
...
|
@@ -85,17 +97,21 @@ public class RedisCommonUtil { |
|
|
public static boolean getRedisIsSlave(String masterHost,int masterPort,String host,int port ) {
|
|
|
Jedis testClient = null;
|
|
|
try {
|
|
|
String testStr = "test1" + random.nextInt(1000);
|
|
|
//主redis塞值
|
|
|
testClient=new Jedis(masterHost, masterPort);
|
|
|
testClient.set("yoho_redis_monitor_test", "test1");
|
|
|
Thread.sleep(1000);
|
|
|
testClient.set("yoho_redis_monitor_test", testStr);
|
|
|
Thread.sleep(2000);
|
|
|
testClient=new Jedis(host, port);
|
|
|
if("test1".equals(testClient.get("yoho_redis_monitor_test"))){
|
|
|
String getTestStr = testClient.get("yoho_redis_monitor_test");
|
|
|
if(testStr.equals(getTestStr)){
|
|
|
return true;
|
|
|
}else {
|
|
|
logger.error("RedisIsSlave testStr:{}; getTestStr:{};", testStr, getTestStr);
|
|
|
return false;
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
logger.error(e.toString());
|
|
|
return false;
|
|
|
}finally {
|
|
|
if(testClient!=null){
|
...
|
...
|
|