18- Zookeeper单机伪集群搭建

Zookeeper的来源

为什么叫动物园管理员呢,因为他是hadoop(大象)的一个子项目,用来管理hadoop和hive(蜜蜂)还有pig(猪)等项目的,所以就叫做了动物园管理员了。

搭建集群

  1. 首先下载zookeeper
    下载地址

下载之后找到合适的位置解压

2.创建zk数据文件
在一个合适的位置创建zoo_nodes(名称随意)文件夹,然后再里面分别创建node1 node2 node3三个文件夹每个文件夹中海油data和log两个文件夹

《18- Zookeeper单机伪集群搭建》 image.png

在data文件夹中创建myid文件(注意没有后缀),文件内容分别为1,2,3

  1. 修改配置文件
    找到conf下面的zoo_sample.cfg文件复制3份,命名为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.
# 注意这里,在windows中必须要使用转义字符!!!!
dataDir=D:\\zhouyang\\dev\\zoo_nodes\\node1\\data
dataLogDir=D:\\zhouyang\\dev\\zoo_nodes\\node1\\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

server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890

  • 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所用的端口。

  1. 修改cmd文件并启动
    进入bin目录,把zkServer.cmd负责3分,命名为zkServer1.cmd zkServer2.cmd zkServer3.cmd

然后分别修改,添加set ZOOCFG=..\conf\zoo1.cfg

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain

set ZOOCFG=..\conf\zoo1.cfg
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

然后分别执行zkServer1.cmd zkServer2.cmd zkServer3.cmd

Linux系统下直接在zookeeper根目录执行以下命令就可以

./bin/zkServer.sh start zoo1.cfg
./bin/zkServer.sh start zoo2.cfg
./bin/zkServer.sh start zoo3.cfg

现在zookeeper单机伪集群就建立起来了

    原文作者:熙熙爸爸
    原文地址: https://www.jianshu.com/p/1cea33aa8d06
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