一、Zookeeper介绍
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
Zookeeper是Hadoop的一个组件,但是它却可以独立运行,而且Zookeeper还提供了Windows版本的脚本,因此在Windows环境下运行Zookeeper时也不需要Cygwin。(在windows下进行安装时,可以作为开发平台使用)
二、Zookeeper安装(单机版)
1、下载
进入Apache官网Zookeeper Home进行稳定版本下载。
2、解压到相应目录
3、修改配置
进入到conf目录将zoo_sample.cfg
文件重命名为zoo.cfg
,创建data上当和log目录,并进行以下配置:
# The number of milliseconds of each tick 心跳间隔 毫秒每次 2秒
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 初始化时 连接到服务器端的间隔次数,总时间10*2=20秒
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# ZK Leader 和follower 之间通讯的次数,总时间5*2=10秒
syncLimit=5
# the directory where the snapshot is stored. 镜像数据位置
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储内存中数据库快照的位置,如果不设置参数,更新事务日志将被存储到默认位置。
#dataDir=/tmp/zookeeper
dataDir=D:\software\BigData\Zookeeper\work\data
# 日志目录
dataLogDir=D:\software\BigData\Zookeeper\work\log
# 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
Zookeeper不会自动创建data目录和log目录,得手动创建才能启动
可以用netstat -ano|findstr “2181” 看看是否OK。
4、运行
配置过后,用命令窗口进入bin目录,运行zkServer.cmd脚本,进行启动。
可以用JPS 查看启动的JAVA 进程的情况,会出现这样
.\zkServer.cmd
C:\windows\system32>jps
8068
10040 QuorumPeerMain // 这东西是zk的东西,源码有介绍
10556 Jps
也可以用自带客户端命令 : zkCli.cmd -server 127.0.0.1:2181
三、Zookeeper安装(伪集群版)
1、配置文件设置
Zookeeper除了能够进行单机安装也可以进行伪集群安装和集群安装。伪集群的搭建和单机版本基本差不多,简单的说就是将上面的配置多拷贝几份,然后配置不同的地址和端口。操作如下:
# The number of milliseconds of each tick 心跳间隔 毫秒每次 2秒
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 初始化时 连接到服务器端的间隔次数,总时间10*2=20秒
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# ZK Leader 和follower 之间通讯的次数,总时间5*2=10秒
syncLimit=5
# the directory where the snapshot is stored. 镜像数据位置
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储内存中数据库快照的位置,如果不设置参数,更新事务日志将被存储到默认位置。
# dataDir=/tmp/zookeeper
# 在其它配置文件中写上对就的目录zk2、zk3
dataDir=D:\software\BigData\Zookeeper\work\data\zk1
# 日志目录
dataLogDir=D:\software\BigData\Zookeeper\work\log\zk1
# the port at which the clients will connect 客户端连接的端口
clientPort=2181
#2887 是server 之间通讯的,3887 是应用程序通讯的
# 同时加入其他两个服务的地址和端口信息
server.1=127.0.0.1:2887:3887
server.2=127.0.0.1:2888:3888
server.3=127.0.0.1:2889:3889
# 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
在文件目录下创建3份,zoo1.cfg,zoo2.cfg,zoo3,cfg 还需改参数
在一台机器上部署了3个server,需要注意的是在集群为分布式模式下我们使用的每个配置文档模拟一台机器,也就是说单台机器及上运行多个Zookeeper实例。但是,必须保证每个配置文档的各个端口号不能冲突,除了clientPort不同之外,dataDir也不同。另外,还要在dataDir所对应的目录中创建myid文件来指定对应的Zookeeper服务器实例。
- clientPort端口:如果在1台机器上部署多个server,那么每台机器都要不同的 clientPort,比如 server1是2181,server2是2182,server3是2183
- dataDir和dataLogDir:dataDir和dataLogDir也需要区分下,将数据文件和日志文件分开存放,同时每个server的这两变量所对应的路径都是不同的
- server.X和myid: server.X 这个数字就是对应,data/myid中的数字。在3个server的myid文件中分别写入了0,1,2,那么每个server中的zoo.cfg都配server.0 server.1,server.2就行了。因为在同一台机器上,后面连着的2个端口,3个server都不要一样,否则端口冲突
2、修改zkServer.cmd
除了这些外还要修改zkServer.cmd
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
# 读取配置的路径,每个启动服务对应一份
set ZOOCFG=..\conf\zoo1.cfg
# 同理创建3个zkServer1.cmd,zkServer2.cmd,zkServer3.cmd,修改成对应的配置文件。
3、创建myid文件
还得在dataDir 指定目录,也就是D:\software\BigData\Zookeeper\work\log\zk1
下创建myid 的文件,内容对应配置文件zoo.cof中server后的数字1、2、3 即可。这个的数字是唯一的,在1-255 之间,用来表示自身的id。
四、参考文档
ZooKeeper Getting Started Guide
zookeeper windows 入门安装和测试
zookeeper 安装 windows环境