前提是要安装好Java环境 ^_^

安装

Download

然后解压到安装目录即可.这里假设是安装在/ihome/java/zookeeper-3.4.8

启动模式

配置文件:解压后,在安装目录下的conf/zoo_sample.cfg样板配置文件。

独立模式

创建数据目录(默认是在/tmp目录下,不过建议修改下): mkdir -p /ihome/java/zookeeper-3.4.8/data/z1

然后复制一份配置文件到该目录下:

cp /ihome/java/zookeeper-3.4.8/conf/zoo_sample.cfg /ihome/java/zookeeper-3.4.8/data/z1/zoo.cfg

修改一下数据目录的指向:

dataDir=/ihome/java/zookeeper-3.4.8/data/z1

然后启动:

╭─sky@sky-linux /ihome/java/zookeeper-3.4.8/data/z1  
╰─➤  /ihome/java/zookeeper-3.4.8/bin/zkServer.sh start ./zoo.cfg

这时,可以使用客户端登录即可:

╭─sky@sky-linux /ihome/java/zookeeper-3.4.8/data/z1  
╰─➤  /ihome/java/zookeeper-3.4.8/bin/zkCli.sh                                                                                                                                          130 ↵
Connecting to localhost:2181
2016-05-12 18:29:52,341 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2016-05-12 18:29:52,343 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=sky-linux
2016-05-12 18:29:52,343 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_60
...
2016-05-12 18:29:52,409 [myid:] - INFO  [main-SendThread(ip6-localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server ip6-localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x154a47364d60005, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] 

如果看到Session establishment complete on server就表示成功了。

集群模式

假设有三个节点,数据目录分别为: /ihome/java/zookeeper-3.4.8/data/z1 /ihome/java/zookeeper-3.4.8/data/z2 /ihome/java/zookeeper-3.4.8/data/z3

节点z1

节点z1的配置文件(/ihome/java/zookeeper-3.4.8/data/z1/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=/ihome/java/zookeeper-3.4.8/data/z1
# 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

server.1=127.0.0.1:2222:2223
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445

id文件为:(/ihome/java/zookeeper-3.4.8/data/z1/myid),内容为:

╭─sky@sky-linux /ihome/java/zookeeper-3.4.8/data/z1  
╰─➤  cat myid 
1

其他2个的配置文件类似,主要修改下:

每个节点都有以下节点配置信息:

server.1=127.0.0.1:2222:2223
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445

其中server.N,就是myid文件里的数字.127.0.0.1表示节点的IP地址,2222端口:用于仲裁通信,2223端口:用于群首选举.

然后每个节点的dataDir要修改为自己的: z2的配置文件,将数据目录修改为:

dataDir=/ihome/java/zookeeper-3.4.8/data/z2

z2的配置文件,将客户端连接的端口修改为:

clientPort=2182

z3的配置文件,将数据目录修改为:

dataDir=/ihome/java/zookeeper-3.4.8/data/z3

z3的配置文件,将客户端连接的端口修改为:

clientPort=2183

启动集群

启动节点z1:

╭─sky@sky-linux /ihome/java/zookeeper-3.4.8/data/z1  
╰─➤  /ihome/java/zookeeper-3.4.8/bin/zkServer.sh start ./zoo.cfg 

启动节点z2:

╭─sky@sky-linux /ihome/java/zookeeper-3.4.8/data/z2  
╰─➤  /ihome/java/zookeeper-3.4.8/bin/zkServer.sh start ./zoo.cfg 

启动节点z3:

╭─sky@sky-linux /ihome/java/zookeeper-3.4.8/data/z3  
╰─➤  /ihome/java/zookeeper-3.4.8/bin/zkServer.sh start ./zoo.cfg 

连接集群

╭─sky@sky-linux /ihome/java/zookeeper-3.4.8  
╰─➤  ./bin/zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

即后面-server连接集群每个IP地址:客户端端口 客户端端口,即是上面的clientPort.

ZooKeeper的服务器搭建就这样子了.^_^

连接字符串

IP1:PORT1,IP2:PORT2,...,IPn:PORTn