【环境搭建】Apache ZooKeeper 3.8.4 Stable

软件环境

Ubuntu 20.04 、OpenJDK 11

OpenJDK 11(如果已经安装,可以跳过这一步)

安装OpenJDK 11:

复制代码
 $ sudo apt-get update
 $ sudo apt-get install -y openjdk-11-jdk

设置 JAVA_HOME 环境变量:

复制代码
 $ sudo gedit ~/.bashrc

添加内容:

复制代码
 export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
 export PATH=$PATH:$JAVA_HOME/bin

环境变量生效:

复制代码
 $ source ~/.bashrc

验证:

复制代码
 $ java --version

获取 Zookeeper

进入 Zookeeper 官网Apache ZooKeeper,找到Downloads,直接下载编译好的安装包,不然下载源码还要自己手动编译比较麻烦,我们选择Apache ZooKeeper 3.8.4 Stable:

复制这个HTTP网址:

使用wget下载到本地:

复制代码
 $ wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.4/apache-zookeeper-3.8.4-bin.tar.gz --no-check-certificate

解压:

复制代码
 $ tar -zxvf apache-zookeeper-3.8.4-bin.tar.gz

将解压后的文件移动到合适的目录,例如/opt/zookeeper

复制代码
 $ sudo mv apache-zookeeper-3.8.4-bin /opt/zookeeper

配置 Zookeeper

/opt/zookeeper/conf目录下,复制一份默认配置文件示例:

复制代码
 $ cd /opt/zookeeper/conf
 $ sudo cp zoo_sample.cfg zoo.cfg

打开zoo.cfg文件,找到并修改dataDir参数,指定 Zookeeper 存储数据的目录:

复制代码
 $ sudo gedit zoo.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=/var/lib/zookeeper/data
 # 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.
 #
 # https://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
 ​
 ## Metrics Providers
 #
 # https://prometheus.io Metrics Exporter
 #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
 #metricsProvider.httpHost=0.0.0.0
 #metricsProvider.httpPort=7000
 #metricsProvider.exportJvmInfo=true

此外,可以根据需要修改clientPort参数来指定 Zookeeper 服务端口,默认是2181。如果要配置 Zookeeper 集群,还需要在zoo.cfg文件中添加其他节点信息。

创建相应的目录:

复制代码
 $ sudo mkdir -p /var/lib/zookeeper/data

给予目录所有者读写执行权限,给所属组和其他用户读执行权限:

复制代码
 $ sudo chmod -R 755 /var/lib/zookeeper/data

启动 Zookeeper

/opt/zookeeper/bin目录添加到系统的PATH环境变量中:

复制代码
 $ sudo gedit ~/.bashrc

添加内容如下:

复制代码
 export PATH=$PATH:/opt/zookeeper/bin

环境变量生效:

复制代码
 $ source ~/.bashrc

先切换为root shell,否则权限不够:

复制代码
 $ sudo su

启动 Zookeeper:

复制代码
 $ zkServer.sh start

出现问题:

解决:

权限不够,切换root shell即可。

出现问题:

复制代码
 # zkServer.sh start
 zkServer.sh: command not found

解决:

在root shell里按照刚才的步骤再添加一次环境变量并生效即可。

检查 Zookeeper 服务的状态,确保其正常启动:

复制代码
 # zkServer.sh status
相关推荐
IT大白21 小时前
2、Kafka原理-Producer
分布式·kafka
巧克力味的桃子1 天前
Spark 课程核心知识点复习汇总
大数据·分布式·spark
SeaTunnel1 天前
Apache SeaTunnel 2025 案例精选重磅发布!
大数据·开源·apache·seatunnel·案例
Java 码农1 天前
RabbitMQ集群部署方案及配置指南05
分布式·rabbitmq
`林中水滴`1 天前
ZooKeeper 系列:ZooKeeper 集群环境搭建
zookeeper
小马爱打代码1 天前
ZooKeeper:五种经典应用场景
分布式·zookeeper·云原生
上海锟联科技1 天前
DAS一体化光模块
分布式·分布式光纤传感·ofdr·光频域反射·das
麦兜*1 天前
Spring Boot 整合 Apache Doris:实现海量数据实时OLAP分析实战
大数据·spring boot·后端·spring·apache
Java 码农1 天前
RabbitMQ集群部署方案及配置指南04
分布式·rabbitmq
独自破碎E1 天前
在RabbitMQ中,怎么确保消息不会丢失?
分布式·rabbitmq