ZooKeeper

ZooKeeper是一个开源的分布式协调服务,他为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如统一命名空间服务,配置服务和分布式锁等分布式基础服务。

就是一个服务注册中心。一个典型的ZooKeeper集群如下:

ZooKeeper拥有一个层次的命名空间,这个和标准的文件系统非常相似,如下图所示:

安装

  1. 下载并解压;
  2. 创建data和log目录;
  3. 修改config - zoo.cfg;

    1
    2
    dataDir=/opt/zookeeper-3.4.13/data
    dataLogDir=/opt/zookeeper-3.4.13/log
  4. 启动

    1
    2
    3
    4
    5
    6
    7
    8
    [root@Automation bin]# ./zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /opt/zookeeper-3.4.13/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@Automation bin]# ./zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /opt/zookeeper-3.4.13/bin/../conf/zoo.cfg
    Mode: standalone

命令操作

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
[root@Automation bin]# ./zkCli.sh -server localhost:2181
Connecting to localhost:2181
2018-06-02 09:26:12,387 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2018-06-02 09:26:12,395 [myid:] - INFO [main:Environment@100] - Client environment:host.name=<NA>
2018-06-02 09:26:12,395 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_11
2018-06-02 09:26:12,405 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-06-02 09:26:12,405 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.8.0_11/jre
2018-06-02 09:26:12,405 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper-3.4.13/bin/../build/classes:/opt/zookeeper-3.4.13/bin/../build/lib/*.jar:/opt/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/opt/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/opt/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/opt/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/opt/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.13/bin/../conf:.:/usr/java/jdk1.8.0_11/lib/dt.jar:/usr/java/jdk1.8.0_11/lib/tools.jar:/usr/java/jdk1.8.0_11/jre/lib
2018-06-02 09:26:12,405 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/i386:/lib:/usr/lib
2018-06-02 09:26:12,405 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-06-02 09:26:12,405 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-06-02 09:26:12,405 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-06-02 09:26:12,406 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=i386
2018-06-02 09:26:12,406 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-573.el6.x86_64
2018-06-02 09:26:12,406 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2018-06-02 09:26:12,406 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2018-06-02 09:26:12,407 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/zookeeper-3.4.13/bin
2018-06-02 09:26:12,410 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1e0aca6
Welcome to ZooKeeper!
2018-06-02 09:26:12,484 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-06-02 09:26:12,510 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2018-06-02 09:26:12,572 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1003ae6d0d60000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

这里不做过多深入学习说明,集群环境搭建,工作原理,Leader选举等,请参见官方文档。

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!