In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how zookeeper implements competitive locks. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Package com.hgs.sharelocks;import java.util.Collections;import java.util.List;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.EventType;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;public class ZKsharelocks {private ZooKeeper zk = null; private final static String connectString = "192.168.6.131purl 2181192.168.6.132purl 2181192.168.6.133purl 2181" Private final static int sessionTimeout = 2000; private final static String parentNode= "/ servers"; private final static String subNode= "/ sub"; private volatile String myPath = null; private boolean lockflag = false;public static void main (String [] args) throws Exception {ZKsharelocks sl = new ZKsharelocks (); sl.getConnect (); Thread.sleep (Long.MAX_VALUE) } public void getConnect () throws Exception {/ / create instance zk = new ZooKeeper (connectString, sessionTimeout, new Watcher () {@ Overridepublic void process (WatchedEvent event) {/ / determine whether the event type and path is the business path if (event.getType () = = EventType.NodeChildrenChanged&&event.getPath (). Equals (parentNode)) {List inchild = null Try {/ / get the child node and continue listening to inchild = zk.getChildren (parentNode, true); / / sort Collections.sort (inchild) / / determine that the returned time path is not null and the smallest subpath is the current day path if (myPath! = null&&inchild.size () > 0&&inchild.indexOf (myPath.substring (parentNode.length () + 1)) = = 0) {doSomthing (); Thread.sleep (2000) MyPath=zk.create (parentNode+subNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);}} catch (Exception e) {e.printStackTrace ();}); / / first create a temporary sequence entry myPath=zk.create (parentNode+subNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL) / / get the number of subdirectories under the parent node and listen to List childs = zk.getChildren (parentNode, true); / / determine whether you are the only one when entering. If you are the only one, you can do the business logic if (childs.size () = = 1) {/ / the business method has the operation doSomthing () to delete the myPath entry in the business method; / / continue to myPath=zk.create (parentNode+subNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL) after deletion }} public void doSomthing () throws Exception {System.out.println ("get the lock..." + Thread.currentThread (). GetName ()); / / delete the lock held after the lock is completed, that is, delete the entry zk.delete (myPath,-1);}} Thank you for reading! This is the end of the article on "how to achieve competitive locks in zookeeper". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.