The Distributed Lock service may not be used much in everyone's projects, because everyone puts exclusive to the database layer. When a large number of row locks, table locks, and transactions flood the database. In general, many bottlenecks of web applications are on the database. Here we will introduce a solution to reduce the burden of database locks by using the zookeeper Distributed Lock service.
Zookeeper is a subproject under hadoop. It is used to coordinate distributed frameworks related to hadoop, such as hadoop, hive, and pig. In fact, they are all animals, so it is called zookeeper-"zoo administrator ". There are certainly a lot of animals in the zoo. Visitors can view various types of animals in different venues based on the Wizard provided by the zoo, rather than watching animals as though they were walking in the original jungle. In order to let different animals stay where they should be, rather than connecting to each other or killing each other, the zoo administrator needs to classify and manage the animals according to their habits, in this way, we can watch animals safely. Back to our enterprise-level application system, as the level of information technology continues to improve, our enterprise-level systems become increasingly bloated, with a sharp decline in performance and frequent complaints from customers. Splitting a system is the only effective solution to system scalability and performance problems. However, splitting systems also brings about system complexity. subsystems do not exist in isolation and need to collaborate and interact with each other. This is what we often call distributed systems. Every subsystem is like an animal in a zoo. In order for each subsystem to normally provide unified services for users, a mechanism must be required for coordination-Zookeeper-The zoo administrator.
Zookeeper is essentially a distributed small file storage system. It was originally a component of Apache hadoop and is now split into an independent sub-project of hadoop, zookeeper cluster is also used for DBMS with a large data volume in a distributed environment. Zookeeper has the following features:
1) Simple
Zookeeper is a streamlined file system. It provides some simple file operations and additional functions, such as sorting and notification.
2) Easy expression
The data structure prototype of zookeeper is a znode tree (similar to a Linux File System), and they are some blocks that have been built and can be used to build large collaborative data structures and protocols.
3) High Availability
Zookeeper can run on a group of servers and is designed to provide high availability for your applications.ProgramAvoid spof.
4) loosely coupled interaction
The Watcher mechanism provided by zookeeper makes the interaction between clients and servers loosely coupled. Each client can interact with other clients without having to know the existence of other clients.
5) rich APIs
Zookeeper provides developers with a rich set of APIS, reducing the burden on developers to write common protocols.
In fact, Zookeeper maintains the same tree for each node in the cluster. The structure of the tree is similar to that of the Linux directory structure, any nodes and leaf nodes can be expanded below, and each node can write data. the implementation of distributed locks Based on zookeeper is actually due to the robustness of zookeeper file synchronization. We believe that when we access the zookeeper tree every moment, the data returned by the same node is consistent. this depends on some internal zookeeperAlgorithmEspecially the Leader Election Algorithm.
Official documents: http://zookeeper.apache.org/doc/r3.3.2/zookeeperOver.html#ch_DesignOverview
Download: http://zookeeper.apache.org/releases.html
C # zookeeper client or some and zookeeper Latest Version 3.4.3 compatible https://github.com/ewhauser/zookeeper/branches
The data of each node in the zookeeper cluster is consistent, so we can use these nodes as the mark of the lock.
First, set the lock API to include at least three methods: Lock, unlock, and islocked. Then we can create a factory ), used to produce locks. the lock creation process is described as follows:
Premise: Each lock must be specified by a path (for example,/geffzhang/lock)
1. Check whether the node in the zookeeper cluster exists based on the specified path (it indicates that the node has been locked)
2. If yes, check whether the current lock is the same as that of the queryer based on some characteristic data (such as IP Address/hostname) of the queryer.
3. If it is not the queryer' lock, null is returned, indicating that the lock creation failed.
4. If the lock is from the queryer, return the lock to the queryer.
5. If the node does not exist, there is no lock currently. Create a temporary node, write the queryer' feature information to the data of the node, and return the lock.
Based on the above five data parts, a distributed lock can be created.
The created lock has three statuses:
1. The creation failed (null), indicating that the lock was used by another querymaker .'
2. The instance is successfully created but is not locked. You can use
3. The lock is successfully created, but it is locked.
Zookeeper client Programming
Distributed Lock Service
Distributed service framework zookeeper-manage data in a distributed environment
Distributed locks implemented based on zookeeper
Conclusion of the Hangzhou ADC technology Carnival-SOA, go to C
Zookeeper. Net Client
Introduction to zookeeper Client
Build a large-scale Configuration System Based on zookeeper
Application of zookeeper in Gateway
Zookeeper configuration (2)
Zookeeper installation configuration (3)