【zookeeper02】消息队列与微服务之zookeeper单机部署

ZooKeeper 单机部署

单机版的 ZooKeeper 安装

官方文档:

 https://zookeeper.apache.org/doc/r3.6.2/zookeeperStarted.html#sc_InstallingSingleMode

1.配置 Java 环境

官方依赖介绍

https://zookeeper.apache.org/doc/r3.8.0/zookeeperAdmin.html#sc_systemReq ZooKeeper: Because Coordinating Distributed Systems is a Zoo

 

范例: 安装 JDK-11

[root@ubuntu2004 ~]#apt update && apt -y install openjdk-11-jdk
[root@ubuntu2204 ~]#java -version
openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)
[root@ubuntu2004 ~]#apt update && apt -y install openjdk-11-jdk 
[root@ubuntu2004 ~]#java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

范例: 安装 JDK-8

[root@ubuntu1804 ~]#apt update && apt -y install openjdk-8-jdk 
[root@ubuntu1804 ~]#Java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~18.04-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)

2.部署 ZooKeeper

包安装

[root@ubuntu2204 ~]#apt list zookeeper
正在列表... 完成
zookeeper/jammy 3.4.13-6ubuntu4 all
[root@ubuntu2204 ~]#apt -y install zookeeper
[root@ubuntu2204 ~]#grep -Ev "#|^$" /etc/zookeeper/conf/zoo.cfg
 tickTime=2000  #服务器与服务器之间的单次心跳检测时间间隔,单位为毫秒
initLimit=10    
#集群中leader 服务器与follower服务器初始连接心跳次数,即多少个 2000 毫秒
syncLimit=5    
#leader 与follower之间检测发送和应答的心跳次数,如果该follower在时间段5*2000不能与leader进行通信,此
follower将不可用
dataDir=/var/lib/zookeeper  #自定义的zookeeper保存数据的目录
clientPort=2181   
#客户端连接 Zookeeper 服务器的端口,Zookeeper会监听这个端口,接受客户端的访问请求
[root@ubuntu2204 ~]#ls /usr/share/zookeeper/bin/
zkCleanup.sh  zkCli.sh  zkEnv.sh  zkServer.sh
[root@ubuntu2204 ~]#/usr/share/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /etc/zookeeper/conf/zoo.cfg
Starting zookeeper ... STARTED
[root@ubuntu2204 ~]#/usr/share/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /etc/zookeeper/conf/zoo.cfg
Mode: standalone

二进制安装

历史版本下载

https://archive.apache.org/dist/zookeeper/

范例: 安装单机 zookeeper

#官方下载
[root@ubuntu1804 ~]#wget -P /usr/local/src  https://downloads.apache.org/zookeeper/stable/apache-zookeeper
3.6.3-bin.tar.gz
 #国内镜像下载
