In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to create ZooKeeper shared locks". In daily operation, I believe many people have doubts about how to create ZooKeeper shared locks. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to create ZooKeeper shared locks". Next, please follow the editor to study!
As we all know, in Java development, it is very simple to create a single-process lock, and the lock function can be realized by using the java.util.concurrent.locks.Lock interface that comes with JDK.
Now it has come to the era of big data and cloud, and this kind of lock can no longer meet the needs of inter-cluster service locking. This article will briefly talk about how to use ZooKeeper to implement shared locking. As for what is a shared lock? It's also simple to understand: it means using the same lock in multiple instances. I seem to be talking nonsense. )
Also shining is our Curator Framework, which encapsulates locks for various scenarios, such as shared locks, read-write locks, revocable locks, and so on.
LockStartup.java
Package org.bigmouth.common.zookeeper.shared; import java.util.concurrent.TimeUnit; import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;import org.bigmouth.common.zookeeper.config.ZooKeeperFactory; public class LockStartup {public static void main (String [] args) throws Exception {CuratorFramework client = ZooKeeperFactory.get () / / ZooKeeperFactory in the previous article "ZooKeeper Learning Notes-configuration Management", final InterProcessSemaphoreMutex processSemaphoreMutex = new InterProcessSemaphoreMutex (client, "/ lock"); printProcess (processSemaphoreMutex); System.out.println ("Starting get lock..."); boolean flag = processSemaphoreMutex.acquire (12, TimeUnit.SECONDS); System.out.println (flag? "Getting lock successful.": "Getting failed!"); printProcess (processSemaphoreMutex); Thread.sleep (20 * 1000); if (processSemaphoreMutex.isAcquiredInThisProcess ()) {processSemaphoreMutex.release ();} printProcess (processSemaphoreMutex); client.close () } private static void printProcess (final InterProcessSemaphoreMutex processSemaphoreMutex) {/ / whether the lock is activated (executing) System.out.println ("isAcquiredInThisProcess:" + processSemaphoreMutex.isAcquiredInThisProcess ());}}
Start LockStartup and get the following result:
Then start another instance of LockStartup, and the result is that the lock cannot be acquired. The program still waits for 12 seconds, and ends if the lock cannot be acquired after 12 seconds:
If we set the processing time to 10 seconds, the wait time is 12 seconds. Then when the first process is released, the second process will acquire the lock.
Thread.sleep (10 * 1000)
There are no examples of shared locks suitable for other scenarios, but they are all similar to each other. You can take a look at the implementation class of interface org.apache.curator.framework.recipes.locks.InterProcessLock.
At this point, the study on "how to create ZooKeeper shared locks" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.