安装准备
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它是一个为分布式应用提供一致性服务的软件,它是Apache Hadoop和HBase的重要组成部分。ZooKeeper的安装需要满足一定的系统环境,主要有以下几点:
- 操作系统:Windows/Linux/macOS
- 内存:不低于512MB
- 硬盘:至少有3GB的可用空间
- Java:JDK1.6及以上
安装步骤
1. 下载ZooKeeper,从官方网站下载ZooKeeper安装包,下载地址:http://zookeeper.apache.org/releases.html,选择合适的版本,下载完成后,将压缩包解压到指定的目录。
2. 配置环境变量,将解压出来的bin目录添加到系统环境变量中,以便可以在任何目录下运行ZooKeeper的命令行工具。
3. 配置ZooKeeper配置文件,在bin目录下有一个zoo.cfg文件,它是ZooKeeper的配置文件,我们可以在这个文件中配置ZooKeeper的参数,例如:
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. dataDir=/tmp/zookeeper # the port at which the clients will connect clientPort=2181
4. 启动ZooKeeper,在bin目录下,执行命令“zkServer.sh start”,如果提示“JMX enabled by default”,则表示启动成功。
使用方法
1. 连接ZooKeeper,使用命令“zkCli.sh -server 127.0.0.1:2181”来连接ZooKeeper,如果提示“Connecting to 127.0.0.1:2181”,则表示连接成功。
2. 查看根节点,使用命令“ls /”可以查看根节点,如果提示“[]”,则表示根节点下没有任何子节点。
3. 创建节点,使用命令“create /node1 hello”可以创建一个名为node1的节点,并且设置节点的值为hello,如果提示“Created /node1”,则表示创建成功。
4. 读取节点,使用命令“get /node1”可以读取节点node1的值,如果提示“hello”,则表示读取成功。
5. 修改节点,使用命令“set /node1 world”可以修改节点node1的值,如果提示“world”,则表示修改成功。
6. 删除节点,使用命令“delete /node1”可以删除节点node1,如果提示“Node deleted”,则表示删除成功。
7. 退出,使用命令“quit”可以退出ZooKeeper的客户端,如果提示“Exiting”,则表示退出成功。