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。

好了,今天就到这了啦。

相关推荐
Chan165 小时前
【 SpringCloud | 微服务 MQ基础 】
java·spring·spring cloud·微服务·云原生·rabbitmq
小鸡脚来咯6 小时前
RabbitMQ入门
分布式·rabbitmq
2201_761199046 小时前
k8s4部署
云原生·容器·kubernetes
慌ZHANG7 小时前
云原生技术驱动 IT 架构现代化转型:企业实践与落地策略全解
云原生
qq_463944868 小时前
【Spark征服之路-2.2-安装部署Spark(二)】
大数据·分布式·spark
西京刀客8 小时前
k8s热更新-subPath 不支持热更新
云原生·容器·kubernetes·configmap·subpath
敖云岚8 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
正在努力Coding9 小时前
kafka(windows)
分布式·kafka
佛祖让我来巡山11 小时前
【Zookeeper从入门到实战】SpringBoot整合完整指南
zookeeper
爱瑞瑞12 小时前
云原生学习笔记(五) 构建 Docker 镜像与运行容器
云原生