14-zookeeper环境搭建

0、环境

  • java:1.8
  • zookeeper:3.5.6

1、下载

zookeeper下载点击这里

2、安装

下载完成后解压,放到你想放的目录里。先看一下zookeeper的目录结构,如下图:

进入conf目录,复制zoo_sample.cfg,并重新命名zoo.cfg。我们来看一下zoo.cfg的内容:

yml 复制代码
# 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=/Users/jackie/zkdata
#dataLogDir=/Users/jackie/dev/zookeeper-3.5.6/tmp/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

能看到大部分都是注释,我们在这里关注两个参数:

  • dataDir:这个参数是数据保存的目录,改成你想要放的目录。
  • clientPort: 这是客户端连接的端口,保证没被占用即可。

3、启动

bash 复制代码
./zkServer.sh start

我使用的是mac,如果是Windows机器,使用.cmd文件启动。启动成功后,控制台如下图:

大家注意,命令后有个start指令,zookeeper还有其他参数供使用,我列出常用的:

  • start:启动zookeeper服务。
  • stop:停止zookeeper服务。
  • status:获取zookeeper服务运行的状态。
  • restart:重启服务。
  • start-foreground: 前台启动服务

我们一起看一下status参数的效果:

Ok, 单机的zookeeper就搭建完成了。

4、zookeeper集群搭建

我们利用单台电脑搭建一个伪集群。这个集群总共包含三个节点。下来我们先搭建第一个。

4.1、修改 zoo.cfg文件

从图中的红框可以看到,总共有三处需要更改:

  1. dataDir: 每一个节点需要有不同的数据目录
  2. clientPort:每个节点需要不同的端口
  3. server.X: X代表节点的id。server. <id> = <ip> : <port1> : <port2>。port1是leader与follower心跳检测与数据同步的端口。port2是leader选举过程中用于投票的端口。在同一台机器上,都需要保持不同。
  4. admin.serverPort: zk管理端服务端口,可以通过http://localhost:8081/commands访问到。

4.2、创建myid文件

找到1节点的数据目录,创建myid文件,输入内容"1"。如果你的节点id不是1,写你自己的id。
注意这个id,取值范围在1~255,必须是整数,别乱写。

4.3、测试

按照步骤1和2,分别对3个节点配置。配置完成后挨个启动zk服务。

第三个节点就不看了,看到上面的Mode了吗,节点2现在是Leader。

好了,今天就到这了啦。

相关推荐
张3239 小时前
K8s控制器学习难点
云原生·容器·kubernetes
小猿姐10 小时前
实测对比:哪款开源 Kubernetes MySQL Operator 最值得用?(2026 深度评测)
数据库·mysql·云原生
不懂的浪漫13 小时前
mqtt-plus 架构解析(六):多 Broker 管理,如何让一个应用同时连接多个 MQTT 服务
spring boot·分布式·物联网·mqtt·架构
AI攻城狮18 小时前
Adaptive Thinking 的代价:当 AI 自己决定"想多少"
人工智能·云原生·aigc
Dontla19 小时前
Kubernetes Liveness Probe存活探针 / Readiness Probe就绪探针介绍(Startup Probe启动探针)重启容器
云原生·容器·kubernetes
AI攻城狮19 小时前
Vibe Coding 时代:为什么你不应该盲目启用 AI 编码插件
人工智能·云原生·aigc
小夏子_riotous20 小时前
openstack的使用——5. Swift服务的基本使用
linux·运维·开发语言·分布式·云计算·openstack·swift
Gofarlic_OMS21 小时前
Windchill的license合规使用报告自动化生成与审计追踪系统
大数据·运维·人工智能·云原生·自动化·云计算
cyber_两只龙宝21 小时前
【Oracle】Oracle之DQL中WHERE限制条件查询
linux·运维·数据库·云原生·oracle
刘~浪地球1 天前
消息队列--Kafka 生产环境最佳实践
分布式·kafka·linq