java环境1.6往上
下载地址:https://zookeeper.apache.org/releases.html#download
两个文件都要下载
两个压缩包下载后,解压缩合并文件夹:
目录结构如下:
bin目录: zk的可执行脚本目录,包括zk服务进程,zk客户端,等脚本。其中,.sh是Linux环境下的脚本,.cmd是Windows环境下的脚本。 conf目录: 配置文件目录。zoo_sample.cfg为样例配置文件,需要修改为自己的名称,一般为zoo.cfg。log4j.properties为日志配置文件。 lib目录: zk依赖的包。
复制conf目录下文件zoo_sample.cfg,改名为zoo.cfg,内容如下图
# 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. # # http://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.httpPort=7000 #metricsProvider.exportJvmInfo=true
主要参数:
tickTime 时长单位为毫秒,为zk使用的基本时间度量单位。例如,1 * tickTime是客户端与zk服务端的心跳时间,2 * tickTime是客户端会话的超时时间。 tickTime的默认值为2000毫秒,更低的tickTime值可以更快地发现超时问题,但也会导致更高的网络流量(心跳消息)和更高的CPU使用率(会话的跟踪处理)。 clientPort zk服务进程监听的TCP端口,默认情况下,服务端会监听2181端口。 dataDir 无默认配置,必须配置实际路径,用于配置存储快照文件的目录。如果没有配置dataLogDir,那么事务日志也会存储在此目录。
运行zookepper服务:
window:直接运行bin目录 zkServer.cmd
linux: ./zkServer.sh start
客户端连接Zookepper:
单体cmd运行
zkCli.cmd -server localhost:2181
集群:
zkCli.cmd -server 192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181
如下图客户端连接成功:



