• zookeeper的安装使用


    转载从:https://blog.csdn.net/shenlan211314/article/details/6170717

    一、zookeeper 介绍

      ZooKeeper 是一个为分布式应用所设计的分布的、开源的协调服务。它的文件系统使用我们所熟悉的目录树结构。ZooKeeper 使用 Java 所编写,但是支持 Java 和 C 两种编程语言。我们设计 ZooKeeper 的目的是为了减轻分布式应用程序所承担的协调任务。

    二、zookeeper的安装

     首先linux系统需要安装jdk8

    下载

    https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

    下载:apache-zookeeper-3.4.14.tar,上传至服务器

    单机模式:通俗,就是只安装在同一台机子上面 

    解压:

    tar -xvf apache-zookeeper-3.4.14.tar.gz

    删除安装包:

    rm -rf apache-zookeeper-3.4.14.tar.gz

    重命名:

     

    配置环境变量:

    export ZOOKEEPER_HOME=/home/linewell/zookeeper
    export PATH=$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH
    source /etc/profile

    配置zookeeper

    修改配置文件的名字:

    创建data目录:

    配置zookeeper:

    vim zoo.cfg

    tickTime:心跳时间,为了确保连接存在的,以毫秒为单位,最小超时时间为两个心跳时间
    initLimit:多少个心跳时间内,允许其他server连接并初始化数据,如果ZooKeeper管理的数据较大,则应相应增大这个值
    clientPort:服务的监听端口
    dataDir:用于存放内存数据库快照的文件夹,同时用于集群的myid文件也存在这个文件夹里(注意:一个配置文件只能包含一个dataDir字样,即使它被注释掉了。)
    dataLogDir:用于单独设置transaction log的目录,transaction log分离可以避免和普通log还有快照的竞争
    syncLimit:多少个tickTime内,允许follower同步,如果follower落后太多,则会被丢弃。

    server.A=B:C:D:
    A是一个数字,表示这个是第几号服务器,B是这个服务器的ip地址
    C第一个端口用来集群成员的信息交换,表示的是这个服务器与集群中的Leader服务器交换信息的端口
    D是在leader挂掉时专门用来进行选举leader所用

    配置如下:

    # 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=/tmp/zookeeper
    # 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=0
    #
    minSessionTimeout=4000
    maxSessionTimeout=10000
    # 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

    dataDir=/usr/local/zookeeper/data

    server.1=ip:2888:3888

     

    启动:

    /home/linewell/zookeeper/bin/zkServer.sh start /home/linewell/zookeeper/conf/zoo.cfg

    查看状态:

    zkServer.sh status

     

    启动报错:

    切换成前台启动:

    ./bin/zkServer.sh start-foreground

    然后就可以查看打印的信息。

    搭建zookeeper集群,搭建zookeeper集群的方式和单机的方式大同小异。需要在datadir所指向的目录创建一个叫做myid的文件。上面写下当前服务器的编号。

    然后myid里面写下当前服务器的编号。然后配置到zoo.cfg当中。

     zoo.cfg的服务器配置:

    server.1=ip:2888:3888
    server.2=ip:2888:3888

    zookeeper 四字命令的使用

    先yum安装nc

    yum install -y nc

    然后测试:

     就可以得到配置信息了。

    zookeeper的命令行工具使用

    输入

    zkCli.sh –server 10.77.20.23:2181

    进入命令行工具

    输入 help 之后,屏幕会输出可用的 ZooKeeper 命令

    相关命令使用:

    查看特定的路径下,的节点信息,就是查兰特定路径下的节点

    创建一个节点,并添加数据:

    查看节点的字符串信息:

     重置节点的字符串信息:

    然后从新查看,可以发现,已经被重置了。

    delete操作删除对应的节点:

    删除后用ls命令查看,发现没有了对应的节点了。 

    zookeeper相关的api介绍:

    maven依赖:

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.13</version>
    </dependency>

    demo:

     

    图片copy从:https://blog.csdn.net/asd54090/article/details/80920489

  • 相关阅读:
    非旋Treap——fhq treap
    LCA
    树链剖分
    复习计划
    BZOJ2565: 最长双回文串(回文树)
    回文自动机
    luogu P3796 【模板】AC自动机(加强版)
    【BZOJ2908】 又是nand
    【HDU2460】 Network
    【CF786B】 Legacy
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/11075664.html
Copyright © 2020-2023  润新知