• Linux


    Linux下搭建单机Zookeeper集群

    前期准备

    安装jdk

    https://www.cnblogs.com/helios-fz/p/12623038.html

    下载zookeeper

    https://zookeeper.apache.org/releases.html

    将下载到的压缩包解压到你喜欢的位置,我放置的位置是

    /usr/local/apache-zookeeper-3.7.0-bin

    集群配置

    创建一个文件夹放置集群节点文件

    mkdir zookeeper-cluster

    在这个文件夹下再创建三个文件夹:zoo1、zoo2、zoo3

    在上述三个文件夹下都分别创建data和log两个文件夹

    在每一个data文件夹下,都创建一个myid文件,文件内容分别对应为1、2、3

    进入zookeeper的conf文件夹,复制三份zoo_sample.cfg,命名为zoo-1.cfg、zoo-2.cfg、zoo-3.cfg

    其中zoo-1.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=/usr/local/zookeeper-cluster/zoo1/data
    dataLogDir=/usr/local/zookeeper-cluster/zoo1/log
    # the port at which the clients will connect
    clientPort=2181
    server.1=127.0.0.1:2888:3888
    server.2=127.0.0.1:2889:3889
    server.3=127.0.0.1:2890:3890
    # 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

    其中需要区别配置的是这一部分:

    zoo-1.cfg

    dataDir=/usr/local/zookeeper-cluster/zoo1/data
    dataLogDir=/usr/local/zookeeper-cluster/zoo1/log
    clientPort=2181

    zoo-2.cfg

    dataDir=/usr/local/zookeeper-cluster/zoo2/data
    dataLogDir=/usr/local/zookeeper-cluster/zoo2/log
    clientPort=2182

    zoo-3.cfg

    dataDir=/usr/local/zookeeper-cluster/zoo3/data
    dataLogDir=/usr/local/zookeeper-cluster/zoo3/log
    clientPort=2183
    • dataDir:数据目录配置项,myid文件位于此目录下
    • dataLogDir:日志目录配置项,不设置的话默认使用dataDir配置的路径
    • clientPort:表示客户端连接zookeeper集群中的节点的端口号。在生产环境的集群中,不同节点处于不同的服务器上,端口号一般相同,因为这里是单机集群,所以配置了不同的端口号
    • server.id=host:port:port:id与所对应节点的数据目录下的myid文件中的id一致;两个port中,第一个port用于节点之间的通信,第二个port用于选举主节点
    • tickTime:单元时间。其他配置的时间间隔都是使用tickTime的倍数来表示的
    • initLimit:节点的初始化时间。用户Follower(从节点)的启动,并完成与Leader(主节点)进行数据同步的时间。Follower节点在启动过程中,会与Leader节点建立连接并完成对数据的同步,从而确定自己的起始状态。Leader节点允许Follower节点在initLimit时间内完成这项工作。该参数默认值为10,表示是参数tickTime值的10倍,必须配置且为正整数
    • syncLimit:心跳最大延迟周期。该参数用于配置Leader节点和Follower节点之间进行心跳检测的最大延时时间。在ZK集群运行的过程中,Leader节点会通过心跳检测来确定Follower节点是否存活。如果Leader节点在syncLimit时间内无法获取到Follower节点的心跳检测响应,那么Leader节点就会认为该Follower节点已经脱离了和自己的同步。

    启动集群

    启动服务

    切换到zookeeper安装目录的bin目录下,然后执行:

    ./zkServer.sh start ../conf/zoo-1.cfg
    ./zkServer.sh start ../conf/zoo-2.cfg
    ./zkServer.sh start ../conf/zoo-3.cfg

    查看服务状态

    ./zkServer.sh status ../conf/zoo-1.cfg 
    ./zkServer.sh status ../conf/zoo-2.cfg 
    ./zkServer.sh status ../conf/zoo-3.cfg 

    在这里可以看见,节点2被选举成了leader,1、3是follower

    客户端连接

    分别打开三个终端,执行以下命令进行客户端连接

    ./zkCli.sh  -server 127.0.0.1:2181
    ./zkCli.sh  -server 127.0.0.1:2182
    ./zkCli.sh  -server 127.0.0.1:2183

    在任意节点创建一个文件夹,在其他节点就可以看到同步创建

    create /test ""

    以上就是在节点1上创建test文件夹,然后在节点3上发现了同步文件夹

    宕机重新选举

    关停节点2

    ./zkServer.sh stop ../conf/zoo2.cfg

    等待超时时间过去后,重新查看节点状态

    可以看到节点3被选举成为新的leader

  • 相关阅读:
    Android拍照+方形剪裁——附代码与效果图
    Caffe源代码中Solver文件分析
    Java学习笔记五(多线程)
    setTimeout和setInterval的区别
    javascript中this的妙用
    javascript基于原型的语言的特点
    css样式小技巧
    html块元素和内联元素
    怎么解决浏览器兼容性问题
    高效率、简洁、CSS代码优化原则
  • 原文地址:https://www.cnblogs.com/helios-fz/p/14971418.html
Copyright © 2020-2023  润新知