在CentOS7上安装和使用ZooKeeper最新版本(V3.4.12)

0.ZooKeeper文档

http://zookeeper.apache.org/doc/r3.4.11/zookeeperOver.html

1.准备

在CentOS7上安装zookeeper时,如下工具必须要有:wget

如果没有,则进行安装:

yum -y install wget
 
2.下载安装

下载到本地

  1.  
    cd /usr/local/src
  2.  
  3.  
    wget   http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz

解压

tar -xzvf zookeeper-3.4.12.tar.gz

创建的安装目录,并移动解压后的目录到其下

  1.  
    mkdir -p ../zookeeper
  2.  
    mv zookeeper-3.4.12/ ../zookeeper

创建数据目录,用于存放数据

mkdir -p /var/lib/zookeeper

创建配置

  1.  
    cd ../zookeeper/zookeeper-3.4.12/conf
  2.  
    cp zoo_sample.cfg zoo.cfg

修改其中配置dataDir:

vi zoo.cfg
dataDir=/var/lib/zookeeper

3.启动服务

  1.  
    cd ..
  2.  
    ./bin/zkServer.sh start

输出如下信息,表示启动成功

  1.  
    Connecting to 127.0.0.1:2181
  2.  
    2018-02-09 14:56:10,914 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
  3.  
    2018-02-09 14:56:10,922 [myid:] - INFO [main:Environment@100] - Client environment:host.name=jiaodacailei-thinkpad-centos7
  4.  
    2018-02-09 14:56:10,922 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_161
  5.  
    2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
  6.  
    2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk/jdk1.8.0_161/jre
  7.  
    2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper-3.4.11/bin/../build/classes:/usr/local/zookeeper/zookeeper-3.4.11/bin/../build/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../zookeeper-3.4.11.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../src/java/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../conf::/usr/local/jdk/jdk1.8.0_161/lib/dt.jar:/usr/local/jdk/jdk1.8.0_161/lib/tools.jar:/usr/local/jdk/jdk1.8.0_161/jre/lib/dt.jar
  8.  
    2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
  9.  
    2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
  10.  
    2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
  11.  
    2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
  12.  
    2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
  13.  
    2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
  14.  
    2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
  15.  
    2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
  16.  
    2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.11
  17.  
    2018-02-09 14:56:10,932 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@579bb367
  18.  
    Welcome to ZooKeeper!
  19.  
    2018-02-09 14:56:10,983 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1035] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
  20.  
    JLine support is enabled
  21.  
    2018-02-09 14:56:11,141 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@877] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
  22.  
    [zk: 127.0.0.1:2181(CONNECTING) 0] 2018-02-09 14:56:11,242 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x1000150f9ff0000, negotiated timeout = 30000
  23.  
     
  24.  
    WATCHER::
  25.  
     
  26.  
    WatchedEvent state:SyncConnected type:None path:null

4.启动客户端

./bin/zkCli.sh -server 127.0.0.1:2181

输入如下,等待用户输入命令:

[zk: 127.0.0.1:2181(CONNECTED) 0]

5.获取帮助

输入如下命令,可以获取帮助信息:

help

输出信息如下:

  1.  
    ZooKeeper -server host:port cmd args
  2.  
    stat path [watch]
  3.  
    set path data [version]
  4.  
    ls path [watch]
  5.  
    delquota [-n|-b] path
  6.  
    ls2 path [watch]
  7.  
    setAcl path acl
  8.  
    setquota -n|-b val path
  9.  
    history
  10.  
    redo cmdno
  11.  
    printwatches on|off
  12.  
    delete path [version]
  13.  
    sync path
  14.  
    listquota path
  15.  
    rmr path
  16.  
    get path [watch]
  17.  
    create [-s] [-e] path data acl
  18.  
    addauth scheme auth
  19.  
    quit
  20.  
    getAcl path
  21.  
    close
  22.  
    connect host:port

上面是所有可以使用的命令

6.ls命令

ls /

默认根节点下有一个zookeeper节点

[zookeeper]

7.创建节点

创建节点采用create命令,创建之后查看根路径,发现多了一个节点;

通过get命令,可以查询节点路径对应的数据

  1.  
    [zk: 127.0.0.1:2181(CONNECTED) 4] create /zk_test test_data
  2.  
    Created /zk_test
  1.  
    [zk: 127.0.0.1:2181(CONNECTED) 6] ls /
  2.  
    [zookeeper, zk_test]
  1.  
    [zk: 127.0.0.1:2181(CONNECTED) 7] get /zk_test
  2.  
    test_data
  3.  
    cZxid = 0x2
  4.  
    ctime = Fri Feb 09 15:19:11 CST 2018
  5.  
    mZxid = 0x2
  6.  
    mtime = Fri Feb 09 15:19:11 CST 2018
  7.  
    pZxid = 0x2
  8.  
    cversion = 0
  9.  
    dataVersion = 0
  10.  
    aclVersion = 0
  11.  
    ephemeralOwner = 0x0
  12.  
    dataLength = 9
  13.  
    numChildren = 0

8.修改节点数据

set命令可以修改路径对应的节点数据,修改后再通过get命令查看结果

[zk: 127.0.0.1:2181(CONNECTED) 8] set /zk_test test_data_change
cZxid = 0x2
ctime = Fri Feb 09 15:19:11 CST 2018
mZxid = 0x3
mtime = Fri Feb 09 15:21:31 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 10] get /zk_test
test_data_change
cZxid = 0x2
ctime = Fri Feb 09 15:19:11 CST 2018
mZxid = 0x3
mtime = Fri Feb 09 15:21:31 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0

9.删除节点

delete命令可以删除路径对应的节点,删除之后,通过ls命令查看其父路径,可以看到节点已经减少一个

  1.  
    [zk: 127.0.0.1:2181(CONNECTED) 11] delete /zk_test
  2.  
    [zk: 127.0.0.1:2181(CONNECTED) 12] ls /
  3.  
    [zookeeper]

10.退出客户端

quit

11.停止服务

./bin/zkServer.sh stop
posted @ 2018-08-21 19:53  疯子110  阅读(782)  评论(0编辑  收藏  举报