您好,登錄后才能下訂單哦!
這篇文章主要講解了“Zookeeper分布式鎖實例操作”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Zookeeper分布式鎖實例操作”吧!
/** * 包名:com.lencee.demo.zookeeper.locks * 文件名:LockClient.java * 版本信息: * 日期:2015年1月23日-下午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; /** * * <p>TODO:類名稱<p> * <p>TODO:描述本類實現的功能作用,若為接口應該聲明調用地址</p> * @version 2015年1月23日 下午4:49:48 * */ public class LockClient { // Zookeeper集群服務地址與端口 private static String zkUrl = "192.168.0.101:11001"; // 配置結點根路徑 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; //鎖路徑 private String lockPath; //等待路徑 private String selfWaitPath; //監聽前置鎖路徑 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; //創建根結點 String rootValue = "分布式鎖"; 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); } //在鎖結點上增加子結點 this.lockPath = addNode(ROOT_LOCK+SELF_PATH,SELF_DATA.getBytes(),CreateMode.EPHEMERAL_SEQUENTIAL); //在等待結點上增加子結點 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 { //檢查本線程是否取到鎖 List<String> 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,別坑我"); }else if(index==0){ //獲取到鎖 System.out.println("do something..."); //刪除鎖隊列 //zk.delete(this.lockPath, -1); //刪除等待隊列 //zk.delete(this.selfWaitPath, -1); this.iswait = false; }else{ //未取到鎖,偵聽前一個節點 String waitLockPath = list.get(index-1); this.waitPath = WAIT_LOCK+"/"+waitLockPath; zk.getData(this.waitPath, true, new Stat()); System.out.println("沒取到鎖,偵聽"+this.waitPath); } } public String addNode(String path,byte[] data,CreateMode createMode) throws Exception{ String nodePath = null; if(!path.startsWith("/")){ throw new Exception("傳入的路徑沒有以'/'開始"); } if(this.zk.exists(path, true)==null){ //結點不存在 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("初始化結束。。。。。"); Thread.sleep(20*1000); lc.getLock(); while(lc.isIswait()); } }
感謝各位的閱讀,以上就是“Zookeeper分布式鎖實例操作”的內容了,經過本文的學習后,相信大家對Zookeeper分布式鎖實例操作這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。