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。

好了,今天就到这了啦。

相关推荐
真上帝的左手5 小时前
30. 技术专题-‌应用框架组件-分布式架构
分布式·架构·系统架构·分布式架构
罗小爬EX9 小时前
Helm 从概念到实战:以一个普通 Web 应用为例
云原生·helm
分布式存储与RustFS17 小时前
镜像仓库的层别只堆本地盘:用 RustFS 给 Harbor 当 S3 后端
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
分布式存储与RustFS19 小时前
工作流脚本和依赖 JAR 散在各节点本地盘:用 DolphinScheduler 资源中心接到 RustFS
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
zixu_star19 小时前
从单机到分布式:定时任务如何处理大量数据与执行失败?
分布式
Eyfcom20 小时前
〖物流云原生实战 01〗K8s Operator 在物流定时任务中的实践:从“裸 CronJob”到“声明式任务编排”
云原生·物流管理系统·物流云原生实战
阿里云云原生1 天前
阿里云智能体白皮书开源进展
云原生
阿里云云原生1 天前
万联智策基于 AgentCore 构建数字员工团队,800+智能体规模化落地
云原生
分布式存储与RustFS1 天前
群晖 NAS 被 C3RB3R 锁了:本地备份一起被加密,RustFS 对象锁为什么删不掉
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
lsc_blog2 天前
听到优化风声,先准备能由自己掌控的事
数据库·分布式·中间件·职场和发展·重构