• Zookeeper


    1. zookeeper简介

    1、分布式协调服务,存储在内存中,实现高吞吐量和低延迟。
    2、非常重视高性能、高可用,可以被复制,使用主从(leader-follower)模式避免单点故障
    3、主挂掉后200毫秒内即可选出新主,恢复可用状态,速度极快
    4、使用目录树结构,每层称为节点(持久节点和临时节点,两者皆可序列化),节点可以存1M以内的数据
    5、不要当作数据库使用
    6、客户端访问时会创建session(临时节点),根据session是否过期可以实现setnx()的过期检查
    7、顺序一致性,写操作均需操作leader
    8、原子性,更新只有成功或失败,没有部分成功
    9、单系统映像,无论连接到哪个服务器,客户端都将看到相同的服务视图
    10、可靠性,一旦应用了更新,就会写日志保证持久化
    11、及时性,保证客户端的访问能最终一致

    2. zookeeper集群搭建

    2.1 环境

    4台centos7 node01~node04

    2.2 安装jdk,并设置javahome

    2.3 下载zookeeper

    地址:zookeeper

    2.4 将zookeeper上传到相关目录

    本文放在/usr/local/zookeeper下

    cd /usr/local
    tar -zxvf  zookeeper-3.7.1.tar.gz
    mv  zookeeper-3.7.1 /usr/local/zookeeper
    
    • 1
    • 2
    • 3

    2.5 将zookeeper配置到环境变量

    vim /etc/profile 
    ##修改内容
    export ZOOKEEPER_HOME=/usr/local/zookeeper
    export PATH=$PATH:$ZOOKEEPER_HOME/bin
    #让环境变量生效
    source /etc/profile
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.6 修改zookeeper配置文件

    cd /usr/local/zookeeper/conf
    cp zoo_sample.cfg zoo.cfg
    # 修改zoo.cfg
    vim zoo.cfg
    
    • 1
    • 2
    • 3
    • 4

    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/gek/zk/
    # 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
    
    ## Metrics Providers
    #
    # https://prometheus.io Metrics Exporter
    #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
    #metricsProvider.httpPort=7000
    #metricsProvider.exportJvmInfo=true
    # 四个节点的名字
    server.1=node01:2888:3888
    server.2=node02:2888:3888
    server.3=node03:2888:3888
    server.4=node04:2888:3888
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    2.7 创建数据文件

    mkdir -p /var/gek/zk
    #新建myid文件,写入1用来表示zookeeper的唯一标识
    echo 1 > /var/gek/zk/myid
    
    • 1
    • 2
    • 3

    2.8 将node01节点创建好的zookeeper文件复制到其他节点

    scp /usr/local/zookeeper/ node02:`pwd`
    scp /usr/local/zookeeper/ node03:`pwd`
    scp /usr/local/zookeeper/ node03:`pwd`
    #注意
    需要在各个节点创建好 /var/gek/zk目录
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.9启动各个节点的zookeeper

    #表示前台启动可以查看是否有报错
    zkServer.sh start-foreground
    
    • 1
    • 2

    命令合集
    在这里插入图片描述

    2.10客户端连接

    zkCli.sh
    # help查看所有命令
    
    • 1
    • 2

    在这里插入图片描述

    相关入门命令

    #查看跟节点
    ls /
    # 创建节点
    create /ooxx ""
    ## 创建临时节点
    create -e /ooxx/xoxo
    ##分布式创建唯一节点-s代表序列创建,leader来维护序列,Created /a/xxx0000000002,创建自己的一个序列,
    #规避覆盖
    create -s /abc/xxx “”
    ## 获取节点
    get /ooxx
    ## 设置值,值最多1M
    set /a “123”
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    get a 获取结果

    cZxid = 0x200000002------>>创建目录的事务ID,其中0x2中0x代表16进制,2是代表第几个leader的纪元,后32位00000002代表创建目录的事务ID
    ctime = Wed Aug 10 09:36:20 PDT 2022------>>创建目录的时间
    mZxid = 0x200000003------>>该目录增删改的最后的事务ID
    mtime = Wed Aug 10 09:38:13 PDT 2022该目录增删改的最后时间
    pZxid = 0x200000004------>>该目录下,最后的创建目录的事务ID
    cversion = 1
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0------>>归属的临时节点ID
    dataLength = 11
    numChildren = 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查看连接情况

    netstat -natp |egrep '(2888|3888)'
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    修改docker默认数据目录
    NIO基础
    一篇文章带你掌握性能测试工具——Jmeter
    多线程-- 原子访问和atomic原子操作类实现原理
    java EE初阶 — 计算机工作原理
    基于深度学习的图像去雨去雾
    Java开发学习(三十九)----SpringBoot整合mybatis
    Android中的图像矩阵归一化
    猿创征文|【算法刷题日记之本手篇】洗牌与MP3光标位置
    HTML基础入门
  • 原文地址:https://blog.csdn.net/yaoxie1534/article/details/126545814