vip 02 zookeeper客戶端使用與集群特性_第1頁
vip 02 zookeeper客戶端使用與集群特性_第2頁
vip 02 zookeeper客戶端使用與集群特性_第3頁
vip 02 zookeeper客戶端使用與集群特性_第4頁
vip 02 zookeeper客戶端使用與集群特性_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(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)造方法中傳入一個接口watcher(org.apache.zookeeper.數(shù)可以設(shè)置為null以表明不需要設(shè)置默認(rèn)的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服務(wù)開發(fā)功能做了封裝,例如Leader、分布式計數(shù)器、分布式鎖。這就減少了技術(shù)人員在使用ZooKeeper時的大部分底層細(xì)節(jié)開發(fā)工作。在會話重新連接、Watch反復(fù)、多種異常處理等使用場景中,用原生的ZooKeeper處理比較復(fù)雜。而在使用Curator時,由于其對這些功能都做了高度的封裝,使用起來更加簡Curator 以Maven工程為例,首先要引入Curator框架相關(guān)的開發(fā)包,這里為了方便測試引入了junit,lombok,由于Zookeeper本身以來了log4j日志框架,所以這里可以創(chuàng)建對應(yīng)的log4j配置文件后直接使用。如下面的代碼所示, 通過將Curator相關(guān)的包配置到Maven工程的pom文件中,將Curaotr框架到工程項目里,在配置文件中分別了兩個Curator相關(guān)的包,第一個是curator-framework包,該包是對ZooKeeper底層API的一些封裝。另一個是curator-recipes包,該包封裝了一些ZooKeeper服務(wù)的高級特性,如:Cache、、分布式鎖、分布式Barrier。 =newExponentialBackoffRetry(1000,3nString,= fluent112=newExponentialBackoffRetry(1000,= 789義一個ZooKeeper客戶端對象,并在的上下文中使用。在定義CuratorFramework對象 使用了CuratorFrameworkFactory工廠方法,并指定了connectionString服務(wù)器地址列表、retry重試策略、sessionTimeoutMs會話超時時間、connectionTimeoutMs會話創(chuàng)建超時時間。下面 是多個地址。如果是多個地址,那么每個服務(wù)器地址列表用逗號分隔,如host1:port1,host2:port2,host3;port3。重新連接ZooKeeper服務(wù)端。而Curator提供了一次重試、多次重試等不同種類的實現(xiàn)方式。在Curator,可以通過判斷服務(wù)器返回的keeperException的狀態(tài)代碼來判斷是否進(jìn)OKSYSTEMERROR超時時間:Curator客戶端創(chuàng)建過程中,有兩個超時時間的設(shè)置。一個是sessionTimeoutMs會話超時時間,用來設(shè)置該條會話在ZooKeeper服務(wù)端的失效時間。另一個是ZooKeeper服務(wù)端應(yīng)答的時間。sessionTimeoutMs作用在服務(wù)端,而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服務(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來實現(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使用一個 了映射。所以TreeCache可以當(dāng)前節(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宕機(jī),F(xiàn)ollower節(jié)點 1java1java1wgettar‐zxvfcd重命名 cpzoo_sample.cfg1#1#vim23server.1=:2001:3001:participantparticipant可以不用寫,默認(rèn)就是part 己對外提供服務(wù)的起始狀態(tài)。Leader服務(wù)器允許Follower再initLimit時間內(nèi)完成這個工與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)配置,需要開啟超級管理員驗證模式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)容里面會有圖紙預(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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論