下載安裝
下載
進入官方下載網(wǎng)站赂乐,選擇最近的穩(wěn)定版進行下載
軟件要求
安裝jdk富腊,且版本在java7以上
安裝
單機部署
- 將下載好的文件放到對應(yīng)目錄狮惜,linux系統(tǒng)一般將該文件放在
/user/local
下面 - 解壓文件:
tar xzvf apache-zookeeper-3.5.5-bin.tar.gz
- 創(chuàng)建配置文件
conf/zoo.cfg
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
-
tickTime
單位是毫秒圾旨,用于心跳檢測与涡,最小超時時間為兩倍的tickTime
惹谐。 -
dataDir
指定一個已經(jīng)存在的空目錄,存儲zookeeper內(nèi)存數(shù)據(jù)庫的快照以及日志文件驼卖。 -
clientPort
指定一個端口氨肌,監(jiān)聽客戶端的連接請求。
- 啟動zookeeper
bin/zkServer.sh start
管理zookeeper存儲
連接到zookeeper
bin/zkCli.sh -server 127.0.0.1:2181
list
指令
ls /
創(chuàng)建一個新的znode
酌畜,關(guān)聯(lián)數(shù)據(jù)"my_data"
字符串
create /zk_test my_data
查看znode
關(guān)聯(lián)的數(shù)據(jù)
get /zk_test
設(shè)置znode
關(guān)聯(lián)的數(shù)據(jù)
set /zk_test junk
刪除znode
delete /zk_test
Java客戶端
maven加入zookeeper依賴
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.5</version>
</dependency>
- 客戶端版本最好與服務(wù)端版本一致
代碼示例
- 創(chuàng)建
znode
public class ZooKeeperClient {
private static ZooKeeper zooKeeper;
public static void main(String[] args){
try {
zooKeeper = new ZooKeeper("120.78.228.80:3181",5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
if(event.getState() == KeeperState.SyncConnected) {
String msg = zooKeeper.create("/zk_test", "my_data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(msg);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 查看
znode
關(guān)聯(lián)的數(shù)據(jù)
public class ZooKeeperClient {
private static ZooKeeper zooKeeper;
public static void main(String[] args){
try {
zooKeeper = new ZooKeeper("120.78.228.80:3181",5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
if(event.getState() == KeeperState.SyncConnected) {
byte[] bytes = zooKeeper.getData("/zk_test", false, null);
System.out.println(new String(bytes));
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 設(shè)置
znode
關(guān)聯(lián)的數(shù)據(jù)
public class ZooKeeperClient {
private static ZooKeeper zooKeeper;
public static void main(String[] args){
try {
zooKeeper = new ZooKeeper("120.78.228.80:3181",5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
if(event.getState() == KeeperState.SyncConnected) {
zooKeeper.setData("/zk_test", "junk".getBytes(), -1);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 刪除
znode
public class ZooKeeperClient {
private static ZooKeeper zooKeeper;
public static void main(String[] args){
try {
zooKeeper = new ZooKeeper("120.78.228.80:3181",5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
if(event.getState() == KeeperState.SyncConnected) {
zooKeeper.delete("/zk_test", -1);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
配置
關(guān)閉管理臺入口
編輯配置文件conf/zoo.cfg
admin.enableServer=false