Simple use of zookeeper in Java

1、 Fundamentals of zookeeper

The data model is as follows:

The structure of zookeeper data model is very similar to UNIX file system. On the whole, it can be regarded as a tree, and each node is called a znode. Each znode can be uniquely identified by its path, such as the first znode on the third layer in the above figure, whose path is / app1 / C1. A small amount of data can be stored on each znode (1m by default, which can be modified through configuration. Generally, it is not recommended to store a large amount 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, and the child node will not inherit the parent node.

Zookeeper properties:

1. Read, write (update) mode

In the zookeeper cluster, readers can read from any zookeeper server, which is the key to ensure the better reading performance of zookeeper; The write request will be forwarded to the leader first, and then the leader will broadcast the request to all followers through the atomic broadcast protocol in zookeeper. After receiving more than half of the write successful acks, the leader will consider the write successful, persist the write and tell the client that the write is successful.

2. Wal and snapshot

Like most distributed systems, zookeeper also has a wal (write ahead log). For each update operation, zookeeper will write the wal first, then update the data in memory, and then notify the client of the update result. In addition, zookeeper will periodically snapshot the directory tree in memory and land it on disk, which is similar to fsimage in HDFS. The main purpose of this is, of course, to persist the data, and to speed up the recovery after restart. If all of them are recovered in the form of replaywal, it will be relatively slow.

3、FIFO

For each zookeeper client, all operations follow FIFO order, which is guaranteed by the following two basic characteristics: first, the network communication between zookeeper client and server is based on TCP, which ensures the sequence of transmission packets between client / server; Second, zookeeper server executes client requests in strict FIFO order.

4、Linearizability

In zookeeper, all update operations have strict partial order relationship, and the update operations are executed serially, which is the key to ensure the correctness of zookeeper function.

2、 Common commands for 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 can delete this node by executing delete / qyktest.

In addition, we can continue to create child nodes under qyktest this node.

Well, let's talk about this person in a few basic commands. There are many other commands. You can check the information.

3、 The Java API operation of zookeeper

The Java API operation zookeeper is relatively simple. The author directly posts the code as follows:

Then, you can see that the console output is as follows:

Therefore, some public configurations can be saved in zookeeper, and then other services can be used

summary

The above is a simple use of zookeeper in Java introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>