Objective
Zkclient has done two main things:
One is to automatically create a new zookeeper instance to re-connect when the session loss and session expire.
The other is to wrap the disposable watcher as a durable watcher. The latter is done simply by re-reading the data and then registering the same watcher instance in the watcher callback.
Zkclient has been used in a number of projects, well-known Dubbo, Kafka, Helix.
Zkclient jar Package Download, or add maven dependencies directly: Http://mvnrepository.com/artifact/com.101tec/zkclient
Maven dependencies:
<!--https://mvnrepository.com/artifact/com.101tec/zkclient -<Dependency> <groupId>Com.101tec</groupId> <Artifactid>Zkclient</Artifactid> <version>0.10</version></Dependency>
Body
1. Test class
Package Com.xbq.demo;import java.io.ioexception;import org. i0itec.zkclient.zkclient;import org. i0itec.zkclient.izkdatalistener;/** * @ClassName: Zkclientdemo * @Description: TODO (zkclient test) * @author XBQ * @date 2017-3-26 a.m. 11:49:39 */public class Zkclientdemo {//This demo uses a cluster, so there are multiple IPs and ports private static String Connect_server = "192.168.99.138:2181,192.168.99.138:2182,192.168.99.138:2183"; private static int session_timeout = 3000; private static int connection_timeout = 3000; private static zkclient zkclient; static {zkclient = new zkclient (Connect_server, Session_timeout,connection_timeout,new Myzkserializer ()); public static void Main (string[] args) {Add (zkclient);//update (zkclient);//delete (zkclient); Adddigui (zkclient);//Deletedigui (zkclient); Subscribe (zkclient); }/** * @Title: Add * @Description: TODO (add a specified node) * @param @param zkclient Set file * @return void return type * @throws */public static void Add (Zkclient zkclient) {//If no node exists, Create a new node if (!zkclient.exists ("/config")) {zkclient.createpersistent ("/config", "Javacoder"); }//Check to see if the increase succeeds String value = Zkclient.readdata ("/config"); System.out.println ("value===" + value); }/** * @Title: Addsequential * @Description: TODO (Create node recursively) * @param @param zkclient settings file * @r Eturn void return type * @throws */public static void Adddigui (Zkclient zkclient) {//Recursive create node Zkclie Nt.createpersistent ("/xbq/java/coder", true); if (zkclient.exists ("/xbq/java/coder")) {System.out.println ("Add success!") "); }else {System.out.println ("Increase failed! "); }}/** * @Title: Delete * @Description: TODO (delete specified node) * @param @param zkclient settings file * @ret urn void return type * @throws */public static void Delete (ZKCLIEnt zkclient) {//Presence node is removed if (zkclient.exists ("/config")) {Boolean flag = Zkclient.delete ("/conf IG "); System.out.println ("delete" + (flag = = true?) Success ":" Failed! ")); }}/** * @Title: Deletedigui * @Description: TODO (Recursive delete) * @param @param zkclient settings file * @ return void returns type * @throws */public static void Deletedigui (Zkclient zkclient) {//Presence node for deletion The IF (zkclient.exists ("/XBQ")) {/////Recursive delete is only passed to the parent node, and if all nodes are passed in, the return is true, but still not deleted,//Because Zkclient will The exception is encapsulated, and when it enters the catch, it returns True, which is a pit Boolean flag = zkclient.deleterecursive ("/XBQ"); System.out.println ("delete" + (flag = = true?) Success ":" Failed! ")); }}/** * @Title: Update * @Description: TODO (Modify the value of the node) * @param @param zkclient settings file * @ret urn void return type * @throws * * public static void update (Zkclient zkclient) {if (Zkclient.exists ("/config ")){ Zkclient.writedata ("/config", "testupdate"); Check to see if the modification succeeds String value = Zkclient.readdata ("/config"); System.out.println ("value===" + value); }}/** * @Title: Subscribe * @Description: TODO (Event subscription, can be used for configuration management) * Subscribe first, then operate the add and revise. (Can subscribe multiple clients) * @param @param zkclient settings file * @return void return type * @throws */public static void S Ubscribe (zkclient zkclient) {zkclient.subscribedatachanges ("/config/username", new Izkdatalistener () {@ Override public void handledatadeleted (String arg0) throws Exception {System.out.println ("triggered delete Event: "+ arg0); } @Override public void Handledatachange (String arg0, Object arg1) throws Exception { System.out.println ("Triggered a Change event:" + arg0 + "-" + arg1); } }); try {System.in.read (); } catch (IOException e) {e.printsTacktrace (); } }}
2. Custom Serialization Classes
Package Com.xbq.demo;import org. i0itec.zkclient.exception.zkmarshallingerror;import org. I0itec.zkclient.serialize.zkserializer;import java.io.unsupportedencodingexception;/** * @ClassName: Myzkserializer * @Description: TODO (Implement serialization interface, convert to UTF-8 encoding) * @author XBQ * @date 2017-3-26 a.m. 11:56:22 */ public class Myzkserializer implements zkserializer{ @Override public byte[] Serialize (Object data) throws Zkmarshallingerror { try { return string.valueof (data). GetBytes ("UTF-8"); } catch ( Unsupportedencodingexception e) { e.printstacktrace (); } return null; } @Override Public Object deserialize (byte[] bytes) throws Zkmarshallingerror { try { return new String ( Bytes, "UTF-8"); } catch (Unsupportedencodingexception e) { e.printstacktrace (); } return null;} }
ZooKeeper (iv)--use of third-party client Zkclient