集群最少要运行3个实例,这里就运行3个实例。
1.去官网下载Zookeeper的安装包,下载地址:https://archive.apache.org/dist/zookeeper/ 2. 解压到本地,并且拷贝3份出来 tar -xzvf zookeeper-3.4.5.tar.gz 3. 修改conf中的配置文件
进入到conf 文件夹中,重命名原来的zoo_sample.cfg 文件 ,mv zoo_sample.cfg zoo.cfg编辑zoo.cfg,主要修改如下参数 clientPort:端口,一台机器上面的不同的实例端口不同即可dataDir和dataLogDir : Zookeeper实例存储的数据以及日志存储的路径,不同即可;dataDir下面要创建一个myid 文件,里面只有一个数字,不同的实例数字不同即可。server.X和myid:server.X 这个数字就是对应 ,dataDir/myid中的数字。即每个实例用一个数字代替,比如这3个server的myid文件中分别写入1、2、3,那么这三个server的zoo.cfg里面配置就是server1,server2,server3。server1的zoo.cfg文件配置如下
1 # The number of milliseconds of each tick # 发送心跳的间隔时间,单位毫秒 2 tickTime=2000 3 # The number of ticks that the initial 4 # synchronization phase can take # 用来配置Zookeeper客户端(连接Leader服务器的Follower服务器)初始化连接时最长能忍受多少个心跳时间(也就是tickTime)的间隔数 5 initLimit=10 6 # The number of ticks that can pass between 7 # sending a request and getting an acknowledgement # 标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度 8 syncLimit=5 9 # the directory where the snapshot is stored. 10 # do not use /tmp for storage, /tmp here is just 11 # example sakes. # 数据存放路径 12 dataDir=/Users/jiachang/sunny/myserver/zookeeper/dataDir/data1 13 # the port at which the clients will connect # 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求 14 clientPort=2181 15 16 dataLogDir=/Users/jiachang/sunny/myserver/zookeeper/dataLog/log1 17 # server.A=B:C:D # A:几号服务器 # B:这个服务器的IP地址 # C:这个服务器与集群中的 Leader 服务器交换信息的端口 # D:表示的是万一 集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口 18 server.1=localhost:2281:3381 19 server.2=localhost:2282:3382 20 server.3=localhost:2283:3383 21 # 22 # Be sure to read the maintenance section of the 23 # administrator guide before turning on autopurge. 24 # 25 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance 26 # 27 # The number of snapshots to retain in dataDir 28 #autopurge.snapRetainCount=3 29 # Purge task interval in hours 30 # Set to "0" to disable auto purge feature 31 #autopurge.purgeInterval=1server2的zoo.cfg文件配置如下
# The number of milliseconds of each tick 2 tickTime=2000 3 # The number of ticks that the initial 4 # synchronization phase can take 5 initLimit=10 6 # The number of ticks that can pass between 7 # sending a request and getting an acknowledgement 8 syncLimit=5 9 # the directory where the snapshot is stored. 10 # do not use /tmp for storage, /tmp here is just 11 # example sakes. 12 dataDir=/Users/jiachang/sunny/myserver/zookeeper/dataDir/data2 13 # the port at which the clients will connect 14 clientPort=2182 15 16 dataLogDir=/Users/jiachang/sunny/myserver/zookeeper/dataLog/log2 17 18 server.1=localhost:2281:3381 19 server.2=localhost:2282:3382 20 server.3=localhost:2283:3383 21 22 # 23 # Be sure to read the maintenance section of the 24 # administrator guide before turning on autopurge. 25 # 26 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance 27 # 28 # The number of snapshots to retain in dataDir 29 #autopurge.snapRetainCount=3 30 # Purge task interval in hours 31 # Set to "0" to disable auto purge feature 32 #autopurge.purgeInterval=1server3的zoo.cfg文件配置如下
1 # The number of milliseconds of each tick 2 tickTime=2000 3 # The number of ticks that the initial 4 # synchronization phase can take 5 initLimit=10 6 # The number of ticks that can pass between 7 # sending a request and getting an acknowledgement 8 syncLimit=5 9 # the directory where the snapshot is stored. 10 # do not use /tmp for storage, /tmp here is just 11 # example sakes. 12 dataDir=/Users/jiachang/sunny/myserver/zookeeper/dataDir/data3 13 # the port at which the clients will connect 14 clientPort=2183 15 16 dataLogDir=/Users/jiachang/sunny/myserver/zookeeper/dataLog/log3 17 18 server.1=localhost:2281:3381 19 server.2=localhost:2282:3382 20 server.3=localhost:2283:3383 21 22 # 23 # Be sure to read the maintenance section of the 24 # administrator guide before turning on autopurge. 25 # 26 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance 27 # 28 # The number of snapshots to retain in dataDir 29 #autopurge.snapRetainCount=3 30 # Purge task interval in hours 31 # Set to "0" to disable auto purge feature 32 #autopurge.purgeInterval=1 Zookeeper 的启动 分别进入对应server的bin文件夹,执行启动命令./zkServer.sh start,就会看到如下启动页面,代表启动成功了,如果启动不成功,说明配置的有问题 查看服务的运行状态跟伪集群的相似,只是配置server.x配置的那里换成实际的IP即可。
只要运行一个server即可。