Monitor whether a zookeeper instance in the zookeeper cluster is down
/ * *
* listen to whether a Zookeeper instance in the Zookeeper cluster is down
* @ author Administrator
*
, /
Public class ZookeeperServiceStatusTest {
Private static final String HOST = "192.168.0.128"
Private static final Integer PORT = 2182
@ Test
Public void serverStatus () throws IOException {
Socket sock = null
BufferedReader reader = null
String cmd = "status"
Try {
Sock = new Socket (HOST, PORT)
OutputStream outstream = sock.getOutputStream ()
/ / obtain the status of the server through the status command of Zookeeper
Outstream.write (cmd.getBytes ())
Outstream.flush ()
Sock.shutdownOutput ()
Reader = new BufferedReader (new InputStreamReader (sock.getInputStream ()
String line
While ((line = reader.readLine ())! = null) {
/ / if (line.indexOf ("Mode:")! =-1) {
/ / System.out.println (line.replaceAll ("Mode:", ") .trim ()
/ /}
System.out.println (line)
}
} catch (Exception e) {
System.out.println ("Zookeeper address:" + HOST + ":" + PORT + "connection failed" + "msg:" + ExceptionUtils.getStackTrace (e))
} finally {
If (sock! = null) {
Sock.close ()
}
If (reader! = null) {
Reader.close ()
}
}
}
}