![vip 02 zookeeper客戶端使用與集群特性_第1頁](http://file4.renrendoc.com/view/8bc7f7b92311ea49a0d2555334e66cb2/8bc7f7b92311ea49a0d2555334e66cb21.gif)
![vip 02 zookeeper客戶端使用與集群特性_第2頁](http://file4.renrendoc.com/view/8bc7f7b92311ea49a0d2555334e66cb2/8bc7f7b92311ea49a0d2555334e66cb22.gif)
![vip 02 zookeeper客戶端使用與集群特性_第3頁](http://file4.renrendoc.com/view/8bc7f7b92311ea49a0d2555334e66cb2/8bc7f7b92311ea49a0d2555334e66cb23.gif)
![vip 02 zookeeper客戶端使用與集群特性_第4頁](http://file4.renrendoc.com/view/8bc7f7b92311ea49a0d2555334e66cb2/8bc7f7b92311ea49a0d2555334e66cb24.gif)
![vip 02 zookeeper客戶端使用與集群特性_第5頁](http://file4.renrendoc.com/view/8bc7f7b92311ea49a0d2555334e66cb2/8bc7f7b92311ea49a0d2555334e66cb25.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
ZookeeperJava客戶端ZookeeperJava publicpublicclassTest3 privatestaticfinalString5 privatestaticfinalintSESSION_TIMEOUT=7 privatestaticZooKeeper9 privatestaticfinalStringpublicvoidinit()throwsIOException,InterruptedExceptionfinalCountDownLatchcountDownLatch=newzooKeeper=newZooKeeper(ZK_ADDRESS,SESSION_TIMEOUT,event‐>eventevent.getType()==} }26ZooKeeperZooKeeper(StringconnectString,intsessionTimeout,WatcherZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,ZKZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,booleananBeReadOnly,HostProvider)ZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,booleananBeReadOnly,HostProvider,ZK ZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,ZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,booleananBeReadOnly,ZK ZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,long,byZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,long,e[],boolean,ZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,long,bye[],boolean,HostProvider,ZK ZooKeeper(StringconnectString,intsessionTimeout,Watcherwatcher,long,byte[],boolean)后的 客戶端在構(gòu)造方法中傳入一個(gè)接口watcher(org.apache.zookeeper.數(shù)可以設(shè)置為null以表明不需要設(shè)置默認(rèn)的Watcher處理器。boeedn(只oKepe這就是ZooKeeper的“read-only”模式。DoKepeZoeper實(shí)longgetSessionId();publicvoidcreateTest()throwsKeeperException,InterruptedExceptionStringpath=zooKeeper.create(ZK_NODE,"data".getBytes(),CL_UNSAFE,("createdpath:5publicvoidcreateAsycTest()throwsInterruptedException(rc,path,ctx,name)‐>("rc{},path{},ctx7publicvoidsetTest()throwsKeeperException,InterruptedException33Statstat=newbyte[]data=zooKeeper.getData(ZK_NODE,false,("修改前,newbyte[]dataAfter=zooKeeper.getData(ZK_NODE,false,("修改后,new}Curator是一套flix公司開源的,Java語言編程的ZooKeeper客戶端框架,Curator項(xiàng)目用,Curator把 平時(shí)常用的很多ZooKeeper服務(wù)開發(fā)功能做了封裝,例如Leader、分布式計(jì)數(shù)器、分布式鎖。這就減少了技術(shù)人員在使用ZooKeeper時(shí)的大部分底層細(xì)節(jié)開發(fā)工作。在會話重新連接、Watch反復(fù)、多種異常處理等使用場景中,用原生的ZooKeeper處理比較復(fù)雜。而在使用Curator時(shí),由于其對這些功能都做了高度的封裝,使用起來更加簡Curator 以Maven工程為例,首先要引入Curator框架相關(guān)的開發(fā)包,這里為了方便測試引入了junit,lombok,由于Zookeeper本身以來了log4j日志框架,所以這里可以創(chuàng)建對應(yīng)的log4j配置文件后直接使用。如下面的代碼所示, 通過將Curator相關(guān)的包配置到Maven工程的pom文件中,將Curaotr框架到工程項(xiàng)目里,在配置文件中分別了兩個(gè)Curator相關(guān)的包,第一個(gè)是curator-framework包,該包是對ZooKeeper底層API的一些封裝。另一個(gè)是curator-recipes包,該包封裝了一些ZooKeeper服務(wù)的高級特性,如:Cache、、分布式鎖、分布式Barrier。 =newExponentialBackoffRetry(1000,3nString,= fluent112=newExponentialBackoffRetry(1000,= 789義一個(gè)ZooKeeper客戶端對象,并在的上下文中使用。在定義CuratorFramework對象 使用了CuratorFrameworkFactory工廠方法,并指定了connectionString服務(wù)器地址列表、retry重試策略、sessionTimeoutMs會話超時(shí)時(shí)間、connectionTimeoutMs會話創(chuàng)建超時(shí)時(shí)間。下面 是多個(gè)地址。如果是多個(gè)地址,那么每個(gè)服務(wù)器地址列表用逗號分隔,如host1:port1,host2:port2,host3;port3。重新連接ZooKeeper服務(wù)端。而Curator提供了一次重試、多次重試等不同種類的實(shí)現(xiàn)方式。在Curator,可以通過判斷服務(wù)器返回的keeperException的狀態(tài)代碼來判斷是否進(jìn)OKSYSTEMERROR超時(shí)時(shí)間:Curator客戶端創(chuàng)建過程中,有兩個(gè)超時(shí)時(shí)間的設(shè)置。一個(gè)是sessionTimeoutMs會話超時(shí)時(shí)間,用來設(shè)置該條會話在ZooKeeper服務(wù)端的失效時(shí)間。另一個(gè)是ZooKeeper服務(wù)端應(yīng)答的時(shí)間。sessionTimeoutMs作用在服務(wù)端,而connectionTimeoutMs作用在客戶端。 22publicvoidtestCreate()ceptionStringpath=("curatorcreatenode:{}6CuratorcreatewithMode點(diǎn),調(diào)用forPath函數(shù)來指定節(jié)點(diǎn)的路徑和數(shù)據(jù)信息。publicvoidtestCreateWithParent()ceptionStringStringpath=curatorFramework.create().creatingParentsIfNeeded().forPath(pa("curatorcreatenode:{}6publicvoidtestGetData()ceptionbyte[]bytes=("getdatafromnode:{}successfully.",new5后邊,通過forPath函數(shù)來指定更新的數(shù)據(jù)節(jié)點(diǎn)路徑以及要更新的數(shù)據(jù)。byte[]bytes=("getdatafromnode/curator‐node:{}successfully.",newString(bytceptionpublicvoidtestSetData()66publicvoidtestDelete()ceptionString5ZooKeeper服務(wù)端被刪除。在異步線程中調(diào)用,默認(rèn)在EventThread中調(diào)用,也可以自定義線程池。publicpublicinterface{ *Calledwhentheasyncbackgroundoperation** *@parameventoperation 89*ceptionpublicvoid},CuratorEventevent) inBackground異步處理默認(rèn)在EventThread中執(zhí)行publicvoidtest()ception ("background:{}",6 8publicvoidtest()ception ExecutorServiceexecutorService=4("background:{}",8}1*Receivesnotificationsabouterrorsandbackgroundpublicinterface{ *Calledwhenabackgroundtaskhascompletedorawatchhas***@parameventthe*ceptionanypublicvoid},CuratorEventevent)CuratorCurator引入了Cache來實(shí)現(xiàn)對Zookeeper服務(wù)端,Cache可以理解為一node112345publicString‐path‐pathto,publicclassNodeCacheTest3 publicstaticfinalString5publicvoidtestNodeCacheTest() ception8NodeCachenodeCache=newNodeCache(curatorFramework,nodeCache.getListenable().addListener(newNodeCacheListener()publicvoidnodeChanged() ("{}pathnodeChanged:}} publicvoidprintNodeData()ceptionpublicvoidaddListener(NodeCacheListenerAddachangelistener‐thebyte[]byte[] ("data:{}",new}27path11456publicStringpath,booleancacheData)‐path‐pathto,237cacheData‐iftrue,nodecontentsarecachedinadditiontothepublicclassPathCacheTest3 publicstaticfinalString5publicvoidtestPathCache()ception8PathChildrenCachepathChildrenCache=newPathChildrenCache(curatorFramework,PATH,true);PathChildrenCacheListener(){publicvoidchildEvent(CuratorFramework ,PathChildrenCacheEventevent)throw ception{("event:} //publicvoidaddListener(PathChildrenCacheListenerAddachangelistener‐the}21treeTreeCache使用一個(gè) 了映射。所以TreeCache可以當(dāng)前節(jié)點(diǎn)下所有節(jié)點(diǎn)的。11456publicStringpath,booleancacheData)‐path‐pathto,237cacheData‐iftrue,nodecontentsarecachedinadditiontothepublicclassTreeCacheTest3 publicstaticfinalString5678publicvoidtestTreeCache()throwceptionTreeCachetreeCache=newTreeCache(curatorFramework,publicvoidchildEvent(CuratorFramework ,TreeCacheEventevent)throwsException{("treecache:}}18publicvoidaddListener(TreeCacheListenerAddachangelistener‐theZookeeper Follower:只能處理讀請求,同時(shí)作為Leader的候選節(jié)點(diǎn),即如果Leader宕機(jī),F(xiàn)ollower節(jié)點(diǎn) 1java1java1wgettar‐zxvfcd重命名 cpzoo_sample.cfg1#1#vim23server.1=:2001:3001:participantparticipant可以不用寫,默認(rèn)就是part 己對外提供服務(wù)的起始狀態(tài)。Leader服務(wù)器允許Follower再initLimit時(shí)間內(nèi)完成這個(gè)工與dataDir:顧名思義就是Zookeeper保存數(shù)據(jù)的 cpconf/zoo1.cfgcp cpconf/zoo1.cfgcpconf/zoo1.cfgcpconf/zoo1.cfg4vim vimvim vim Step5:標(biāo)識Server myidserverid,即cdcdvim1cdvim2cdvim3cdvim4////////////binzkServer.shstartconfbinzkServer.shstartconfbinzkServer.shstartconf4Step7:檢測集群狀態(tài),也可以直接用命令zkServer.sh statusconf/zoo1.cfg進(jìn)行每臺服務(wù)1serverzookeeper/configZookeeper3.5.0Zookeeper3.5.0以前,Zookeeper集群角色要發(fā)生改變的話,只能通過停掉所有的應(yīng)對高可用需求,Zookeeper3.5.0提供了支持動態(tài)擴(kuò)容/縮容的新特性。但是通過客戶端API可以變更服務(wù)端集群狀態(tài)是件很的事情,所以在zookeeper3.5.3版本要用動態(tài)配置,需要開啟超級管理員驗(yàn)證模式ACLs。如果是在一個(gè)安全的環(huán)境也可以通過配置系統(tǒng)參數(shù)-echo‐ng
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年住宅裝飾工程委托施工協(xié)議
- 2025年不動產(chǎn)分期付款銷售合同范文
- 2025年勞動合同社會保障權(quán)益
- 2025年企業(yè)財(cái)務(wù)規(guī)劃委托協(xié)議范本
- 2025年一致行動方股權(quán)交易協(xié)議范例
- 2025年信用貸款延期還款協(xié)議書(7)
- 2025年合作契約解除協(xié)議范例
- 2025年共同策劃建立子公司的合同范例
- 2025年債權(quán)購買協(xié)議書格式
- 2025年中山貨運(yùn)上崗證考試題庫
- 中考數(shù)學(xué)計(jì)算題練習(xí)100道(2024年中考真題)
- 中國慢性腎臟病早期評價(jià)與管理指南2023
- 中藥材倉儲標(biāo)準(zhǔn)化與信息化建設(shè)
- 陰囊常見疾病的超聲診斷
- 2024屆高考數(shù)學(xué)高考總復(fù)習(xí):集合與常用邏輯用語集合的概念與運(yùn)算
- DZ∕T 0051-2017 地質(zhì)巖心鉆機(jī)型式與規(guī)格系列(正式版)
- 《行業(yè)標(biāo)準(zhǔn)-太陽能光熱發(fā)電技術(shù)監(jiān)督導(dǎo)則》
- 壓力管道穿(跨)越施工工藝規(guī)程2015
- 業(yè)主授權(quán)租戶安裝充電樁委托書
- 建筑工人實(shí)名制管理制度及實(shí)施方案
- 《養(yǎng)老護(hù)理員》-課件:協(xié)助老年人穿脫簡易矯形器
評論
0/150
提交評論