java 中 zookeeper簡(jiǎn)單使用
一、zookeeper的基本原理
數(shù)據(jù)模型,如下:

ZooKeeper數(shù)據(jù)模型的結(jié)構(gòu)與Unix文件系統(tǒng)很類似,整體上可以看作是一棵樹(shù),每個(gè)節(jié)點(diǎn)稱做一個(gè)ZNode。每個(gè)ZNode都可以通過(guò)其路徑唯一標(biāo)識(shí),比如上圖中第三層的第一個(gè)ZNode,它的路徑是/app1/c1。在每個(gè)ZNode上可存儲(chǔ)少量數(shù)據(jù)(默認(rèn)是1M, 可以通過(guò)配置修改,通常不建議在ZNode上存儲(chǔ)大量的數(shù)據(jù)),這個(gè)特性非常有用。另外,每個(gè)ZNode上還存儲(chǔ)了其Acl信息,這里需要注意,雖說(shuō)ZNode的樹(shù)形結(jié)構(gòu)跟Unix文件系統(tǒng)很類似,但是其Acl與Unix文件系統(tǒng)是完全不同的,每個(gè)ZNode的Acl的獨(dú)立的,子結(jié)點(diǎn)不會(huì)繼承父結(jié)點(diǎn)的。
ZooKeeper特性:
1、讀、寫(更新)模式
在ZooKeeper集群中,讀可以從任意一個(gè)ZooKeeperServer讀,這一點(diǎn)是保證ZooKeeper比較好的讀性能的關(guān)鍵;寫的請(qǐng)求會(huì)先Forwarder到Leader,然后由Leader來(lái)通過(guò)ZooKeeper中的原子廣播協(xié)議,將請(qǐng)求廣播給所有的Follower,Leader收到一半以上的寫成功的Ack后,就認(rèn)為該寫成功了,就會(huì)將該寫進(jìn)行持久化,并告訴客戶端寫成功了。
2、WAL和Snapshot
和大多數(shù)分布式系統(tǒng)一樣,ZooKeeper也有WAL(Write-Ahead-Log),對(duì)于每一個(gè)更新操作,ZooKeeper都會(huì)先寫WAL,然后再對(duì)內(nèi)存中的數(shù)據(jù)做更新,然后向Client通知更新結(jié)果。另外,ZooKeeper還會(huì)定期將內(nèi)存中的目錄樹(shù)進(jìn)行Snapshot,落地到磁盤上,這個(gè)跟HDFS中的FSImage是比較類似的。這么做的主要目的,一當(dāng)然是數(shù)據(jù)的持久化,二是加快重啟之后的恢復(fù)速度,如果全部通過(guò)ReplayWAL的形式恢復(fù)的話,會(huì)比較慢。
3、FIFO
對(duì)于每一個(gè)ZooKeeper客戶端而言,所有的操作都是遵循FIFO順序的,這一特性是由下面兩個(gè)基本特性來(lái)保證的:一是ZooKeeperClient與Server之間的網(wǎng)絡(luò)通信是基于TCP,TCP保證了Client/Server之間傳輸包的順序;二是ZooKeeperServer執(zhí)行客戶端請(qǐng)求也是嚴(yán)格按照FIFO順序的。
4、Linearizability
在ZooKeeper中,所有的更新操作都有嚴(yán)格的偏序關(guān)系,更新操作都是串行執(zhí)行的,這一點(diǎn)是保證ZooKeeper功能正確性的關(guān)鍵。
二、zookeeper的常用命令
我們可以執(zhí)行zookeeper-client或者執(zhí)行/opt/cloudera/parcels/CDH-5.0.0-1.cdh5.0.0.p0.47/lib/zookeeper/bin/zkCli.sh-server localhost,進(jìn)入zookeeper命令行,如下:

然后,執(zhí)行l(wèi)s /可以看到:

然后,我們可以執(zhí)行create /qyktest‘qyktest'創(chuàng)建一個(gè)節(jié)點(diǎn),如下:

然后,我們執(zhí)行g(shù)et /qyktest獲取節(jié)點(diǎn)值,如下:

然后,我們可以執(zhí)行set /qyktest‘111'修改節(jié)點(diǎn)的值,如下:

