zookeeper

[ZooKeeper之三] 使用 ZooKeeper 入门

2020-05-17  本文已影响0人  小胡_鸭

一、ZooKeeper 发行包

  ZooKeeper 的发行包中的bin目录中提供了 zkServerzkCli 工具,可以使用 zkServer 启动服务端,默认提供与客户端连接的端口是 2181,不过可以通过conf目录下的 zoo.cfg 修改连接端口配置。使用 zkCli 可以启动客户端,默认连接到 127.0.0.1:2181。这些工具主要用于调试和监控目的,如果想实现一个真实的 ZooKeeper 应用,应该使用 ZooKeeper 的语言套件。

二、配置文件详解

  ZooKeeper 的默认配置文件是 conf/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=/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=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
配置项 默认值 含义
tickTime 2000 心跳检测的间隔,集群中的服务器需要互发心跳探活,客户端的最小会话超时时间为 2 * tickTime
initLimit 10 初始化同步阶段最多可以消耗多少个心跳时间
syncLimit 5 从发起提议(发生事务或leader选举时)到收到确认最大耗时多少个心跳时间
dataDir /tmp/zookeeper 用于配置内存数据库保存的模糊快照的目录,如果某个服务器为集群中的一台,id文件也保存在该目录下
dataLogDir - 存储事务日志数据的目录,由于比较敏感,磁盘过于忙碌会影响写入的性能,所以最佳实践是使用专用的日志存储设备,将dataLogDir的目录配置指向该设备
clientPort 2181 提供给客户端连接的端口
maxClientCnxns 60 允许每个IP地址的并发 socket 连接的最大数量,避免同个IP的客户端连接数过多
autopurge.snapRetainCount 3 当发生数据清理时,快照能保存的最大数量
autopurge.purgeInterval 1 对快照和日志进行垃圾回收操作的时间间隔的小时数


三、启动服务端

  使用下面命令启动服务端:

cd bin
./zkServer.sh start

  上面的命令会使服务在后台运行,如果想方便地查看启动日志和运行输出,可使用下面命令:

bin/zkServer.sh start-foreground
启动服务端


四、启动客户端

bin/zkCli.sh
启动客户端
  从日志中,可以看到客户端启动的处理步骤是:
(1)启动程序建立会话。
(2)初始化客户端连接,尝试连接到 localhost:2181。
(3)连接成功后,服务器开始初始化这个新会话。
(4)会话初始化成功,创建的会话id为 0x100005def600000
(5)服务端向客户端发送一个 SyncConnected 事件。


四、使用客户端操作 ZooKeeper

1、查询根节点下有哪些子节点



2、查询根节点数据



3、创建节点

4、修改节点数据



5、删除节点
上一篇 下一篇

猜你喜欢

热点阅读