In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Zookeeper distributed lock instance operation". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Zookeeper distributed lock instance operation".
/ * package name: com.lencee.demo.zookeeper.locks * File name: LockClient.java * version Information: * date: January 23, 2015-4:49:48 * * / package com.lencee.demo.zookeeper.locks;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;import org.apache.zookeeper.data.Stat;/** * *
TODO: class name
*
TODO: describes the function implemented by this class. If it is an interface, the calling address should be declared.
* @ version 4:49:48 on January 23rd, 2015 * * / public class LockClient {/ / Zookeeper cluster service address and port private static String zkUrl = "192.168.0.101 virtual 11001"; / / configure node root path private final static String ROOT_LOCK = "/ lock"; private final static String WAIT_LOCK = "/ lockwait"; private final static String SELF_PATH = "/ client"; private final static String SELF_DATA = "/ client"; private ZooKeeper zk = null Private boolean iswait = true; / / lock path private String lockPath; / / wait path private String selfWaitPath; / / listen on the prelock path private String waitPath; public LockClient () {try {ZooKeeper zk = new ZooKeeper (zkUrl,3000,new Watcher ()) {@ Override public void process (WatchedEvent event) {try {if (event.getType () = = EventType.NodeDeleted) {System.out.println (event.getPath () + ":" + waitPath); getLock () }} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();}); while (zk.getState ()! = ZooKeeper.States.CONNECTED) {/ / System.out.println ("connecting:" + zk.getState ()); Thread.sleep (3000);} this.zk = zk; / / create root node String rootValue = "distributed lock" If (zk.exists (ROOT_LOCK, true) = = null) {zk.create (ROOT_LOCK, rootValue.getBytes (), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);} if (zk.exists (WAIT_LOCK, true) = = null) {zk.create (WAIT_LOCK, rootValue.getBytes (), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT) } / / add sub-node this.lockPath = addNode (ROOT_LOCK+SELF_PATH,SELF_DATA.getBytes (), CreateMode.EPHEMERAL_SEQUENTIAL) to the lock node; / / add sub-node this.selfWaitPath = WAIT_LOCK+this.lockPath.substring (ROOT_LOCK.length ()); addNode (this.selfWaitPath,SELF_DATA.getBytes (), CreateMode.EPHEMERAL); System.out.println ("lockpath:" + this.lockPath) System.out.println ("selfWaitPath:" + this.selfWaitPath); System.out.println ("waitPath:" + this.waitPath);} catch (Exception e) {e.printStackTrace ();}} public void getLock () throws Exception {/ / check whether the thread gets the lock List list = zk.getChildren (ROOT_LOCK, false); Collections.sort (list); for (String child:list) {System.out.println (child) } String lookfor = this.lockPath.substring (ROOT_LOCK.length () + 1); System.out.println (lookfor); int index= list.indexOf (lookfor); if (index==-1) {System.out.println ("NND, don't fool me");} else if (index==0) {/ / get lock System.out.println ("do something..."); / / remove lock queue / / zk.delete (this.lockPath,-1) / / Delete waiting queue / / zk.delete (this.selfWaitPath,-1); this.iswait = false;} else {/ / No lock, listen to the previous node String waitLockPath = list.get (index-1); this.waitPath = WAIT_LOCK+ "/" + waitLockPath; zk.getData (this.waitPath, true, new Stat ()); System.out.println ("lock not taken, listen" + this.waitPath) }} public String addNode (String path,byte [] data,CreateMode createMode) throws Exception {String nodePath = null; if (! path.startsWith ("/")) {throw new Exception ("incoming path does not start with'/'");} if (this.zk.exists (path, true) = = null) {/ / node does not exist nodePath = this.zk.create (path, data, Ids.OPEN_ACL_UNSAFE, createMode);} return nodePath } / * iswait * * @ return the iswait * @ since 1.0.0 * / public boolean isIswait () {return iswait;} / * @ param iswait the iswait to set * / public void setIswait (boolean iswait) {this.iswait = iswait;} public static void main (String [] args) throws Exception {LockClient lc = new LockClient (); System.out.println ("initialization ends") ; Thread.sleep (20,1000); lc.getLock (); while (lc.isIswait ());}} Thank you for reading. The above is the content of "Zookeeper distributed Lock instance Operation". After the study of this article, I believe you have a deeper understanding of the problem of Zookeeper distributed lock instance operation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.