1、下载zookeeper安装包
下载地址:http://zookeeper.apache.org/releases.html
2、创建存放的目录解压:
mkdir /usr/local/zookeeper
tar -zxvf zookeeper-3.4.11.tar.gz
3、进入zookeeper/conf/目录下,将改目录下的zoo_sample.cfg配置文件拷贝4份,依次为zoo.cfg zoo1.cfg zoo2.cfg zoo3.cfg
4、使用vim编辑zoo1.cfg zoo2.cfg zoo3.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=/usr/local/zookeeper/zookeeper-3.4.11/server1/data
dataLogDir=/usr/local/zookeeper/zookeeper-3.4.11/server1/logs
# 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:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
配置说明
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒
syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10秒
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
5、使用vim修改zoo2.cfg的dataDir=dataDir=/usr/local/zookeeper/zookeeper-3.4.11/server2/data
dataLogDir=/usr/local/zookeeper/zookeeper-3.4.11/server2/logs
,clientPort=2182,修改zoo3.cfg的dataDir=/usr/local/zookeeper/zookeeper-3.4.11/server3/data
dataLogDir=/usr/local/zookeeper/zookeeper-3.4.11/server3/logs
clientPort=2183。dataDir目录开始不存在,需要手动创建,并在各data文件下创建名为myid的文件,文件内容对应服务器编号
6、启动
进入目录/usr/local/zookeeper/zookeeper-3.4.11/bin目录,执行命令:
7、联通测试
Connecting to 127.0.0.1:21822018-04-15 11:37:12,454 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT2018-04-15 11:37:12,457 [myid:] - INFO [main:Environment@100] - Client environment:host.name=izbp1a3y9d1a133g371xe3z2018-04-15 11:37:12,458 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_1612018-04-15 11:37:12,459 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.8.0_161/jre2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper-3.4.11/bin/../build/classes:/usr/local/zookeeper/zookeeper-3.4.11/bin/../build/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../zookeeper-3.4.11.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../src/java/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../conf:2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.2.2.el7.x86_64
2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:user.name=lzb
2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/lzb
2018-04-15 11:37:12,460 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.11/bin
2018-04-15 11:37:12,461 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=127.0.0.1:2182 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90
2018-04-15 11:37:12,500 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1035] - Opening socket connection to server 127.0.0.1/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2018-04-15 11:37:12,624 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@877] - Socket connection established to 127.0.0.1/127.0.0.1:2182, initiating session
2018-04-15 11:37:12,665 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1302] - Session establishment complete on server 127.0.0.1/127.0.0.1:2182, sessionid = 0x20000c043e90000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2182(CONNECTED) 0]
出现以上信息就表示成功了