介绍
下载
wget http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
解压
sudo cp zookeeper-3.4.9.tar.gz /home/zookeeper
sudo tar -zxvf zookeeper-3.4.9.tar.gz
复制(2n+1)个zk服务
这里创建3个
sudo mv zookeeper-3.4.9 zk1
sudo cp -R zk1 zk2
sudo cp -R zk1 zk3
配置
1.重命名 zk1/conf/zoo_sample.cfg 为zk1/conf/zoo.cfg
2.在zk/创建dataDir目录(mkdir dataDir)
3.在dataDir目录下创建myid(touch myid),将myid的文件内容写成1
4.修改zoo.cfg的配置:
# Client-Server通信心跳时间
tickTime=2000
# Leader-Follower初始通信时限
initLimit=10
# Leader-Follower同步通信时限
syncLimit=5
# 数据文件目录
dataDir=/home/zookeeper/zk1/dataDir
# 客户端连接端口
clientPort=2181
# 集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
5.用上述步骤将zk2和zk3都操作一遍,注意myid文件和zoo.conf文件中的dataDir和clientPort配置要 zk2和zk3的myid文件内容分别对应2和3 ,clientPort的配置分别对应2182和283,dataDir的配置分别修改/home/zookeeper/zk2/dataDir和/home/zookeeper/zk3/dataDir
启动
sudo /home/zookeeper/zk1/bin/zkServer.sh start /home/zookeeper/zk1/conf/zoo.cfgsudo /home/zookeeper/zk2/bin/zkServer.sh start /home/zookeeper/zk2/conf/zoo.cfgsudo /home/zookeeper/zk3/bin/zkServer.sh start /home/zookeeper/zk3/conf/zoo.cfg
验证
sudo /home/zookeeper/zk1/bin/zkServer.sh status /home/zookeeper/zk1/conf/zoo.cfg
输出内容:
ZooKeeper JMX enabled by defaultUsing config: /home/zookeeper/zk1/conf/zoo.cfgMode: follower
客户端连接验证;
/home/zookeeper/zk1/bin/zkCli.sh -server 127.0.0.1:2181
输出下面信息
2017-01-23 11:09:52,931 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4e49968bWelcome to ZooKeeper!2017-01-23 11:09:52,961 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)2017-01-23 11:09:52,968 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating sessionJLine support is enabled2017-01-23 11:09:52,980 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x159c5f916690009, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:null[zk: 127.0.0.1:2181(CONNECTED) 0]
![](http://upload-images.jianshu.io/upload_images/2071349-15452c2ebbf73882.gif?imageMogr2/auto-orient/strip)![点击并拖拽以移动](http://upload-images.jianshu.io/upload_images/2071349-15452c2ebbf73882.gif?imageMogr2/auto-orient/strip)