[Reprint] Zookeeper Distributed lock service

Source: Internet
Author: User

Reprinted from Http://www.cnblogs.com/shanyou/archive/2012/09/22/2697818.html

Distributed lock service may not be used in many of our projects, because everyone puts exclusivity in the database layer to block. When a large number of row locks, table locks, transactions are flooded with the database. General Web application A lot of bottlenecks are on the database, here is to introduce a solution to reduce the database lock burden, the use of zookeeper distributed lock service.

Zookeeper is a sub-project under Hadoop that coordinates some of the distributed frameworks associated with Hadoop, such as Hadoop, Hive, pig, and so on, in fact they are animals, so called zookeeper--"zoo Keeper". There are certainly a lot of animals in the zoo, visitors can see the various types of animals according to the guide map provided by the zoo, rather than the passive objects of stricken in the primitive jungle. In order for the various animals to stay where they should be, instead of stopping by each other, or fighting each other, the zoo keeper needs to classify and manage the animals according to their various habits, so that we can be more assured of safe viewing of animals. Back to our enterprise application system, with the continuous improvement of the level of information technology, our enterprise-class system has become more and more bulky, performance dropped sharply, customer complaints frequently. The split system is the only effective method we can choose to solve the scalability and performance problems of the system at the moment. But the split system also brings the complexity of the system-the subsystems do not exist in isolation, they need to collaborate and interact with each other, which is what we often call distributed systems. Each subsystem is like a zoo animal, in order to enable each subsystem to provide users with a unified service, must need a mechanism to coordinate-this is zookeeper--Zoo Administrator.

Zookeeper is essentially a distributed small file storage System. Originally a component of Apache Hadoop, it is now split into a separate sub-project for Hadoop, and zookeeper clusters are also used in HBase, another split sub-project in Hadoop, a DBMS for large amounts of data in a distributed environment. Zookeeper has the following characteristics:

1) Simple

The zookeeper core is a streamlined file system that provides simple file operations as well as additional features such as sorting and notifications.

2) Easy expression

Zookeeper's data structure prototype is a znode tree (Linux-like file system), and they are already built blocks that can be used to build large collaborative data structures and protocols.

3) High Availability

Zookeeper can run on a set of servers, and they are designed to be highly available to avoid a single point of failure for your application.

4) Loose coupling interaction

The watcher mechanism provided by zookeeper makes the interaction between client and server more loosely coupled, and each client can interact with other clients without needing to know the presence of other clients.

5) Rich API

Zookeeper provides developers with a rich set of APIs that ease the burden of developers writing generic protocols.

Zookeeper in fact, each node in the cluster maintains the same tree, the structure of the tree is similar to the concept of the Linux directory structure, with/for the node, the bottom can expand any node and leaf nodes, each node can write data. The implementation of distributed locks based on zookeeper, in fact, thanks to the zookeeper synchronization files, we believe that every time we visit the Zookeeper tree, the same node returns the data are consistent. This is achieved by some algorithms within the zookeeper. Especially leader's election algorithm.

Official Document: Http://zookeeper.apache.org/doc/r3.3.2/zookeeperOver.html#ch_DesignOverview

Download: http://zookeeper.apache.org/releases.html

C # Zookeeper clients still have some compatibility with Zookeeper's latest version 3.4.3 Https://github.com/ewhauser/zookeeper/branches

The data for each node of the zookeeper cluster is consistent, so we can use these nodes as flags for the lock.

First set the API for the lock, at least to include, lock (lock), unlock (unlock), isLocked (Lock) Three methods, and then we can create a factory (lockfactory), used to specialize in the production of locks. The process of creating a lock is described as follows:

Premise: Each lock requires a path to be specified (for example:/geffzhang/lock)

1. Based on the specified path, find out whether this node exists under the Zookeeper cluster. (indicates that a lock has been made)

2. If present, according to some characteristic data of the query (such as IP address/hostname), the current lock is not the query person's

3. If it is not the caller's lock, then NULL is returned, indicating that the lock creation failed

4. If it is the caller's lock, return this lock to the Finder

5. If the node does not exist, it does not currently have a lock, then creates a temporary node and writes the query's feature information to the node's data and returns the lock.

According to the above 5, a distributed lock can be created.

There are three types of locks created:

1. Creation failed (null), indicating that the lock was used by other respondents. '

2. Created successfully, but not currently locked (unlocked), you can use

3. The creation was successful, but it is currently locked (locked) and cannot continue to lock.

Zookeeper Client Programming

Distributed lock Service

Distributed Service Framework Zookeeper--managing data in a distributed environment

A distributed lock based on zookeeper implementation

Hangzhou ADC Technology Carnival two days summary-soa, go c

Zookeeper. Net Client

Zookeeper Client Profile

Application of Zookeeper in Gateway

Zookeeper Configuration (ii)

Zookeeper installation Configuration (iii)

[Reprint] Zookeeper Distributed lock service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.