Zookeeper is cumbersome for clients on Java, and watcher can only be triggered once. So with Zkclient and curator. The curator feature is that you can use the fluent style to write code.
Today the curator API is a simple use of a bit.
1.curator operation of the node requires curator-framework jar package
<dependency> <groupId>org.apache.curator</groupId> <artifactid>curator-frame Work</artifactid> <version>2.8.0</version> </dependency>
(String[] args) { string servers = retrypolicy retrypolicy = exponentialbackoffretry () curatorframework curator = curatorframeworkfactory. () . ConnectString (servers) .retrypolicy (Retrypolicy) .sessiontimeoutms () .build () Curator.start () { curator.create (). creatingParentsIfNeeded (). Inbackground (Backgroundcallback () { (curatorframework curatorframeworkcuratorevent curatorevent) Exception { system. println (curatorevent) System: println (Curatorframework)} }). ForPath (. GetBytes ())} (exception e) { e.printstacktrace ()} } mycallback backgroundcallback { (curatorframework curatorframeworkcuratorevent curatorevent) Exception { system. println (curatorevent) System: println (Curatorframework)} }
2. Monitoring of nodes and sub-nodes requires Curator-recipes jar report
org.apache.curatorcurator-recipes2.8.0
(String[] args) { String servers = Retrypolicy retrypolicy = exponentialbackoffretry () curatorframework curator = Curatorframeworkfactory. (Serversretrypolicy) Curator.start () { Pathchildrencache childcache = pathchildrencache (curator) Childcache.start () Childcache.getlistenable (). AddListener (Pathchildrencachelistener () { (curatorframework Curatorframeworkpathchildrencacheevent pathchildrencacheevent) Exception { system. println () System: println (Pathchildrencacheevent.tostring ())} }) timeunit. Sleep ()}&NBSP; (exception e) { E.printstacktrace ()} }
Use of the curator API