Home  >  Article  >  Java  >  Detailed explanation of the use of zookeeper in java

Detailed explanation of the use of zookeeper in java

黄舟
黄舟Original
2017-09-28 09:37:565634browse

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

In the ZooKeeper cluster, reads can be read from any ZooKeeperServer. This is the key to ensuring better read performance of ZooKeeper; write requests will first be forwarded to the Leader, and then the Leader will pass through the ZooKeeper The atomic broadcast protocol broadcasts the request to all Followers. After the Leader receives more than half of the Acks of successful writing, it considers that the writing is successful, and will persist the writing and tell the client that the writing is successful.

2, WAL and Snapshot

Like most distributed systems, ZooKeeper also has WAL (Write-Ahead-Log), for each update operation , ZooKeeper will first write WAL, then update the data in memory, and then notify the Client of the update results. In addition, ZooKeeper will regularly take snapshots of the directory tree in the memory and store it on the disk. This is similar to the FSImage in HDFS. The main purpose of doing this is, of course, the persistence of data, and the second is to speed up the recovery after restart. If all recovery is done through ReplayWAL, it will be slower.

3. FIFO

For each ZooKeeper client, all operations follow FIFO order. This feature is driven by the following two This is guaranteed by several basic features: first, the network communication between ZooKeeper Client and Server is based on TCP, and TCP guarantees the order of transmission packets between Client/Server; second, ZooKeeper Server executes client requests in strict accordance with FIFO order.

4. Linearizability

In ZooKeeper, all update operations have a strict partial order relationship, and update operations are executed serially. This One point is the key to ensuring the correct functionality of ZooKeeper.

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:



Then, execute ls / and you can see:




Then, we can execute create /qyktest'qyktest' to create a node, As follows:



Then, we execute get /qyktest to obtain the node value, as follows:




Then, we can execute set /qyktest'111' to modify the value of the node, as follows:




Finally, we execute delete /qyktest to delete this node.


In addition, we can also continue to create sub-nodes under the qyktest node.


Okay, I will talk about this person with a few basic commands. There are many other commands. You can check the information.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn