JDK 1.8 + 【不要安装OpenJdk】
zookeeper 3.4.14
zkUI 2.0.+
maven 3.5.+
http://apache.spinellicreations.com/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
# 解压文件
tar -xvf zookeeper-3.4.14.tar.gz
# 改名
mv zookeeper-3.4.14 zookeeper
# 删除无用的tar包
rm -f zookeeper-3.4.14.tar.gz
# 进入到 zookeeper/conf 下面
cd zookeeper/conf
# 将 zoo_sample.cfg 复制一份,命名为 zoo.cfg,此即为Zookeeper的配置文件
cp zoo_sample.cfg zoo.cfg
# 编辑配置文件
vim 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=/data/zookeeper/data
#日志存放的位置
dataLogDir=/data/log/zookeeper/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
#集群的配置,后面为你的ip,后面有节点依次往下面排
server.0=172.31.3.86:2888:3888
server.1=172.31.3.87:2888:3888
Server.2=172.31.3.26:2888:3888
# 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=/data/zookeeper/data
#日志存放的位置
dataLogDir=/data/log/zookeeper/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
#集群的配置,后面为你的ip,后面有节点依次往下面排
server.0=172.31.3.86:2888:3888
server.1=172.31.3.87:2888:3888
server.2=172.31.3.26:2888:3888
最后使用wq!保存修改,说明:
dataDir 和 dataLogDir 需要在启动前创建完成
clientPort 为 zookeeper的服务端口
server.0、server.1、server.2 为 zk 集群中三个 node 的信息,定义格式为 hostname:port1:port2,其中 port1 是 node 间通信使用的端口,port2 是node 选举使用的端口,需确保三台主机的这两个端口都是互通的。
Zookeeper 默认会将控制台信息输出到启动路径下的 zookeeper.out 中,通过如下方法,可以让 Zookeeper 输出按尺寸切分的日志文件:
修改 conf/log4j.properties 文件,将
zookeeper.root.logger=INFO, CONSOLE
改为
zookeeper.root.logger=INFO, ROLLINGFILE
修改bin/zkEnv.sh文件,将
ZOO_LOG4J_PROP="INFO,CONSOLE"
改为
ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kE1IeBk6-1591337986359)(https://s1.ax1x.com/2020/06/04/t08ufe.png)]
切换到 zookeeper 目录下
# zookeeper 存放数据目录
mkdir -p /data/zookeeper/data
cd /data/zookeeper/data
touch myid
# 这里的 0 和 zoo.cfg配置文件server.x 对应上面配置集群节点的数字,比如第一台就写入数字0即可:
# 此处为第一台服务器的示例,其余的依次改为1、2
echo "0" > myid
分别在三台主机的 dataDir 路径下创建一个文件名为 myid 的文件,文件内容为该 zk 节点的编号
分别启动配置的 zookeeper 服务
cd /data/zookeeper/bin
./zkServer.sh start
返回信息:
节点启动完成后,可依次执行如下命令查看集群状态:
./zkServer.sh status
# ZKUI2.0的GitHub地址:
https://github.com/DeemOpen/zkui
# 进入下载的ZKUI2.0的项目文件夹,执行:
mvn install -Dmaven.test.skip=true
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OdRv76xC-1591337986369)(https://s1.ax1x.com/2020/06/04/t08znI.png)]
打包成功后在target目录下可以看到如下zkui-2.0-SNAPSHOT-jar-with-dependencies.jar,这个正是我们需要的jar包
配置文件位置在项目文件的根目录下,如下图所示:
#创建存放目录 放入上面的jar包和cfg文件 一起放在/data/zkui目录下
mkdir /data/zkui
修改conf.cfg配置文件,将zookeeper地址,多个zk实例被逗号分开。例如:
172.31.3.86:2181,172.31.3.87:2181,172.31.3.26:2181
放入启动脚本zkui_start.sh:
#!/bin/bash
. /etc/profile
ROOT_PATH=$(dirname $(readlink -f $0))
#获取可执行jar包名称
function getJar() {
for item in $(ls $ROOT_PATH); do
fileName=$item
if [ ! -d $fileName ]; then
if [ ${fileName##*.} = jar ]; then
APP_NAME=$fileName
return
fi
fi
done
echo "当前目录下未找到可执行jar包"
exit 1
}
getJar
JAVA_OPTS="-server \
-Xms1024m \
-Xmx1024m \
-Xmn512m \
-Xss512m \
-XX:MaxMetaspaceSize=512m \
-XX:MaxNewSize=512m \
-XX:MetaspaceSize=512m \
-XX:SurvivorRatio=9 \
-XX:+UseConcMarkSweepGC \
-XX:ParallelCMSThreads=4 \
-XX:MaxTenuringThreshold=4 \
-XX:+UseCMSCompactAtFullCollection \
-XX:CMSFullGCsBeforeCompaction=4 \
-XX:+CMSParallelRemarkEnabled \
-XX:+CMSScavengeBeforeRemark \
-XX:+CMSClassUnloadingEnabled \
-XX:+UseCMSInitiatingOccupancyOnly \
-XX:CMSInitiatingOccupancyFraction=80 \
-XX:CMSInitiatingPermOccupancyFraction=92 \
-XX:SoftRefLRUPolicyMSPerMB=0 \
-XX:+UseFastAccessorMethods \
-XX:+AggressiveOpts \
-XX:-UseBiasedLocking \
-XX:+DisableExplicitGC"
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist() {
pid=$(ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}')
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java ${JAVA_OPTS} -jar ${ROOT_PATH}/${APP_NAME} >/dev/null 2>&1 &
#nohup java $JAVA_OPTS -jar $APP_NAME >/www/log.txt 2>&1 &
fi
}
#停止方法
stop() {
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart() {
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
使用脚本启动:
sh zkui_start.sh start
停止命令参考:
sh zkui_start.sh stop
检测是否启动成功:
netstat -antp |grep 9090
启动完成后访问:
http://172.31.3.86:9090/
输入我们配置中的账密,默认是:
admin | manager
查看节点信息界面:
查看集群状态:
vim zookeeper.service
zookeeper.service 添加内容:
[Unit]
Description=Zookeeper service
After=network.target
[Service]
Type=simple
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/java/jdk-11.0.1/bin"
User=root
Group=root
ExecStart=/opt/kafka/kafka_2.11-2.1.0/bin/zookeeper-server-start.sh /opt/kafka/kafka_2.11-2.1.0/config/zookeeper.properties
ExecStop=/opt/kafka/kafka_2.11-2.1.0/bin/zookeeper-server-stop.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
注:以上文件 根据自己的 jdk 和 kafka 安装目录相应的修改。
systemctl daemon-reload
systemctl enable zookeeper
systemctl start/stop/restart zookeeper
systemctl status zookeeper