【环境搭建】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
相关推荐
yuanzhengme10 小时前
Shell【脚本 02】离线安装配置Zookeeper及Kafka并添加service服务和开机启动(脚本分析)
linux·zookeeper·kafka·自动化·安装脚本
你我约定有三10 小时前
分布式微服务--Nacos作为配置中心(二)
java·分布式·spring cloud·微服务·架构·wpf·负载均衡
黄雪超11 小时前
Kafka——关于Kafka动态配置
大数据·分布式·kafka
bing_15812 小时前
如何利用 Redis 的原子操作(INCR, DECR)实现分布式计数器?
数据库·redis·分布式
爱思德学术17 小时前
中国计算机学会(CCF)推荐学术会议-A(软件工程/系统软件/程序设计语言):FSE 2026
分布式·软件工程·软件构建
Apache Flink1 天前
Apache Flink 2.1.0: 面向实时 Data + AI 全面升级,开启智能流处理新纪元
人工智能·flink·apache
光军oi1 天前
Javaweb————Apache Tomcat服务器介绍及Windows,Linux,MAC三种系统搭建Apache Tomcat
服务器·tomcat·apache
Code季风1 天前
API 网关与服务发现:实现动态路由与智能请求转发的原理揭秘
分布式·微服务
lang201509281 天前
Apache RocketMQ 中 Consumer(消费者)的详细说明
apache·rocketmq