生活随笔
收集整理的這篇文章主要介紹了
zookeeper配置中心
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
多個客戶端從zookeeper 的配置中心拿到配置,如果配置中心沒有配置就阻塞,如果配置修改了就拿到新的。
原理
因為zookeeper的同步性質,即單線程的分兩段式(當發生修改時,第一階段:leader向各個follower發送log任務,過半成功返回后進行第二段的具體的修改)的事務方式,如果節點發生更改,則要么成功且所有客戶端都get到最新的修改結果,要么失敗。所以代碼只要注冊發生修改(包括數據修改,節點增加,刪除等,以下同)的事件,死循環取就行。本文只寫了簡單的框架,復雜的業務邏輯沒有。用zookeeper模擬另外的客戶端做修改的操作。
代碼
目錄結構
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.ZooKeeper
;
import org
.junit
.After
;
import org
.junit
.Before
;
import org
.junit
.Test
;
public class Main {ZooKeeper zk
;@Beforepublic void before() {zk
= ZkUtils
.getZK();}@Afterpublic void after() {try {zk
.close();} catch (InterruptedException e
) {e
.printStackTrace();}}@Testpublic void getConf() {WatchCallBack w
= new WatchCallBack();MyConf conf
= new MyConf();w
.setConf(conf
);w
.setZk(zk
);w
.await();while (true) {if (conf
.getConfStr().equals("")) {System
.out
.println("配置丟了 ....");w
.await();} else {System
.out
.println("conf: " + conf
.getConfStr());}}}
}
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.WatchedEvent
;
import org
.apache
.zookeeper
.Watcher
;import java
.util
.concurrent
.CountDownLatch
;
public class DefaultWatcher implements Watcher {CountDownLatch c
;public void setC(CountDownLatch c
) {this.c
= c
;}@Overridepublic void process(WatchedEvent e
) {Event
.EventType type
= e
.getType();Event
.KeeperState state
= e
.getState();String path
= e
.getPath();switch (type
) {case None
:break;case NodeCreated
:break;case NodeDeleted
:break;case NodeDataChanged
:break;case NodeChildrenChanged
:break;}switch (state
) {case Unknown
:break;case Disconnected
:break;case NoSyncConnected
:break;case SyncConnected
:c
.countDown();break;case AuthFailed
:break;case ConnectedReadOnly
:break;case SaslAuthenticated
:break;case Expired
:break;}}
}
package org
.faithgreen
.conf
;
public class MyConf {private String confStr
;public String
getConfStr() {return confStr
;}public void setConfStr(String confStr
) {this.confStr
= confStr
;}
}
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.AsyncCallback
;
import org
.apache
.zookeeper
.WatchedEvent
;
import org
.apache
.zookeeper
.Watcher
;
import org
.apache
.zookeeper
.ZooKeeper
;
import org
.apache
.zookeeper
.data
.Stat
;import java
.util
.concurrent
.CountDownLatch
;
public class WatchCallBack implements Watcher, AsyncCallback
.DataCallback
, AsyncCallback
.StatCallback
{ZooKeeper zk
;CountDownLatch cc
= new CountDownLatch(1);MyConf conf
;public ZooKeeper
getZk() {return zk
;}public void setZk(ZooKeeper zk
) {this.zk
= zk
;}public CountDownLatch
getCc() {return cc
;}public void setCc(CountDownLatch cc
) {this.cc
= cc
;}public MyConf
getConf() {return conf
;}public void setConf(MyConf conf
) {this.conf
= conf
;}public void await() {zk
.exists("/appConf", this, this, "abc");try {cc
.await();} catch (InterruptedException e
) {e
.printStackTrace();}}@Overridepublic void process(WatchedEvent e
) {String path
= e
.getPath();Event
.EventType type
= e
.getType();Event
.KeeperState state
= e
.getState();switch (type
) {case None
:break;case NodeCreated
:zk
.getData("/appConf", this, this, "def");break;case NodeDeleted
:conf
.setConfStr("");cc
= new CountDownLatch(1);break;case NodeDataChanged
:zk
.getData("/appConf", this, this, "def");break;case NodeChildrenChanged
:break;}}@Overridepublic void processResult(int i
, String s
, Object o
, byte[] bytes
, Stat stat
) {if (bytes
!= null
) {String s1
= new String(bytes
);conf
.setConfStr(s1
);cc
.countDown();}}@Overridepublic void processResult(int i
, String s
, Object o
, Stat stat
) {if (stat
!= null
) {zk
.getData("/appConf", this, this, "def");}}
}
package org
.faithgreen
.conf
;import org
.apache
.zookeeper
.ZooKeeper
;import java
.util
.concurrent
.CountDownLatch
;public class ZkUtils {static ZooKeeper zk
;final static String address
= "192.168.172.3:2181,192.168.172.4:2181,192.168.172.5:2181,192.168.172.6:2181/testConf";final static DefaultWatcher defaultWatcher
= new DefaultWatcher();static CountDownLatch c
= new CountDownLatch(1);public static ZooKeeper
getZK() {try {zk
= new ZooKeeper(address
, 4000, defaultWatcher
);defaultWatcher
.setC(c
);c
.await();} catch (Exception e
) {e
.printStackTrace();}return zk
;}
}
總結
以上是生活随笔為你收集整理的zookeeper配置中心的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。