Zookeeper安装入门详解,简单使用

专栏收录该内容

Hi I'm Shendi

最近尝试 Dubbo,Dubbo官网文档使用Zookeeper来做服务发现,于是这里先学习一下Zookeeper



Zookeeper


官网:https://zookeeper.apache.org

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。



下载

下载:https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.8.1/apache-zookeeper-3.8.1-bin.tar.gz

上面链接下载网速比较慢,可以使用镜像地址下载

https://mirrors.cnnic.cn/apache/zookeeper/


下载带 bin 的文件

下载完后解压

我windows系统使用winrar解压报压缩文件损坏,最开始还真以为压缩文件损坏,后来直接用自带的tar命令来解压了

tar -zxvf 压缩文件

当然,没有这个命令的可以尝试使用其他压缩软件


解压完后目录结构如下

Zookeeper目录结构



配置

在 conf 文件夹下有三个文件,将其中的 zoo_sample.cfg 复制一份命名为 zoo.cfg。或者直接重命名为zoo.cfg,这个是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.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

在上面配置文件更改或增加以下配置

# 数据目录,默认日志也在这个目录,全路径
dataDir=H:/data
# 日志目录
dataLogDir=H:/log

可以根据需要自行修改,关于配置文件的更多信息可以查阅相关文档



启动

进入 Zookeeper 的 bin 文件夹,找到 zkServer,windows就启动cmd后缀的文件,Linux就启动sh后缀

bin文件夹


不出意外的话最后一行内容为

ZooKeeper audit is disabled.

Zookeeper


据说有些人使用3.8版本的闪退,可以先检查一下配置部分,是否有 zoo.cfg 文件,配置里的 clientPort 对应的端口是否被占用,如果都没问题依然闪退的话,那就降低版本吧,3.4左右的


服务启动好了可以尝试启动客户端了,bin目录下有文件 zkCli,同之前一样,windows启动cmd,linux启动sh

效果如下

zkClient启动


在 zkClient 中可以输入命令来操作,随便输入个没有的命令就会弹出命令列表(相当于help)

列表


命令 参数 示例 描述
create -s(创建顺序节点),-e(创建临时节点) create -s -e /nodepath nodedata 创建节点,如果节点是临时的,那么不能含有任何子节点
get get /nodepath 获取节点的值,例如nodedata
set set /nodepath nodedata2 设置节点的值
delete delete /nodepath 删除节点
stat stat /nodepath 节点状态
quit 退出客户端


因为我是为了尝试Dubbo来学习Zookeeper,关于ZK的操作部分其他框架大部分都封装好了,所以这里我的目的已经达到了




END

本文链接:https://sdpro.top/blog/html/article/1084.html

♥ 赞助 ♥

尽管去做,或许最终的结果不尽人意,但你不付出,他不付出,那怎会进步呢?