软件环境
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