下载安装
下载
进入官方下载网站,选择最近的稳定版进行下载
软件要求
安装jdk,且版本在java7以上
安装
单机部署
- 将下载好的文件放到对应目录,linux系统一般将该文件放在
/user/local
下面 - 解压文件:
tar xzvf apache-zookeeper-3.5.5-bin.tar.gz
- 创建配置文件
conf/zoo.cfg
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
-
tickTime
单位是毫秒,用于心跳检测,最小超时时间为两倍的tickTime
。 -
dataDir
指定一个已经存在的空目录,存储zookeeper内存数据库的快照以及日志文件。 -
clientPort
指定一个端口,监听客户端的连接请求。
- 启动zookeeper
bin/zkServer.sh start
管理zookeeper存储
连接到zookeeper
bin/zkCli.sh -server 127.0.0.1:2181
list
指令
ls /
创建一个新的znode
,关联数据"my_data"
字符串
create /zk_test my_data
查看znode
关联的数据
get /zk_test
设置znode
关联的数据
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>
- 客户端版本最好与服务端版本一致
代码示例
- 创建
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
关联的数据
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();
}
}
}
- 设置
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.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();
}
}
}
配置
关闭管理台入口
编辑配置文件conf/zoo.cfg
admin.enableServer=false