The recent collation of information found two years ago I wrote some examples of zookeeper, today sorted out, put here, perhaps later.
First, prepare a zookeeper cluster environment, where you use a stand-alone simulation cluster environment and start the service in code mode.
Zookeeper Service
This assumes that starting three zookeeper services do cluster
Package my.zookeeperstudy;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.zookeeper.server.quorum.QuorumPeerConfig;
Import Org.apache.zookeeper.server.quorum.QuorumPeerMain;
Import Java.io.File;
Import java.net.InetAddress;
Import java.util.Properties;
public class Zkserver {
protected String id = null;
protected String datadir = null;
protected String clientport = null;
Public zkserver (string ID, String datadir, String clientport) {
This.id = ID;
This.datadir = DataDir;
This.clientport = ClientPort;
}
public void StartServer () {
New Thread (New Runnable () {
@Override
public void Run () {
try {
Properties Props = new properties ();
Props.setproperty ("Ticktime", "2000");
Props.setproperty ("DataDir", DataDir);
Fileutils.write (New File (Props.getproperty ("DataDir"), "myID"), id);
Props.setproperty ("ClientPort", ClientPort);
Props.setproperty ("Initlimit", "10");
Props.setproperty ("Synclimit", "5");
String hostname = Inetaddress.getlocalhost (). GetHostName ();
Props.setproperty ("Server.1", hostname + ": 2,881:3,881");
Props.setproperty ("Server.2", hostname + ": 2,882:3,882");
Props.setproperty ("server.3", hostname + ": 2,883:3,883");
Quorumpeerconfig quorumconfig = new Quorumpeerconfig ();
Quorumconfig.parseproperties (props);
Quorumpeermain Quorumpeermain = new Quorumpeermain ();
Quorumpeermain.runfromconfig (Quorumconfig);
catch (Exception e) {
E.printstacktrace ();
}
}
). Start ();
}
}
Package my.zookeeperstudy;
public class ZKServer1 {
public static void Main (string[] args) throws Exception {
Zkserver zkserver = new Zkserver ("1", "/tmp/zookeeper1/data", "2181");
Zkserver.startserver ();
}
}
Package my.zookeeperstudy;
public class ZKServer2 {
public static void Main (string[] args) throws Exception {
Zkserver zkserver = new Zkserver ("2", "/tmp/zookeeper2/data", "2182");
Zkserver.startserver ();
}
}
Package my.zookeeperstudy;
public class ZKServer3 {
public static void Main (string[] args) throws Exception {
Zkserver zkserver = new Zkserver ("3", "/tmp/zookeeper3/data", "2183");
Zkserver.startserver ();
}
}
Testing the Client class
Here are three clients to simulate the election.
Package my.zookeeperstudy.election;
Import org.apache.zookeeper.*;
public class Zkclient {
protected String id = null;
protected String clientport = null;
protected String Path = "/myapp_leader";
Public zkclient (string ID, string clientport) {
This.id = ID;
This.clientport = ClientPort;
}
protected void Startclient () throws Exception {
Zookeeper ZK = new Zookeeper ("localhost:" + clientport, 10000,
New Watcher () {
public void process (Watchedevent event) {
System.out.println ("event:" + Event.gettype ());
}
});
while (true) {
byte[] leader = null;
try {
Leader = Zk.getdata (path, true, NULL);
catch (Exception e) {
System.out.println ("The leader is null.");
}
if (leader = = null) {
try {
Zk.create (Path, this.id.getBytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE, createmode.ephemeral);
catch (Exception e) {
Ignore me
}
} else {
System.out.println ("The leader is:" + new String (leader));
}
Thread.Sleep (1 * 1000);
}
}
}
Package my.zookeeperstudy.election;
public class ZKClient1 {
public static void Main (string[] args) throws Exception {
Zkclient zkclient = new Zkclient ("1", "2181");
Zkclient.startclient ();
}
}
Package my.zookeeperstudy.election;
public class ZKClient2 {
public static void Main (string[] args) throws Exception {
Zkclient zkclient = new Zkclient ("2", "2182");
Zkclient.startclient ();
}
}
Package my.zookeeperstudy.election;
public class ZKClient3 {
public static void Main (string[] args) throws Exception {
Zkclient zkclient = new Zkclient ("3", "2183");
Zkclient.startclient ();
}
}
Test
Start Zkserver1,zkserver2 and ZKServer3 first, the boot process will have errors, can be ignored, it is because the detection of other nodes when the connection failed, the production environment can be specific to ignore such errors.
Then start Zkclient1,zkclient2 and ZKClient3 in turn.
Observe three client log outputs, respectively.
Stop the current election to the leader, such as ZKClient1, and then observe the log output, after a period of time will be the re-election of a leader.