Cppzk
---- Zookeeper Library for C + + encapsulation
Features:
1. C + + package, easy to use interface;
2. Fewer files, a header file and a source file, whether it is compiled into a library or direct source file compilation is very convenient;
3. Support the node's create, set, get, support watch;
4. Only Linux is supported for the time being.
Compile
The boost header file is required because boost::function is used as the callback function.
Under Linux:
CD src
Make-f makefile.mk
Generate LIBCPPZK.A
How to use
#include "ZooKeeper.h"//contains header file ZooKeeper ZK;//define Object zk.init ("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183" );//Initialize, parameter is Zookeeper server address list, format: Ip:port,ip:port,... Zk.exists (path);//Determine if the node exists zk.createnode (path, data, recursive);//Recursive creation of nodes (Recursivce=false without recursive creation, Returns an error directly when the parent node does not exist) Zk.createephemeralnode (path, data, recursive);//recursive creation of ephemeral nodes Zk.createsequencenode ( Path, data, rpath, recursive); Recursive creation of the sequence node, rpath for the actual path returned //watch zk.watchdata (path, datacallback);//Watch node data, when the data changes, trigger callback function Zk.watchchildren (path, childrencallback); Watch child node, when the child node is added or deleted, trigger callback function// log zk.setlogstream (stderr);//Set Log stream Zk.setdebugloglevel (true); Turn on debug logging
The definition of Watch's callback:
Two kinds of callback, data callback and child node callback //Datawatchcallback return path and value of the path //Childrenwatchcallback return path and sub-section name under this path typedef boost::function<void (const std::string &path, const std::string &value) > datawatchcallback; typedef boost::function<void (const std::string &path, const std::vector<std::string> &value) > Childrenwatchcallback;
For clarity, the above code omits the definition of error handling and some variables, and the complete code can be found in src/test.cc
:https://github.com/fdxuwei/cppzk
Zookeeper Library for C + + encapsulation