最后,我們執(zhí)行delete /qyktest便可刪除此節(jié)點(diǎn)。
另外,我們還可以在qyktest此節(jié)點(diǎn)下繼續(xù)創(chuàng)建子節(jié)點(diǎn)。
好了,幾個(gè)基本命令就講到這人啦,其它的命令還有很多,大家可以去查閱下資料。
三、zookeeper的javaapi操作
關(guān)于Javaapi操作zookeeper比較簡(jiǎn)單,筆者直接貼出代碼,如下:
packageorg.zookeeper.demo;
importjava.io.IOException;
importjava.util.concurrent.CountDownLatch;
importorg.apache.zookeeper.CreateMode;
importorg.apache.zookeeper.KeeperException;
importorg.apache.zookeeper.WatchedEvent;
importorg.apache.zookeeper.Watcher;
importorg.apache.zookeeper.Watcher.Event.KeeperState;
importorg.apache.zookeeper.ZooDefs.Ids;
importorg.apache.zookeeper.ZooKeeper;
publicclassZookeeperClientimplementsWatcher{
//連接超時(shí)時(shí)間,10s
privatestaticfinalintSESSION_TIMEOUT= 10000;
//連接的zookeeperserver
privatestaticfinalStringCONNECTION_STRING = "172.31.25.8:2181";
privatestaticfinalStringZK_PATH = "/qyktest";
privateZooKeeperzk = null;
privateCountDownLatchconnectedSemaphore = newCountDownLatch(1);
publicvoidcreateConnection(StringconnectString, intsessionTimeout){
this.releaseConnection();
try{
zk= newZooKeeper(connectString,sessionTimeout, this);
connectedSemaphore.await();
}catch(InterruptedExceptione) {
System.out.println("連接創(chuàng)建失敗,發(fā)生InterruptedException");
e.printStackTrace();
}catch(IOExceptione) {
System.out.println("連接創(chuàng)建失敗,發(fā)生IOException");
e.printStackTrace();
}
}
publicvoidreleaseConnection(){
if(this.zk!= null){
try{
this.zk.close();
}catch(InterruptedExceptione) {
e.printStackTrace();
}
}
}
publicbooleancreatePath(Stringpath, String data) {
try{
Stringresult = this.zk.create(path,data.getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println("節(jié)點(diǎn)創(chuàng)建成功,Path: "+result + ", content: "+data);
}catch(KeeperExceptione) {
System.out.println("節(jié)點(diǎn)創(chuàng)建失敗,發(fā)生KeeperException");
e.printStackTrace();
}catch(InterruptedExceptione) {
System.out.println("節(jié)點(diǎn)創(chuàng)建失敗,發(fā)生InterruptedException");
e.printStackTrace();
}
returntrue;
}
publicStringreadData(Stringpath) {
try{
System.out.println("獲取數(shù)據(jù)成功,path:"+path);
returnnewString(this.zk.getData(path,false,null));
}catch(KeeperExceptione) {
System.out.println("讀取數(shù)據(jù)失敗,發(fā)生KeeperException,path:"+path);
e.printStackTrace();
return"";
}catch(InterruptedExceptione) {
System.out.println("讀取數(shù)據(jù)失敗,發(fā)生InterruptedException,path: "+path);
e.printStackTrace();
return"";
}
}
publicbooleanwriteData(Stringpath, String data) {
try{
System.out.println("更新數(shù)據(jù)成功,path:"+path + ", stat: "+this.zk.setData(path,data.getBytes(), -1));
}catch(KeeperExceptione) {
System.out.println("更新數(shù)據(jù)失敗,發(fā)生KeeperException,path:"+path);
e.printStackTrace();
}catch(InterruptedExceptione) {
System.out.println("更新數(shù)據(jù)失敗,發(fā)生InterruptedException,path: "+path);
e.printStackTrace();
}
returnfalse;
}
publicvoiddeleteNode(Stringpath) {
try{
this.zk.delete(path,-1);
System.out.println("刪除節(jié)點(diǎn)成功,path:"+path);
}catch(KeeperExceptione) {
System.out.println("刪除節(jié)點(diǎn)失敗,發(fā)生KeeperException,path:"+path);
e.printStackTrace();
}catch(InterruptedExceptione) {
System.out.println("刪除節(jié)點(diǎn)失敗,發(fā)生InterruptedException,path: "+path);
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args) {
ZookeeperClientsample = newZookeeperClient();
//獲取連接
sample.createConnection(CONNECTION_STRING,SESSION_TIMEOUT);
//讀數(shù)據(jù)
Stringqyk = sample.readData("/qyktest");
System.out.println("qyk:"+qyk);
Stringurl = sample.readData("/qyk/db/url");
System.out.println("url"+url);
Stringdriver = sample.readData("/qyk/db/driver");
System.out.println("driver"+driver);
StringuserName = sample.readData("/qyk/db/userName");
System.out.println("userName"+userName);
Stringpassword = sample.readData("/qyk/db/password");
System.out.println("password"+password);
//創(chuàng)建節(jié)點(diǎn)
sample.createPath(ZK_PATH,"我是節(jié)點(diǎn)初始內(nèi)容");
System.out.println("數(shù)據(jù)內(nèi)容:"+sample.readData(ZK_PATH) + "\n");
//更新節(jié)點(diǎn)
sample.writeData(ZK_PATH,"更新后的數(shù)據(jù)");
System.out.println("數(shù)據(jù)內(nèi)容:"+sample.readData(ZK_PATH) + "\n");
//刪除節(jié)點(diǎn)
sample.deleteNode(ZK_PATH);
//釋放連接
sample.releaseConnection();
}
@Override
publicvoidprocess(WatchedEventevent) {
System.out.println("收到事件通知:"+event.getState() + "\n");
if(KeeperState.SyncConnected== event.getState()) {
connectedSemaphore.countDown();
}
}
}
然后,執(zhí)行可以看到,控制臺(tái)輸出如下:

所以,像一些公用的配置,我們可以存到zookeeper里面,之后其它的服務(wù)就可以使用了
總結(jié)
以上所述是小編給大家介紹的java 中 zookeeper簡(jiǎn)單使用,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn)
本文主要介紹了Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
SpringBoot中的application.properties無(wú)法加載問(wèn)題定位技巧
這篇文章主要介紹了SpringBoot中的application.properties無(wú)法加載問(wèn)題定位技巧,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
IntelliJ?IDEA?2022安裝注冊(cè)永久激活
java開(kāi)發(fā)工具IntelliJ?IDEA深受用戶喜愛(ài),很多朋友對(duì)這個(gè)idea開(kāi)發(fā)工具比較忠心,一旦有新版本發(fā)出,很多小伙伴就迫不及待的想更新,今天小編給大家?guī)?lái)了idea2022.1最新永久激活碼,親測(cè)有效,喜歡的朋友快來(lái)下載體驗(yàn)吧2022-08-08
解決JavaMail附件名字過(guò)長(zhǎng)導(dǎo)致的亂碼問(wèn)題
這篇文章主要介紹了解決JavaMail附件名字過(guò)長(zhǎng)導(dǎo)致的亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
SpringBoot創(chuàng)建線程池的六種方式小結(jié)
本文主要介紹了SpringBoot創(chuàng)建線程池的六種方式小結(jié),包括自定義線程池,固定長(zhǎng)度線程池,單一線程池,共享線程池,定時(shí)線程池,SpringBoot中注入異步線程池,感興趣的可以了解一下2023-11-11
解決Java提示正在嘗試分配更低的訪問(wèn)權(quán)限問(wèn)題
在本篇文章里小編給大家整理的是關(guān)于解決Java提示正在嘗試分配更低的訪問(wèn)權(quán)限問(wèn)題的相關(guān)方法內(nèi)容,有需要的朋友們跟著參考學(xué)習(xí)下。2019-07-07
Java基于IDEA實(shí)現(xiàn)qq郵件發(fā)送小程序
這篇文章主要介紹了Java基于IDEA實(shí)現(xiàn)qq郵件發(fā)送小程序功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
springboot集成redis啟動(dòng)報(bào)錯(cuò)問(wèn)題的解決方式
這篇文章主要介紹了springboot集成redis啟動(dòng)報(bào)錯(cuò)問(wèn)題的解決方式,從錯(cuò)誤信息上看缺少pool2相關(guān)包,查詢資料發(fā)現(xiàn)當(dāng)redis客戶端選擇Lettuce時(shí)候需要增加:commons-pool22023-11-11
添加引用,重啟服務(wù),需要的朋友可以參考下
Java中g(shù)etParameterTypes()方法的使用與原理分析
本文詳細(xì)介紹了Java中g(shù)etParameterTypes()方法的使用方式、工作原理及其在實(shí)際開(kāi)發(fā)中的應(yīng)用,該方法用于獲取方法的參數(shù)類型列表,并通過(guò)反射機(jī)制在運(yùn)行時(shí)動(dòng)態(tài)地獲取這些信息,感興趣的朋友跟隨小編一起看看吧2025-01-01

