這篇文章主要講解了“Zookeeper分布式鎖實(shí)例操作”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Zookeeper分布式鎖實(shí)例操作”吧!
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比川匯網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式川匯網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋川匯地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。
/** * 包名: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; /** * *TODO:類名稱
*
TODO:描述本類實(shí)現(xiàn)的功能作用,若為接口應(yīng)該聲明調(diào)用地址
* @version 2015年1月23日 下午4:49:48 * */ public class LockClient { // Zookeeper集群服務(wù)地址與端口 private static String zkUrl = "192.168.0.101:11001"; // 配置結(jié)點(diǎn)根路徑 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; //監(jiān)聽(tīng)前置鎖路徑 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; //創(chuàng)建根結(jié)點(diǎn) 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); } //在鎖結(jié)點(diǎn)上增加子結(jié)點(diǎn) this.lockPath = addNode(ROOT_LOCK+SELF_PATH,SELF_DATA.getBytes(),CreateMode.EPHEMERAL_SEQUENTIAL); //在等待結(jié)點(diǎn)上增加子結(jié)點(diǎn) 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 { //檢查本線程是否取到鎖 Listlist = 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..."); //刪除鎖隊(duì)列 //zk.delete(this.lockPath, -1); //刪除等待隊(duì)列 //zk.delete(this.selfWaitPath, -1); this.iswait = false; }else{ //未取到鎖,偵聽(tīng)前一個(gè)節(jié)點(diǎn) String waitLockPath = list.get(index-1); this.waitPath = WAIT_LOCK+"/"+waitLockPath; zk.getData(this.waitPath, true, new Stat()); System.out.println("沒(méi)取到鎖,偵聽(tīng)"+this.waitPath); } } public String addNode(String path,byte[] data,CreateMode createMode) throws Exception{ String nodePath = null; if(!path.startsWith("/")){ throw new Exception("傳入的路徑?jīng)]有以'/'開(kāi)始"); } if(this.zk.exists(path, true)==null){ //結(jié)點(diǎn)不存在 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("初始化結(jié)束。。。。。"); Thread.sleep(20*1000); lc.getLock(); while(lc.isIswait()); } }
感謝各位的閱讀,以上就是“Zookeeper分布式鎖實(shí)例操作”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Zookeeper分布式鎖實(shí)例操作這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!