[root@ubuntu1804 ~]#wget -P /usr/local/src  
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/apache-zookeeper-3.6.3-bin.tar.gz
[root@ubuntu1804 ~]#tar xf /usr/local/src/apache-zookeeper-3.6.3-bin.tar.gz  -C /usr/local/
[root@ubuntu1804 ~]#ln -s /usr/local/apache-zookeeper-3.6.3-bin /usr/local/zookeeper
[root@ubuntu1804 ~]#echo 'PATH=/usr/local/zookeeper/bin:$PATH' >  /etc/profile.d/zookeeper.sh
[root@ubuntu1804 ~]#. /etc/profile.d/zookeeper.sh
[root@ubuntu1804 ~]#ll /usr/local/zookeeper/
total 48
drwxr-xr-x  6 root root  4096 Feb 15 19:19 ./
drwxr-xr-x 11 root root  4096 Feb 15 19:20 ../
drwxr-xr-x  2 wang wang  4096 Sep  4 20:43 bin/
drwxr-xr-x  2 wang wang  4096 Sep  4 20:43 conf/
drwxr-xr-x  5 wang wang  4096 Sep  4 20:44 docs/
drwxr-xr-x  2 root root  4096 Feb 15 19:19 lib/-rw-r--r--  1 wang wang 11358 Sep  4 20:43 LICENSE.txt-rw-r--r--  1 wang wang   
432 Sep  4 20:43 NOTICE.txt-rw-r--r--  1 wang wang  1963 Sep  4 20:43 README.md-rw-r--r--  1 wang wang 3166 Sep  4 20:43 README_packaging.md
[root@ubuntu1804 ~]#ls /usr/local/zookeeper/bin/
README.txt    zkCli.cmd  zkEnv.cmd  zkServer.cmd            zkServer.sh            zkSnapShotToolkit.sh 
zkTxnLogToolkit.sh
zkCleanup.sh  zkCli.sh   zkEnv.sh   zkServer-initialize.sh  zkSnapShotToolkit.cmd  zkTxnLogToolkit.cmd
[root@ubuntu1804 ~]#ls /usr/local/zookeeper/conf/
configuration.xsl  log4j.properties   zoo_sample.cfg
[root@ubuntu1804 ~]#cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
#默认配置可不做修改
[root@ubuntu1804 ~]#grep -v "#" /usr/local/zookeeper/conf/zoo.cfg
tickTime=2000  #"滴答时间",用于配置Zookeeper中最小的时间单元长度,单位毫秒,是其它时间配置的基础
initLimit=10   #初始化时间,包含启动和数据同步,其值是tickTime的倍数
syncLimit=5    #正常工作,心跳监测的时间间隔,其值是tickTime的倍数
dataDir=/tmp/zookeeper #配置Zookeeper服务存储数据快照的目录,基于安全,可以修改为dataDir=/usr/local/zookeeper/data 
dataLogDir=/usr/local/zookeeper/logs  #指定日志路径,默认与 dataDir 一致,事务日志对性能影响非常大,强烈建议事务日志目录和数据目录分开,如果后续修改路径,需要先删除中dataDir中旧的事务日志,否则可能无法启动
clientPort=2181 #配置当前Zookeeper服务对外暴露的端口,用户客户端和服务端建立连接会话
preAllocSize:#为事务日志预先开辟磁盘空间。默认是64M,意味着每个事务日志初始大小64M。如果ZooKeeper产生快照频率较大,可以考虑减小这个参数,因为每次快照后都会切换到新的事务日志,即使前面的64M没有写满。
snapCount:#该配置项指定ZooKeeper在将内存数据库保存为快照之前,需要先写多少次事务日志,即,每写几次事务日志就快照一次。默认值为100000。为了防止所有的ZooKeeper服务器节点同时生成快照(一般情况下,所有集群的实例的配置文件是完全相同的),当某节点的先写事务数量在(snapCount/2+1,snapCount)范围内时挑选一个随机值做为该节点拍快照的时机。
autopurge.snapRetainCount=3 #3.4.0中的新增功能:启用后,ZooKeeper 自动清除功能,会将只保留此最新3个快照和相应的事务日志,并分别保留在dataDir 和dataLogDir中,删除其余部分,默认值为3,最小值为3
autopurge.purgeInterval=24  #3.4.0及之后版本,ZK提供了自动清理日志和快照文件的功能,这个参数指定了清理频率,单位是小时,需要配置一个1或更大的整数,默认是 0,表示不开启自动清理功能

 

3.启动 ZooKeeper

#查看选项
[root@ubuntu1804 ~]#zkServer.sh --help
#前台启动观察启动过程
[root@ubuntu1804 ~]#zkServer.sh start-foreground
#后台启动
[root@ubuntu1804 ~]#zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
#注意:如果配置service,zkServer.sh和systemctl不要混用,否则无法启动

 

4.验证 ZooKeeper

