A very simple example of the use is to listen for changes in the data of a node in zookeeper, see comments in the Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingzookeepernet;namespacezookeeperdemo{/// <summary> ///Listener Implementation Class/// </summary> Public classWatcher:iwatcher { Public voidProcess (watchedevent @event) {if(@event. Type = =eventtype.nodedatachanged) {Console.WriteLine (string. Format ("node: {0}, data changed ...", @event. Path)); Zkhelper.nodedatachangelisten (@event. Path); } } } /// <summary> ///Zookeeper Operation Helper class/// </summary> Public classZkhelper {/// <summary> ///control ZK has only one instance/// </summary> Public StaticZooKeeper ZK =NewZooKeeper ("192.168.1.136:2181",NewTimeSpan (0,0,0, the),Newwatcher ()); /// <summary> ///listener Specifies zookeeper specified node data changes/// </summary> /// <param name= "Nodepath" ></param> Public Static voidNodedatachangelisten (stringNodepath) { //call ZK's GetData () method to fetch data, and callback when the data changes watcher byte[] Byts = ZK. GetData (Nodepath,true,NULL); //print out the changed dataConsole.WriteLine (string. Format ("fetch data to node {0} is: {1}", Nodepath, Encoding.Default.GetString (Byts))); } } classProgram {Static voidMain (string[] args) { //Operation Steps//0. Import operation Zookeeper related class library (NuGet mode), Install-package zookeepernet//1. Running the current console program//2. Connect to zkcli.sh in Linux//3. Use the command line to modify the data of the/root/childtwo node in zookeeper in Linux//4. The command to modify the data is Set/root/childtwo hello888888//5. The current console program monitors changes to the/root/childtwo node data and prints the changed data to the console//6. Repeat the 5th and bottom 6 steps above, the current console data will change continuouslyZkhelper.nodedatachangelisten ("/root/childtwo"); Console.readkey (); } }}
The final effect is as follows:
. NET client listens for zookeeper node data changes