ZooKeeper is a distributed, open source distributed application coordination service. It is an open source implementation of Google's Chubby and an important component of Hadoop and Hbase. This article will share with you the simple use of zookeeper in java. Friends who need it can refer to it
1. The basic principles of zookeeper
data model , as follows:
The structure of the ZooKeeper data model is very similar to the Unix file system. It can be regarded as a tree as a whole, and each node is called Make a ZNode. Each ZNode can be uniquely identified by its path. For example, the first ZNode in the third layer in the above figure has a path of /app1/c1. A small amount of data can be stored on each ZNode (the default is 1M, which can be modified through configuration. It is generally not recommended to store large amounts of data on ZNode). This feature is very useful. In addition, each ZNode also stores its Acl information. It should be noted here that although the tree structure of ZNode is very similar to the Unix file system, its Acl is completely different from the Unix file system. The Acl of each ZNode is independent. , the child node will not inherit the parent node.
ZooKeeper features:
##1. Read and write (update) mode
2, WAL and Snapshot
3. FIFO
4. Linearizability
2. Common commands of zookeeper
We can execute zookeeper-client or /opt/cloudera/parcels/CDH-5.0.0 -1.cdh5.0.0.p0.47/lib/zookeeper/bin/zkCli.sh-server localhost, enter the zookeeper command line, as follows:3. Zookeeper’s javaapi operation
Regarding the Javaapi operation of zookeeper, it is relatively simple. The author directly posted the code, as follows:packageorg.zookeeper.demo; importjava.io.IOException; importjava.util.concurrent.CountDownLatch; importorg.apache.zookeeper.CreateMode; importorg.apache.zookeeper.KeeperException; importorg.apache.zookeeper.WatchedEvent; importorg.apache.zookeeper.Watcher; importorg.apache.zookeeper.Watcher.Event.KeeperState; importorg.apache.zookeeper.ZooDefs.Ids; importorg.apache.zookeeper.ZooKeeper; publicclassZookeeperClientimplementsWatcher{ //连接超时时间,10s privatestaticfinalintSESSION_TIMEOUT= 10000; //连接的zookeeperserver privatestaticfinalStringCONNECTION_STRING = "172.31.25.8:2181"; privatestaticfinalStringZK_PATH = "/qyktest"; privateZooKeeperzk = null; privateCountDownLatchconnectedSemaphore = newCountDownLatch(1); publicvoidcreateConnection(StringconnectString, intsessionTimeout){ this.releaseConnection(); try{ zk= newZooKeeper(connectString,sessionTimeout, this); connectedSemaphore.await(); }catch(InterruptedExceptione) { System.out.println("连接创建失败,发生InterruptedException"); e.printStackTrace(); }catch(IOExceptione) { System.out.println("连接创建失败,发生IOException"); e.printStackTrace(); } } publicvoidreleaseConnection(){ if(this.zk!= null){ try{ this.zk.close(); }catch(InterruptedExceptione) { e.printStackTrace(); } } } publicbooleancreatePath(Stringpath, String data) { try{ Stringresult = this.zk.create(path,data.getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); System.out.println("节点创建成功,Path: "+result + ", content: "+data); }catch(KeeperExceptione) { System.out.println("节点创建失败,发生KeeperException"); e.printStackTrace(); }catch(InterruptedExceptione) { System.out.println("节点创建失败,发生InterruptedException"); e.printStackTrace(); } returntrue; } publicStringreadData(Stringpath) { try{ System.out.println("获取数据成功,path:"+path); returnnewString(this.zk.getData(path,false,null)); }catch(KeeperExceptione) { System.out.println("读取数据失败,发生KeeperException,path:"+path); e.printStackTrace(); return""; }catch(InterruptedExceptione) { System.out.println("读取数据失败,发生InterruptedException,path: "+path); e.printStackTrace(); return""; } } publicbooleanwriteData(Stringpath, String data) { try{ System.out.println("更新数据成功,path:"+path + ", stat: "+this.zk.setData(path,data.getBytes(), -1)); }catch(KeeperExceptione) { System.out.println("更新数据失败,发生KeeperException,path:"+path); e.printStackTrace(); }catch(InterruptedExceptione) { System.out.println("更新数据失败,发生InterruptedException,path: "+path); e.printStackTrace(); } returnfalse; } publicvoiddeleteNode(Stringpath) { try{ this.zk.delete(path,-1); System.out.println("删除节点成功,path:"+path); }catch(KeeperExceptione) { System.out.println("删除节点失败,发生KeeperException,path:"+path); e.printStackTrace(); }catch(InterruptedExceptione) { System.out.println("删除节点失败,发生InterruptedException,path: "+path); e.printStackTrace(); } } publicstaticvoidmain(String[]args) { ZookeeperClientsample = newZookeeperClient(); //获取连接 sample.createConnection(CONNECTION_STRING,SESSION_TIMEOUT); //读数据 Stringqyk = sample.readData("/qyktest"); System.out.println("qyk:"+qyk); Stringurl = sample.readData("/qyk/db/url"); System.out.println("url"+url); Stringdriver = sample.readData("/qyk/db/driver"); System.out.println("driver"+driver); StringuserName = sample.readData("/qyk/db/userName"); System.out.println("userName"+userName); Stringpassword = sample.readData("/qyk/db/password"); System.out.println("password"+password); //创建节点 sample.createPath(ZK_PATH,"我是节点初始内容"); System.out.println("数据内容:"+sample.readData(ZK_PATH) + "\n"); //更新节点 sample.writeData(ZK_PATH,"更新后的数据"); System.out.println("数据内容:"+sample.readData(ZK_PATH) + "\n"); //删除节点 sample.deleteNode(ZK_PATH); //释放连接 sample.releaseConnection(); } @Override publicvoidprocess(WatchedEventevent) { System.out.println("收到事件通知:"+event.getState() + "\n"); if(KeeperState.SyncConnected== event.getState()) { connectedSemaphore.countDown(); } } }
Then, when executed, you can see that the console output is as follows:
So, like some We can save the public configuration in zookeeper, and then other services can use it
Summary
The above is the detailed content of Detailed explanation of the use of zookeeper in java. For more information, please follow other related articles on the PHP Chinese website!