[root@ubuntu1804 ~]#zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
[root@ubuntu1804 ~]#ss -ntpl|grep 2181
LISTEN   0    50        *:2181       *:*     users:(("java",pid=1041,fd=70))
[root@ubuntu1804 ~]#pstree -p 1041
java(1041)─┬─{java}(1044)
           ├─{java}(1045)
           ├─{java}(1046)
           ├─{java}(1047)
           ├─{java}(1048)
           ├─{java}(1049)
           ├─{java}(1050)
           ├─{java}(1051)
           ├─{java}(1052)
           ├─{java}(1053)
           ├─{java}(1054)
           ├─{java}(1055)

5.一键安装 ZooKeeper 脚本

#!/bin/bash
#支持在线和离线安装

ZK_VERSION=3.8.1
#ZK_VERSION=3.8.0
#ZK_VERSION=3.6.3
#ZK_VERSION=3.7.1
#ZK_URL=https://archive.apache.org/dist/zookeeper/zookeeper-${ZK_VERSION}/apache-zookeeper-${ZK_VERSION}-bin.tar.gz
ZK_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-${ZK_VERSION}/apache-zookeeper-${ZK_VERSION}-bin.tar.gz
#ZK_URL="https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/apache-zookeeper-${ZK_VERSION}-bin.tar.gz"
#ZK_URL="https://downloads.apache.org/zookeeper/stable/apache-zookeeper-${ZK_VERSION}-bin.tar.gz"

INSTALL_DIR=/usr/local/zookeeper


HOST=`hostname -I|awk '{print $1}'`

.  /etc/os-release

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}


install_jdk() {
    if [ $ID = 'centos' -o  $ID = 'rocky' ];then
        yum -y install java-1.8.0-openjdk-devel || { color "安装JDK失败!" 1; exit 1; }
    else
        apt update
        apt install openjdk-11-jdk -y || { color "安装JDK失败!" 1; exit 1; } 
        #apt install openjdk-8-jdk -y || { color "安装JDK失败!" 1; exit 1; } 
    fi
    java -version
}


install_zookeeper() {
    if [ -f apache-zookeeper-${ZK_VERSION}-bin.tar.gz ] ;then
        cp apache-zookeeper-${ZK_VERSION}-bin.tar.gz /usr/local/src/
    else
        wget -P /usr/local/src/ --no-check-certificate $ZK_URL || { color  "下载失败!" 1 ;exit ; }
    fi
    tar xf /usr/local/src/${ZK_URL##*/} -C /usr/local
    ln -s /usr/local/apache-zookeeper-*-bin/ ${INSTALL_DIR}
    echo "PATH=${INSTALL_DIR}/bin:$PATH" >  /etc/profile.d/zookeeper.sh
    .  /etc/profile.d/zookeeper.sh
    mkdir -p ${INSTALL_DIR}/data 
    cat > ${INSTALL_DIR}/conf/zoo.cfg <<EOF
tickTime=2000
initLimit=10
syncLimit=5
dataDir=${INSTALL_DIR}/data
clientPort=2181
maxClientCnxns=128
autopurge.snapRetainCount=3
autopurge.purgeInterval=24
EOF
	cat > /lib/systemd/system/zookeeper.service <<EOF
[Unit]
Description=zookeeper.service
After=network.target

[Service]
Type=forking
#Environment=${INSTALL_DIR}
ExecStart=${INSTALL_DIR}/bin/zkServer.sh start
ExecStop=${INSTALL_DIR}/bin/zkServer.sh stop
ExecReload=${INSTALL_DIR}/bin/zkServer.sh restart

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now  zookeeper.service
    systemctl is-active zookeeper.service
	if [ $? -eq 0 ] ;then 
        color "zookeeper 安装成功!" 0  
    else 
        color "zookeeper 安装失败!" 1
        exit 1
    fi   
}


install_jdk

install_zookeeper

上一篇:[ubuntu]编译共享内存读取出现read.c:(.text+0x1a): undefined reference to `shm_open‘问题解决方案