In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the advanced features of zookeeper". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the advanced features of zookeeper are.
One and three concepts
1.CreateMode
You can set the type of znode when you create
There are four main types:
PERSISTENT (persistent, as opposed to EPHEMERAL, does not disappear with the disconnection of client)
PERSISTENT_SEQUENTIAL (persistent and sequential)
EPHEMERAL (short, life cycle dependent on client session)
EPHEMERAL_SEQUENTIAL (ephemeral, sequential)
2.Watcher
Watcher is a reverse push mechanism, that is, zonde (including its child) notifies the client when it changes.
You can customize Watcher and register with zonde.
Watcher is divided into two categories: data watches and child watches. The former listens for changes in data, while the latter listens for changes in sub-node.
Watcher is disposable! Once called, you need to re-register.
3.ACL
Acl is access control. Zookeeper controls permissions through the ACL mechanism. You can specify when you create a znode. As we said earlier, a set of zookeeper will be used by multiple programs. Just like linux supports multiple users. So you need to have a set of permission control: otherwise, if the node you create is deleted by other applications for no reason, who will you go to?
Permission level of Zookeeper:
READ: allows you to get the value of this node and list child nodes.
WRITE: allows you to set the value of this node.
CREATE: allows you to create child nodes.
DELETE: child nodes can be deleted.
ADMIN: Super privilege. Equivalent to root
Enhanced from top to bottom, the subsequent permissions include the previous permissions.
Many of the advanced features of zookeeper are based on the above three concepts. Especially CreateMode and Watcher.
II. Java client
Zookeeper officially provides the java client. The interface provided is also more basic. The only thing that is stronger than the telnet client used in the previous test is that we can implement the extended business with the Watcher interface in the code! You can't do that on the command line.
Maven dependencies:
Org.apache.zookeeper zookeeper 3.4.6
Here is a list of some important interfaces.
The interfaces are intuitive and the description is detailed. There will be no additional instructions.
Third, the implementation principle of some advanced features.
1.Name Service: a bit like JNDI, tree directories are inherently globally unique names. This feature is of little use in practice, and there are many alternatives. A little.
two。 Configure push: for example, if a front-end website has 100 machines, what we need to do is to have a main program and zookeeper on each machine and register the Watcher. Write the configuration locally to zookeeper, and zookeeper will automatically push the configuration to these machines through Watcher. There is no need to update it manually. (equivalent to the script of operation and maintenance. )
3. Cluster Management:
After each Server gets up, create a znode of type EPHEMERAL on the Zookeeper, assuming that they all have a common parent GroupMembers! We call the getChildren (GroupMembers) method on each Server and register a Child Watcher. Because it is the znode of EPHEMERAL, when the Server that created it dies, the znode will be deleted, so the Children will change, and the Watcher on the getChildren will be called, so the other Server will know that some Server has died. The same principle applies to the new Server.
4.Leader election
Basically the same as the design of cluster management, the difference is that the znone type created this time is EPHEMERAL_SEQUENTIAL, not only with the birth and death of member, but also in order. As long as we think of the lowest numbered member as Master, we can achieve the dynamic election of Leader!
5. Global lock
Zookeeper can take advantage of its excellent data consistency algorithm to provide reliable global locking services.
Getting the lock is a recursive process.
1. Create znode "/ distributed_lock"
two。 Create a new child node "/ distributed_lock/xxxxxx" under distributed_lock, EPHEMERAL_SEQUENTIAL mode, if the current serial number is I.
3. Call getChildren () on distributed_lock and acquire the lock if I is the smallest in the children list; otherwise proceed to step 4
4. Wait for the notification (exists () method) of the deletion of the node immediately after I in the children list. Write it down as j. And j depends on a node k that is only smaller than j. Has been recursively waiting for the smallest znode to be deleted.
Note: although EPHEMERAL_SEQUENTIAL is incremental, it is still not rude to think of the node j=i-1 immediately following I. This is because the order in which locks are released is not exactly in the order of nodes!
Release the lock:
Just delete the child node created by yourself.
6. Distributed queue
Similar to the design of the global lock above. Just make sure that the number is the smallest every time you spend it. You can do first-in, first-out.
7 . Slightly depending on the type of node created by zookeeper, and then combined with the Watcher feature. There are many other features that can be provided. For example, various types of queue, various types of Lock (only write locks are introduced above), barriers, semaphore Semaphore atomic type AtomicInteger and so on.
All of these are distributed and highly reliable.
More and more features are implemented in netflix's open source zookeeper client Curator. As an ordinary developer, using Curator directly is the most efficient!
Thank you for your reading, the above is the content of "what are the advanced features of zookeeper". After the study of this article, I believe you have a deeper understanding of what the advanced features of zookeeper have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.