Zookeeper环境搭建

本文详细介绍了如何搭建Zookeeper环境,包括JAVA配置、单机与集群模式下的配置步骤,以及Zookeeper服务的启动、状态查看和客户端管理命令。同时,提供了集群配置的详细指南,包括myid文件配置和各节点的参数设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 /节点名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Simba1949

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值