zookeeper 節點操作和分布式鎖實現

2021-12-30 05:12:39 字數 3305 閱讀 9907

zookeeper節點操作

zookeeper節點操作

節點建立,刪除,遍歷 節點資料讀寫 節點型別

節點型別

持久節點 persistent

持久順序節點 persistent_sequential

臨時節點 ephemeral

臨時順序節點 ephemeral_sequential

節點操作見例項**

public class connectionwatcher implements watcher catch (interruptedexception e)

}public void closezk() throws interruptedexception

@override

public void process(watchedevent watchedevent)

}public class zookeepertest extends connectionwatcher

@override

public void closezk() throws interruptedexception

@override

public void process(watchedevent watchedevent)

public static void main(string args) throws exception

/*** 在給定的節點下建立臨時節點

* @param groupname

* @param numbername

* @throws exception

*/public void join(string groupname,string numbername)throws exception

/*** 獲取 指定節點下的子節點

* @param groupname

* @throws keeperexception

* @throws interruptedexception

*/public void listgroup(string groupname) throws keeperexception, interruptedexception else

}/**

* 刪除指定 節點 以及子節點

* 不支援遞迴刪除,只能從子節點往上刪除

* @param groupname

* @throws keeperexception

* @throws interruptedexception

*/public void deletenode(string groupname) throws keeperexception, interruptedexception

zk.delete(patchgroup,-1);

system.out.println("delete ok");

}/**

* 建立指定永久節點

* @param znode

* @throws exception

*/public void createznode(string znode) throws exception

/*** version -1 基於資料最新版本進行更新操作

* @param znode

* @param data

* @param version

*/public void wridedatatonode(string znode,string data,int version) catch (keeperexception e) catch (interruptedexception e)

system.out.println("向節點"+path+"寫入成功!");

}public void getdatafromnode(string znode) catch (keeperexception e) catch (interruptedexception e) }}

curator zookeeper 實現分布式鎖

原理 臨時順序節點 例項** 關於版本問題

curator zookeeper 實現分布式鎖

原理每個jvm使用同乙個客戶端例項,客戶端例項對某個方法加鎖的時候,

zookeeper對應節點下生成乙個唯一的臨時順序節點,如果是序號最小的,則獲得鎖,釋放鎖的時候刪除節點。

怠機的時候臨時節點會自動刪除,不會有死鎖。

臨時順序節點

如圖是除錯過程中 在zookeeper中生成用於判斷鎖的臨時順序節點

例項**

private static final string zk_address = "192.168.31.201:2181";//位址

private static final string zk_lock_path = "/zktest";//path

static ******dateformat time = new ******dateformat("yyyy-mm-dd hh:mm:ss");

public static void main(string args) throws exception ,"t1");

thread thread2 =new thread(()->,"t2");

thread thread3 =new thread(()->,"t3");

thread1.start();

thread2.start();

thread3.start();

}private static void dowithlock(curatorframework client, interprocessmutex lock) else

} catch (exception e) finally

}catch (exception e)}}

pom.xml

org.apache.curator

curator-framework

2.12.0

org.apache.curator

curator-recipes

2.12.0

org.apache.curator

curator-client

2.12.0

關於版本問題

curator 2.x.x相容zookeeper的3.4.x和3.5.x。

而curator 3.x.x只相容zookeeper 3.5.x,並且提供了一些諸如動態重新配置、watch刪除等新特性。

zookeeper基本操作和節點型別

啟動服務 sh zkserver.sh start 檢視狀態 sh zkserver.sh status 停止服務 sh zkserver.sh stop 重啟服務 sh zkserver.sh restart sh zkcli.sh server ip port ls 檢視資料節點列表 ls s ...

ZooKeeper 節點型別

zookeeper 節點是有生命週期的,這取決於節點的型別。在 zookeeper 中,節點型別可以分為持久節點 persistent 臨時節點 ephemeral 以及時序節點 sequential 具體在節點建立過程中,一般是組合使用,可以生成以下 4 種節點型別。持久節點 persistent...

ZooKeeper 節點型別

zookeeper 節點是有生命週期的,這取決於節點的型別。在 zookeeper 中,節點型別可以分為持久節點 persistent 臨時節點 ephemeral 以及時序節點 sequential 具體在節點建立過程中,一般是組合使用,可以生成以下 4 種節點型別。持久節點 persistent...