First, the following two articles on .net/c# Zookeeper distributed locks:
The hyperlink login is visible.
The hyperlink login is visible.
In fact, the above two articles are wrong! Completely misleading programming developers!
AboutZookeeper installation tutorial:
The implementation principle of Zookeeper distributed lock is explained in simple terms:
1: Get all the child nodes under the locker
2: All sub-nodes are sorted, here are two situations
If your node has the smallest serial number, then the client is considered to have obtained the lock. You can do your own thing! If your node is not the smallest serial number, the node that listens to the smallest node is not the previous node that is smaller than you!!! Pay attention here! (Because, your previous node may time out and release the lock!) )
3: Wait for the Zookeeper notification, and then, repeat the operation of 2, pull all nodes, sort them, and judge whether you are the smallest node............
After we know the principle, let's take a look at why the top two introductions of the article are wrong?
Looking at the example of the order in the article, it feels correct, and the result after running is also correct! However, if you test it a few times, you will know that the code in the article is completely wrong!!!!
So, knowing the principle, let's write it ourselves! Anyway, it's not the first time I've been pitted by code on the Internet!
Here's the correct gif:
Calling Code:
The ZooKeeperNet.dll version used is 3.4.6.1
Finally, attach the source code:
Tourists, if you want to see the hidden content of this post, please Reply
|