Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement ZooKeeper distributed Lock by Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Java how to achieve ZooKeeper distributed lock, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

What is a distributed lock?

1. When we develop stand-alone applications involving concurrent synchronization, we often use synchronized or Lock to solve the problem of code synchronization between multiple threads. At this time, multi-threads run under the same JVM without any problems.

2. When our application is working in a distributed cluster, it belongs to the working environment of multi-JVM (multiple machines), and the synchronization problem can no longer be solved through multi-thread locks across JVM. Then a more advanced locking mechanism is needed to deal with data synchronization between processes across machines-- this is distributed locking. (multiple nodes acquire locks from distributed components)

Examples include the following:

All kinds of ticket-grabbing software clients acquire the lock through zookeeper. Finally, only one client can obtain the lock and communicate with the 12306 server first, and finally pass with the 12306 database server. The lock is released after the communication is complete, and other clients do the same after acquiring the lock. (obviously, the premise is that all these clients communicate with the same server in the node cluster after load balancing.)

3. Other distributed locks

Principle of zookeeper distributed Lock

Core idea: when the client wants to acquire the lock, create the node, and delete the node after using the lock.

1. When the client acquires the lock, a temporary sequential node is created under the lock node.

2. Then get all the child nodes under the lock. After obtaining all the child nodes, if the client finds that the sequence number of the child node he has created is the lowest, then the client is considered to have acquired the lock. When you finish using the lock, delete the node.

Note:

Reasons for creating temporary nodes:

If an outage occurs on a node that has acquired the lock, if the node is persisted, the lock cannot be released. If it is a temporary node, after the outage occurs, the connection disconnection meeting automatically releases the lock.

Reasons for creating sequential nodes:

The nodes are numbered sequentially, and the client determines whether it is the node with the lowest sequence number by the node number, and obtains the lock.

3. If you find that the node you created is not the smallest of all the child nodes of lock, it means that you have not acquired the lock. In this case, the client needs to find the node that is smaller than you, and register the event listener to listen for deletion events.

4. If it is found that the node smaller than its own has been deleted, the client's Watcher will receive the corresponding notification and determine again whether the created node has the lowest sequence number among the lock child nodes. If so, the lock has been acquired. If not, repeat the above steps to continue to obtain a node smaller than yourself and register the listener.

Thank you for reading this article carefully. I hope the article "how to implement ZooKeeper distributed locks in Java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report