![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),請進行舉報或認領
文檔簡介
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)造方法中傳入一個接口watcher(org.apache.zookeeper.數(shù)可以設置為null以表明不需要設置默認的Watcher處理器。boeedn(只oKepe這就是ZooKeeper的“read-only”模式。DoKepeZoeper實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項目用,Curator把 平時常用的很多ZooKeeper服務開發(fā)功能做了封裝,例如Leader、分布式計數(shù)器、分布式鎖。這就減少了技術(shù)人員在使用ZooKeeper時的大部分底層細節(jié)開發(fā)工作。在會話重新連接、Watch反復、多種異常處理等使用場景中,用原生的ZooKeeper處理比較復雜。而在使用Curator時,由于其對這些功能都做了高度的封裝,使用起來更加簡Curator 以Maven工程為例,首先要引入Curator框架相關的開發(fā)包,這里為了方便測試引入了junit,lombok,由于Zookeeper本身以來了log4j日志框架,所以這里可以創(chuàng)建對應的log4j配置文件后直接使用。如下面的代碼所示, 通過將Curator相關的包配置到Maven工程的pom文件中,將Curaotr框架到工程項目里,在配置文件中分別了兩個Curator相關的包,第一個是curator-framework包,該包是對ZooKeeper底層API的一些封裝。另一個是curator-recipes包,該包封裝了一些ZooKeeper服務的高級特性,如:Cache、、分布式鎖、分布式Barrier。 =newExponentialBackoffRetry(1000,3nString,= fluent112=newExponentialBackoffRetry(1000,= 789義一個ZooKeeper客戶端對象,并在的上下文中使用。在定義CuratorFramework對象 使用了CuratorFrameworkFactory工廠方法,并指定了connectionString服務器地址列表、retry重試策略、sessionTimeoutMs會話超時時間、connectionTimeoutMs會話創(chuàng)建超時時間。下面 是多個地址。如果是多個地址,那么每個服務器地址列表用逗號分隔,如host1:port1,host2:port2,host3;port3。重新連接ZooKeeper服務端。而Curator提供了一次重試、多次重試等不同種類的實現(xiàn)方式。在Curator,可以通過判斷服務器返回的keeperException的狀態(tài)代碼來判斷是否進OKSYSTEMERROR超時時間:Curator客戶端創(chuàng)建過程中,有兩個超時時間的設置。一個是sessionTimeoutMs會話超時時間,用來設置該條會話在ZooKeeper服務端的失效時間。另一個是ZooKeeper服務端應答的時間。sessionTimeoutMs作用在服務端,而connectionTimeoutMs作用在客戶端。 22publicvoidtestCreate()ceptionStringpath=("curatorcreatenode:{}6CuratorcreatewithMode點,調(diào)用forPath函數(shù)來指定節(jié)點的路徑和數(shù)據(jù)信息。publicvoidtestCreateWithParent()ceptionStringStringpath=curatorFramework.create().creatingParentsIfNeeded().forPath(pa("curatorcreatenode:{}6publicvoidtestGetData()ceptionbyte[]bytes=("getdatafromnode:{}successfully.",new5后邊,通過forPath函數(shù)來指定更新的數(shù)據(jù)節(jié)點路徑以及要更新的數(shù)據(jù)。byte[]bytes=("getdatafromnode/curator‐node:{}successfully.",newString(bytceptionpublicvoidtestSetData()66publicvoidtestDelete()ceptionString5ZooKeeper服務端被刪除。在異步線程中調(diào)用,默認在EventThread中調(diào)用,也可以自定義線程池。publicpublicinterface{ *Calledwhentheasyncbackgroundoperation** *@parameventoperation 89*ceptionpublicvoid},CuratorEventevent) inBackground異步處理默認在EventThread中執(zhí)行publicvoidtest()ception ("background:{}",6 8publicvoidtest()ception ExecutorServiceexecutorService=4("background:{}",8}1*Receivesnotificationsabouterrorsandbackgroundpublicinterface{ *Calledwhenabackgroundtaskhascompletedorawatchhas***@parameventthe*ceptionanypublicvoid},CuratorEventevent)CuratorCurator引入了Cache來實現(xiàn)對Zookeeper服務端,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使用一個 了映射。所以TreeCache可以當前節(jié)點下所有節(jié)點的。11456publicStringpath,booleancacheData)‐path‐pathto,237cacheData‐iftrue,nodecontentsarecachedinadditiontothepublicclassTreeCacheTest3 publicstaticfinalString5678publicvoidtestTreeCache()throwceptionTreeCachetreeCache=newTreeCache(curatorFramework,publicvoidchildEvent(CuratorFramework ,TreeCacheEventevent)throwsException{("treecache:}}18publicvoidaddListener(TreeCacheListenerAddachangelistener‐theZookeeper Follower:只能處理讀請求,同時作為Leader的候選節(jié)點,即如果Leader宕機,F(xiàn)ollower節(jié)點 1java1java1wgettar‐zxvfcd重命名 cpzoo_sample.cfg1#1#vim23server.1=:2001:3001:participantparticipant可以不用寫,默認就是part 己對外提供服務的起始狀態(tài)。Leader服務器允許Follower再initLimit時間內(nèi)完成這個工與dataDir:顧名思義就是Zookeeper保存數(shù)據(jù)的 cpconf/zoo1.cfgcp cpconf/zoo1.cfgcpconf/zoo1.cfgcpconf/zoo1.cfg4vim vimvim vim Step5:標識Server myidserverid,即cdcdvim1cdvim2cdvim3cdvim4////////////binzkServer.shstartconfbinzkServer.shstartconfbinzkServer.shstartconf4Step7:檢測集群狀態(tài),也可以直接用命令zkServer.sh statusconf/zoo1.cfg進行每臺服務1serverzookeeper/configZookeeper3.5.0Zookeeper3.5.0以前,Zookeeper集群角色要發(fā)生改變的話,只能通過停掉所有的應對高可用需求,Zookeeper3.5.0提供了支持動態(tài)擴容/縮容的新特性。但是通過客戶端API可以變更服務端集群狀態(tài)是件很的事情,所以在zookeeper3.5.3版本要用動態(tài)配置,需要開啟超級管理員驗證模式ACLs。如果是在一個安全的環(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)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年住宅裝飾工程委托施工協(xié)議
- 2025年不動產(chǎn)分期付款銷售合同范文
- 2025年勞動合同社會保障權(quán)益
- 2025年企業(yè)財務規(guī)劃委托協(xié)議范本
- 2025年一致行動方股權(quán)交易協(xié)議范例
- 2025年信用貸款延期還款協(xié)議書(7)
- 2025年合作契約解除協(xié)議范例
- 2025年共同策劃建立子公司的合同范例
- 2025年債權(quán)購買協(xié)議書格式
- 2025年中山貨運上崗證考試題庫
- 中國慢性腎臟病早期評價與管理指南2023
- 中藥材倉儲標準化與信息化建設
- 陰囊常見疾病的超聲診斷
- 2024屆高考數(shù)學高考總復習:集合與常用邏輯用語集合的概念與運算
- DZ∕T 0051-2017 地質(zhì)巖心鉆機型式與規(guī)格系列(正式版)
- 《行業(yè)標準-太陽能光熱發(fā)電技術(shù)監(jiān)督導則》
- 壓力管道穿(跨)越施工工藝規(guī)程2015
- 業(yè)主授權(quán)租戶安裝充電樁委托書
- 建筑工人實名制管理制度及實施方案
- 《養(yǎng)老護理員》-課件:協(xié)助老年人穿脫簡易矯形器
- GB 1886.227-2024食品安全國家標準食品添加劑嗎啉脂肪酸鹽果蠟
評論
0/150
提交評論