...
|
...
|
@@ -10,37 +10,54 @@ import java.util.Map; |
|
|
public class RedisCommonUtil {
|
|
|
|
|
|
public static Map<String,Object> getRedisInfo(String host,int port){
|
|
|
Jedis client = new Jedis(host, port);
|
|
|
String redisInfo=client.info();
|
|
|
if(StringUtils.isBlank(redisInfo)){
|
|
|
Map<String,Object> result=null;
|
|
|
Jedis client =null;
|
|
|
try {
|
|
|
client=new Jedis(host, port);
|
|
|
String redisInfo=client.info();
|
|
|
if(StringUtils.isBlank(redisInfo)){
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
Map<String,Object> result=new HashMap<String, Object>();
|
|
|
String[] arr = redisInfo.split("\r\n");
|
|
|
for (String str : arr) {
|
|
|
if (str.startsWith("#")||str.startsWith("\r\n")) {
|
|
|
continue;
|
|
|
}
|
|
|
String[] a = str.split(":");
|
|
|
if(a.length==2){
|
|
|
result.put(a[0], a[1]);
|
|
|
result=new HashMap<String, Object>();
|
|
|
String[] arr = redisInfo.split("\r\n");
|
|
|
for (String str : arr) {
|
|
|
if (str.startsWith("#")||str.startsWith("\r\n")) {
|
|
|
continue;
|
|
|
}
|
|
|
String[] a = str.split(":");
|
|
|
if(a.length==2){
|
|
|
result.put(a[0], a[1]);
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}catch (Exception e){
|
|
|
return null;
|
|
|
}finally {
|
|
|
if(client!=null){
|
|
|
client.close();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static boolean getRedisIsSlave(String masterHost,int masterPort,String host,int port ) {
|
|
|
|
|
|
Jedis masterClient = new Jedis(masterHost, masterPort);
|
|
|
masterClient.set("yoho_redis_monitor_test", "test");
|
|
|
Jedis client = new Jedis(host, port);
|
|
|
client.get("yoho_redis_monitor_test");
|
|
|
if(client.get("yoho_redis_monitor_test").equals("test")){
|
|
|
return true;
|
|
|
}else {
|
|
|
Jedis testClient = null;
|
|
|
try {
|
|
|
//主redis塞值
|
|
|
testClient=new Jedis(masterHost, masterPort);
|
|
|
testClient.set("yoho_redis_monitor_test", "test1");
|
|
|
testClient=new Jedis(host, port);
|
|
|
if("test1".equals(testClient.get("yoho_redis_monitor_test"))){
|
|
|
return true;
|
|
|
}else {
|
|
|
return false;
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
return false;
|
|
|
}finally {
|
|
|
if(testClient!=null){
|
|
|
testClient.close();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -48,5 +65,6 @@ public class RedisCommonUtil { |
|
|
|
|
|
public static void main(String args[]){
|
|
|
System.out.println(getRedisInfo("192.168.102.222",6379));
|
|
|
System.out.println(getRedisIsSlave("192.168.102.222", 6379, "192.168.102.222",6379));
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|