This is some code example we have developed for the Apache ZooKeeper book. This code constitutes complementary material to the book and it has been written to illustrate how to implement an application with ZooKeeper. It hasn't been heavily tested or debugged, and it misses features, so don't take it as production-ready code. In fact, if you're able to fix bugs and extend this implementation, it probably means that you have learned how to program with ZooKeeper!
This example implements a simple master-worker system. There is a primary master assigning tasks and it supports backup masters to replace the primary in the case it crashes. Workers execute tasks assigned to it. The task consists of reading the content of the task znode, nothing more. A real app will most likely do something more complex than that. Finally, clients submit tasks and wait for a status znode.
Here is a summary of the code flow for each of the components:
We used maven for this little project. Install maven if you don't have it and run "mvn install" to generate the jar file and to run tests. We have a few tests that check for basic functionality. For the C master, you'll need to compile the ZooKeeper C client to use the client library.
To run it, follow these steps:
bin/zkServer.sh start
from a copyof the distribution package.
java -cp .:/usr/local/zookeeper-3.4.8/zookeeper-3.4.8.jar:/usr/local/slf4j-1.7.2/slf4j-api-1.7.2.jar:/usr/local/slf4j-1.7.2/slf4j-ext-1.7.2.jar:/usr/local/slf4j-1.7.2/slf4j-log4j12-1.7.2.jar:/usr/local/apache-log4j-1.2.17/log4j-1.2.17.jar:/path/to/book/repo/target/ZooKeeper-Book-0.0.1-SNAPSHOT.jar org.apache.zookeeper.book.Master localhost:2181
java -cp .:/usr/local/zookeeper-3.4.8/zookeeper-3.4.8.jar:/usr/local/slf4j-1.7.2/slf4j-api-1.7.2.jar:/usr/local/slf4j-1.7.2/slf4j-ext-1.7.2.jar:/usr/local/slf4j-1.7.2/slf4j-log4j12-1.7.2.jar:/usr/local/apache-log4j-1.2.17/log4j-1.2.17.jar:/path/to/book/repo/target/ZooKeeper-Book-0.0.1-SNAPSHOT.jar org.apache.zookeeper.book.Worker localhost:2181
java -cp .:/usr/local/zookeeper-3.4.8/zookeeper-3.4.8.jar:/usr/local/slf4j-1.7.2/slf4j-api-1.7.2.jar:/usr/local/slf4j-1.7.2/slf4j-ext-1.7.2.jar:/usr/local/slf4j-1.7.2/slf4j-log4j12-1.7.2.jar:/usr/local/apache-log4j-1.2.17/log4j-1.2.17.jar:/path/to/book/repo/target/ZooKeeper-Book-0.0.1-SNAPSHOT.jar org.apache.zookeeper.book.Client localhost:2181
For the C master, we do the following:
gcc -I/usr/local/zookeeper-3.4.8/src/c/include -I/usr/local/zookeeper-3.4.8/src/c/generated -DTHREADED -L/usr/local/lib -l zookeeper_mt master.c
./a.out 127.0.0.1:2181
Have fun!