Zookeeper环境搭建
网址
官网:https://zookeeper.apache.org/
下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/
依赖
需要依赖 JAVA 环境
JAVA 配置:
单机
进入 ZK 解压目录下的 conf 目录,配置 ZK 配置文件 zoo.cfg,直接复制 conf 目录下 zoo_sample.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=/tmp/zookeeper 配置数据目录 dataDir=/usr/local/apps/zookeeper/zkData # 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
进入 ZK 解压目录,执行命令即可
# {start|start-foreground|stop|restart|status|print-cmd} # 启动命令 bin/zkServer.sh start # 查看状态命令 bin/zkServer.sh status
进入 ZK 客户端管理界面
bin/zkCli.sh -server 127.0.0.1:2181
客户端管理命令
# 获取客户端管理命令 help # 列举所有节点 ls / # 创建节点 create /节点名 # 获取节点信息 get /节点名 # 设置节点数据 set /节点名 数据 # 删除节点 delete /节点名
集群
配置
每个zk节点都要配置
进入 ZK 解压目录下的 conf 目录,配置 ZK 配置文件 zoo.cfg,直接复制 conf 目录下 zoo_sample.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=/tmp/dataDir # 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 # 配置zookeeper服务,server.ID=IP:C:D # ID:表示每个节点唯一ID; # IP:表示每个节点的IP; # C:表示服务器与集群中的leader服务器交换信息的端口 # D:表示leader挂掉时专门用来进行选举leader所用的端口 server.1= 192.168.239.150:2888:3888 server.2= 192.168.239.149:2888:3888 server.3= 192.168.239.151:2888:3888
配置 myid
需要在zoo.cfg中数据文件目录下配置 myid 文件,用于 ServerID标识,和配置zookeeper服务,server.ID=IP:C:D对应
# 在 192.168.239.150 集群中myid数据为 1 # 在 192.168.239.149 集群中myid数据为 2 # 在 192.168.239.151 集群中myid数据为 3
启动
依次启动三个 zookeeper 服务
# {start|start-foreground|stop|restart|status|print-cmd} # 启动命令 bin/zkServer.sh start # 查看状态命令 bin/zkServer.sh status
测试
使用 zkCli.sh 客户端,在某一台zookeeper服务中创建节点,在其他台zookeeper服务中查询到说明成功
# 启动 150 机器的zkCli客户端 bin/zkCli.sh -server 192.168.239.150:2181 # 启动 149 机器的zkCli客户端 bin/zkCli.sh -server 192.168.239.149:2181 # 启动 151 机器的zkCli客户端 bin/zkCli.sh -server 192.168.239.151:2181
客户端管理命令
# 获取客户端管理命令 help # 列举所有节点 ls / # 创建节点 create /节点名 # 获取节点信息 get /节点名 # 设置节点数据 set /节点名 数据 # 删除节点 delete /节点名