搭建 Zookeeper 集群环境

搭建 Zookeeper 集群环境

  1. 下载安装包

     # 下载
     wget https://apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz
    # 解压
    tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz
    # 移动到要安装的目录并 cd 进入相应路径
    # 。。。命令省略。。。
    
  2. 配置 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=/home/shaw/data/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=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
    
    ###### 配置分布式集群 ######
    server.1=192.168.100.117:2888:3888
    server.2=192.168.100.118:2888:3888
    server.3=192.168.100.119:2888:3888
    

    注:

    1. 上面的配置中,Zookeeper 的数据目录配置为 /home/shaw/data/zookeeper
    2. 配置中的 192.168.100.117192.168.100.118192.168.100.117 为三个 zookeeper 集群服务器的 IP 地址;
    3. 配置中的 28883888 分别为集群间进行通信、选举的端口;
    4. server.1server.2server.3 中的 123分别对应 /home/shaw/data/zookeeper/myid 中的内容,例如 server.1 服务器上的 /home/shaw/data/zookeeper/myid 内容便是 1server.2 服务器上的 /home/shaw/data/zookeeper/myid 内容便是 2
  3. 在 zookeeper 的数据目录(本例中为 /home/shaw/data/zookeeper)下配置 myid;

  4. 启动各个服务器上的 zookeeper 服务:

    ./bin/zkServer.sh start
    
  5. 查看各个服务器上的 zookeeper 启动状态:

    ./bin/zkServer.sh status
    
  6. 连接到 zookeeper 服务器:

    # 连接到 192.168.100.117 zookeeper 服务器,端口号 2181
    ./bin/zkCli.sh -server 192.168.100.117:2181
    
  7. 一些常用命令:

    # 查看根目录下的节点
    ls /
    
    # 创建 /shawearn 节点
    create /shawearn
    # 或创建的同时指定节点值
    create /shawearn hello
    
    # 查看节点值
    get /shawearn
    
    # 设置节点值
    set /shawearn HelloWorld!
    
    # 删除指定节点,节点下面必须不含子节点
    delete /shawearn
    
    # 删除指定节点及其下属所有节点
    deleteall /shawearn
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章