Database Reference
In-Depth Information
The last line saves the process ID of the Java process running the program that adds
goat
as a member. We need to remember the ID so that we can kill the process in a moment,
after checking the members:
%
java ListGroup localhost zoo
goat
duck
cow
To remove a member, we kill its process:
%
kill $goat_pid
And a few seconds later, it has disappeared from the group because the process's
ZooKeeper session has terminated (the timeout was set to 5 seconds) and its associated
ephemeral node has been removed:
%
java ListGroup localhost zoo
duck
cow
Let's stand back and see what we've built here. We have a way of building up a list of a
group of nodes that are participating in a distributed system. The nodes may have no
knowledge of each other. A client that wants to use the nodes in the list to perform some
work, for example, can discover the nodes without them being aware of the client's exist-
ence.
Finally, note that group membership is not a substitution for handling network errors
when communicating with a node. Even if a node is a group member, communications
with it may fail, and such failures must be handled in the usual ways (retrying, trying a
different member of the group, etc.).
ZooKeeper command-line tools
ZooKeeper comes with a command-line tool for interacting with the ZooKeeper
namespace. We can use it to list the znodes under the
/zoo
znode as follows:
%
zkCli.sh -server localhost ls /zoo
[cow, duck]
You can run the command without arguments to display usage instructions.
Deleting a Group
To round off the example, let's see how to delete a group. The
ZooKeeper
class
provides a
delete()
method that takes a path and a version number. ZooKeeper will