zookeeper 的三種api
采用curator進(jìn)行簡單的配置中心案例學(xué)習(xí)
基于curator 的分布式鎖,計(jì)數(shù)器寞酿!
maple-zookeeper
zookeeper 學(xué)習(xí) 家夺,更多請參閱我的碼云,參照源碼學(xué)習(xí)更快,碼云
重點(diǎn)對三種客戶端的學(xué)習(xí)研究熟嫩,關(guān)于zookeeper底層實(shí)現(xiàn)秦踪,沒有過多研究
![輸入圖片說明](https://git.oschina.net/uploads/images/2017/0728/173920_6c0f5000_1147300.jpeg)
1.Zookeeper安裝部署
Zookeeper的部署很簡單,如果已經(jīng)有Java運(yùn)行環(huán)境的話掸茅,下載tarball解壓后即可運(yùn)行。
[root@vm Temp]$ wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
[root@vm Temp]$ tar zxvf zookeeper-3.4.6.tar.gz
[root@vm Temp]$ cd zookeeper-3.4.6
[root@vm zookeeper-3.4.6]$ cp conf/zoo_sample.cfg conf/zoo.cfg
[root@vm zookeeper-3.4.6]$ export ZOOKEEPER_HOME=/usr/local/src/zookeeper-3.4.5
[root@vm zookeeper-3.4.6]$ export PATH=$ZOOKEEPER_HOME/bin:$PATH
[root@vm zookeeper-3.4.6]$ bin/zkServer.sh start
[root@vm zookeeper-3.4.6]$ bin/zkCli.sh -server 127.0.0.1:2181
2.客戶端常用操作
用zkCli.sh連接上Zookeeper服務(wù)后柠逞,用help能列出所有命令:
[root@BC-VM-edce4ac67d304079868c0bb265337bd4 zookeeper-3.4.6]# bin/zkCli.sh -127.0.0.1:2181
Connecting to localhost:2181
2015-06-11 10:55:14,387 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
...
[zk: localhost:2181(CONNECTED) 5] help
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
3.用Curator管理Zookeeper
Curator的Maven依賴如下昧狮,一般直接使用curator-recipes就行了,如果需要自己封裝一些底層些的功能的話板壮,例如增加連接管理重試機(jī)制等逗鸣,則可以引入curator-framework包。
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.7.0</version>
</dependency>
3.1 Client操作
package com.may.curator.api;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
/**
* 利用Curator進(jìn)行CRUD
*
* @author youJie
* @date 2017-07-28 16:15
*
*/
public class CuratorClient {
/** Zookeeper info */
private static final String ZK_ADDRESS = "10.0.14.79:2181";
private static final String ZK_PATH = "/zktest";
public static void main(String[] args) throws Exception {
// 1.Connect to zk
CuratorFramework client = CuratorFrameworkFactory.newClient(
ZK_ADDRESS,
new RetryNTimes(10, 5000)
);
client.start();
System.out.println("zk client start successfully!");
/** 2.Client API test*/
// 2.1 Create node
String data1 = "hello";
print("create", ZK_PATH, data1);
client.create().
creatingParentsIfNeeded().
forPath(ZK_PATH, data1.getBytes());
// 2.2 Get node and data
print("ls", "/");
print(client.getChildren().forPath("/"));
print("get", ZK_PATH);
print(client.getData().forPath(ZK_PATH));
// 2.3 Modify data
String data2 = "world";
print("set", ZK_PATH, data2);
client.setData().forPath(ZK_PATH, data2.getBytes());
print("get", ZK_PATH);
print(client.getData().forPath(ZK_PATH));
// 2.4 Remove node
print("delete", ZK_PATH);
client.delete().forPath(ZK_PATH);
print("ls", "/");
print(client.getChildren().forPath("/"));
}
private static void print(String... cmds) {
StringBuilder text = new StringBuilder("$ ");
for (String cmd : cmds) {
text.append(cmd).append(" ");
}
System.out.println(text.toString());
}
private static void print(Object result) {
System.out.println(
result instanceof byte[]
? new String((byte[]) result)
: result);
}
}
3.2 監(jiān)聽器
Curator提供了三種Watcher(Cache)來監(jiān)聽結(jié)點(diǎn)的變化:
- Path Cache:監(jiān)視一個(gè)路徑下1)孩子結(jié)點(diǎn)的創(chuàng)建绰精、2)刪除撒璧,3)以及結(jié)點(diǎn)數(shù)據(jù)的更新。產(chǎn)生的事件會(huì)傳遞給注冊的PathChildrenCacheListener笨使。
- Node Cache:監(jiān)視一個(gè)結(jié)點(diǎn)的創(chuàng)建卿樱、更新、刪除硫椰,并將結(jié)點(diǎn)的數(shù)據(jù)緩存在本地繁调。
- Tree Cache:Path Cache和Node Cache的“合體”,監(jiān)視路徑下的創(chuàng)建靶草、更新蹄胰、刪除事件,并緩存路徑下所有孩子結(jié)點(diǎn)的數(shù)據(jù)奕翔。
package com.may.curator.api;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.retry.RetryNTimes;
public class CuratorPathWatcher {
/** Zookeeper info */
private static final String ZK_ADDRESS = "10.0.14.79:2181";
private static final String ZK_PATH = "/zktest";
public static void main(String[] args) throws Exception {
// 1.Connect to zk
CuratorFramework client = CuratorFrameworkFactory.newClient(
ZK_ADDRESS,
new RetryNTimes(10, 5000)
);
client.start();
System.out.println("zk client start successfully!");
// 2.Register watcher
PathChildrenCache watcher = new PathChildrenCache(
client,
ZK_PATH,
true // if cache data
);
watcher.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
ChildData data = event.getData();
if (data == null) {
System.out.println("No data in event[" + event + "]");
} else {
System.out.println("Receive event: "
+ "type=[" + event.getType() + "]"
+ ", path=[" + data.getPath() + "]"
+ ", data=[" + new String(data.getData()) + "]"
+ ", stat=[" + data.getStat() + "]");
}
}
});
/*watcher.getListenable().addListener((client1, event) -> {
ChildData data = event.getData();
if (data == null) {
System.out.println("No data in event[" + event + "]");
} else {
System.out.println("Receive event: "
+ "type=[" + event.getType() + "]"
+ ", path=[" + data.getPath() + "]"
+ ", data=[" + new String(data.getData()) + "]"
+ ", stat=[" + data.getStat() + "]");
}
});*/
watcher.start(StartMode.BUILD_INITIAL_CACHE);
System.out.println("Register zk watcher successfully!");
Thread.sleep(Integer.MAX_VALUE);
}
}
4.Curator“菜譜”
既然Maven包叫做curator-recipes裕寨,那說明Curator有它獨(dú)特的“菜譜”:
- 鎖:包括共享鎖、共享可重入鎖派继、讀寫鎖等宾袜。
- 選舉:Leader選舉算法。
- Barrier:阻止分布式計(jì)算直至某個(gè)條件被滿足的“柵欄”互艾,可以看做JDK Concurrent包中Barrier的分布式實(shí)現(xiàn)试和。
- 緩存:前面提到過的三種Cache及監(jiān)聽機(jī)制。
- 持久化結(jié)點(diǎn):連接或Session終止后仍然在Zookeeper中存在的結(jié)點(diǎn)纫普。
- 隊(duì)列:分布式隊(duì)列阅悍、分布式優(yōu)先級(jí)隊(duì)列等好渠。
4.1 分布式鎖
分布式編程時(shí),比如最容易碰到的情況就是應(yīng)用程序在線上多機(jī)部署节视,于是當(dāng)多個(gè)應(yīng)用同時(shí)訪問某一資源時(shí)拳锚,就需要某種機(jī)制去協(xié)調(diào)它們。例如寻行,現(xiàn)在一臺(tái)應(yīng)用正在rebuild緩存內(nèi)容霍掺,要臨時(shí)鎖住某個(gè)區(qū)域暫時(shí)不讓訪問;又比如調(diào)度程序每次只想一個(gè)任務(wù)被一臺(tái)應(yīng)用執(zhí)行等等拌蜘。
下面的程序會(huì)啟動(dòng)兩個(gè)線程t1和t2去爭奪鎖杆烁,拿到鎖的線程會(huì)占用5秒。運(yùn)行多次可以觀察到简卧,有時(shí)是t1先拿到鎖而t2等待兔魂,有時(shí)又會(huì)反過來。Curator會(huì)用我們提供的lock路徑的結(jié)點(diǎn)作為全局鎖举娩,這個(gè)結(jié)點(diǎn)的數(shù)據(jù)類似這種格式:[_c_64e0811f-9475-44ca-aa36-c1db65ae5350-lock-0000000005]析校,每次獲得鎖時(shí)會(huì)生成這種串,釋放鎖時(shí)清空數(shù)據(jù)铜涉。
4.2 Leader選舉
當(dāng)集群里的某個(gè)服務(wù)down機(jī)時(shí)智玻,我們可能要從slave結(jié)點(diǎn)里選出一個(gè)作為新的master,這時(shí)就需要一套能在分布式環(huán)境中自動(dòng)協(xié)調(diào)的Leader選舉方法芙代。Curator提供了LeaderSelector監(jiān)聽器實(shí)現(xiàn)Leader選舉功能吊奢。同一時(shí)刻,只有一個(gè)Listener會(huì)進(jìn)入takeLeadership()方法链蕊,說明它是當(dāng)前的Leader事甜。注意:當(dāng)Listener從takeLeadership()退出時(shí)就說明它放棄了“Leader身份”,這時(shí)Curator會(huì)利用Zookeeper再從剩余的Listener中選出一個(gè)新的Leader滔韵。autoRequeue()方法使放棄Leadership的Listener有機(jī)會(huì)重新獲得Leadership逻谦,如果不設(shè)置的話放棄了的Listener是不會(huì)再變成Leader的。