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。

好了,今天就到这了啦。

相关推荐
Bug退退退1234 小时前
RabbitMQ 高级特性之事务
java·分布式·spring·rabbitmq
CodeWithMe5 小时前
【Note】《Kafka: The Definitive Guide》第四章:Kafka 消费者全面解析:如何从 Kafka 高效读取消息
分布式·kafka
洁辉7 小时前
Spring Cloud 全栈指南:构建云原生微服务的终极武器
spring cloud·微服务·云原生
Gauss松鼠会8 小时前
GaussDB应用场景全景解析:从金融核心到物联网的分布式数据库实践
数据库·分布式·物联网·金融·database·gaussdb
阿里云云原生9 小时前
加工进化论:SPL 一键加速日志转指标
云原生
阿里云云原生10 小时前
破解异构日志清洗五大难题,全面提升运维数据可观测性
云原生
@Jackasher11 小时前
Redisson是如何实现分布式锁的?
分布式
阿里云云原生16 小时前
从 Python 演进探寻 AI 与云对编程语言的推动
云原生
亲爱的非洲野猪16 小时前
关于k8s Kubernetes的10个面试题
云原生·容器·kubernetes
西京刀客17 小时前
k8s之configmap
云原生·容器·kubernetes