|
|
package com.monitor.middleware.zookeeper;
|
|
|
|
|
|
import org.apache.zookeeper.KeeperException;
|
|
|
import org.apache.zookeeper.WatchedEvent;
|
|
|
import org.apache.zookeeper.Watcher;
|
|
|
import org.apache.zookeeper.ZooKeeper;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/6/21.
|
|
|
*/
|
...
|
...
|
@@ -17,9 +25,50 @@ public class ZookeeperMonitorTask { |
|
|
@Scheduled(fixedRate=10000)
|
|
|
public void run() {
|
|
|
log.info("task start...");
|
|
|
System.out.println("************************************************************");
|
|
|
List<String> ipList=new ArrayList<String>();
|
|
|
ipList.add("172.31.50.190");
|
|
|
ipList.add("172.31.50.191");
|
|
|
ipList.add("172.31.50.192");
|
|
|
ipList.add("172.31.50.193");
|
|
|
ipList.add("172.31.50.194");
|
|
|
ipList.add("10.66.4.3");
|
|
|
ipList.add("10.66.4.4");
|
|
|
ipList.add("10.66.4.5");
|
|
|
ipList.add("10.66.4.8");
|
|
|
ipList.add("10.66.4.9");
|
|
|
|
|
|
for(String ip:ipList){
|
|
|
boolean result=true;
|
|
|
try {
|
|
|
result=checkConnection(ip);
|
|
|
} catch (Exception e) {
|
|
|
result=false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
log.info("task end...");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
public static boolean checkConnection(String ip) throws IOException, KeeperException, InterruptedException {
|
|
|
ZooKeeper zk = new ZooKeeper("192.168.102.205:2181", 500000,
|
|
|
new Watcher() {
|
|
|
|
|
|
@Override
|
|
|
public void process(WatchedEvent event) {
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
}
|
|
|
});
|
|
|
// 获取某路径下所有节点
|
|
|
List<String> children = zk.getChildren("/", false);
|
|
|
for (String child : children)
|
|
|
{
|
|
|
System.out.println("this---->"+child);
|
|
|
}
|
|
|
System.out.println("--------get children ok-----------");
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|