ZooKeeper基础

ZooKeeper

索引:

  • ZooKeeper基础
  • 安装
  • zkCli使用
  • ZooKeeper架构
  • 服务监控
  • Observer实现跨区域部署

一 、ZooKeeper基础

a. 典型应用场景:
  • 配置管理(configuration management)
  • DNS 服务
  • 组成员管理(group membership)
  • 各种分布式锁

ZooKeeper 适用于存储和协同相关的关键数据,不适合用于大数据量存储。

b. ZooKeeper数据模型:

ZooKeeper的数据模型是层次模型,常见于文件系统。ZooKeeper使用文件系统模型主要基于以下两点考虑

  • 文件系统的树形结构便于表达数据之间的层次关系
  • 文件系统的树形结构便于为不同的应用分配独立的命名空间

ZooKeeper的层次模型称为data tree。Data tree 的每个节点叫做znode。每个节点都可以保存数据。每个节点都有一个版本,从0开始计数。

c. data tree 接口

ZooKeeper对外提供了一个用来访问data tree 的简化文件系统API:

  • 使用UNIX风格的路径名来定位znode,例如/A/X表示zonde A的子节点X。
  • zonde 的数据只支持全量写入和读取,没有像通用文件系统那样支持部分写入读取。
  • data tree 的所有API都是wait-free 的, 及正在执行中的API调用不会影响其他API的调用。
  • data tree 的API都是对文件系统的wait-free 操作, 不直接提供锁这样的分布式协同机制。但是data tree 的API非常强大, 可以用来实现多种分布式协同机制。

二、 安装

官方网站:https://zookeeper.apache.org/ 下载apache-zookeeper-3.6.0-bin.tar.gz

将 bin目录放到环境变量中 , 配置文件为 conf/zoo.cfg

  • 启动命令 zkServer.sh start
  • 检查日志文件是否有出错信息: 日志文件在logs目录下 grep -E -i “((error)|(exception))” *
  • 检查数据文件: /data/zookeeper
  • 检查是否在2181端口监听 netstat -tunpl |grep 2181

三、zkCli使用

zkCli 是zookeeper链接工具。 使用命令zkCli.sh

创建节点 create [-s] [-e] [-c] [-t ttl] path [data] [acl]: “create /app1”

查看节点 ls [-s] [-w] [-R] path:“ls -R /”

监听节点 stat [-w] path:“stat /lock”

删除节点delete [-v version] path:“delete /lock”

基于监听可以实现简单分布式锁

协同服务说明:

设计一个master-worker的组成员管理系统,要求系统中只能有一个master,master能实时获取系统中worker的情况。

[zk: localhost:2181(CONNECTED) 12] create /workers
Created /workers
[zk: localhost:2181(CONNECTED) 13] create -e /workers/w1 "w1:2224"
Created /workers/w1
[zk: localhost:2181(CONNECTED) 14] create -e /workers/w2 "w2:2225"
Created /workers/w2
[zk: localhost:2181(CONNECTED) 15] ls -w /workers
[w1, w2]
[zk: localhost:2181(CONNECTED) 16] delete /workers/w1
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/workers
[zk: localhost:2181(CONNECTED) 17] ls -w /workers
[w2]

四、ZooKeeper架构

应使用ZooKeeper客户端使用ZooKeeper服务,ZooKeeper客户端负责和ZooKeeper集群的交互。ZooKeeper集群有两种运行模式:standalone模式(单机)和quorum模式(集群)。

a. session:

ZooKeeper客户端和ZooKeeper集群中的某个节点创建了一个session。客户端可以主动关闭session。另外如果ZooKeeper节点没有在session关联的timeout时间内收到客户端消息的话,ZooKeeper节点也会关闭session。另外ZooKeeper客户端如果发现连接的ZooKeeper出错,会自动的和其他ZooKeeper节点建立连接。

b. Quorum模式:

处于quorum模式的ZooKeeper集群包含多个ZooKeeper节点。分为一个leader节点和多个follower节点。leader节点可以处理读写请求,follower只可以处理读请求。follower在接到写请求时会把请求转发给leader来处理

c. 数据一致性:
  • 全局可线性化写入:先到达leader的写请求会被先处理,leader决定写请求的执行顺序。
  • 客户端FIFO顺序:来自给定客户端的请求按照发送顺序执行。
d. 3节点quorum模式ZooKeeper集群演示:

在conf文件夹下创建 quorum文件夹, 并创建三个配置文件:

zoo-quorm-node1.cfg zoo-quorm-node2.cfg zoo-quorm-node3.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.
# 这里3个文件不同, 分别为node1, node2, node3
dataDir=/data/zookeeper/node1
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
# 这里127.0.0.1表示ip,2888表示quorum通讯端口 3888 用于选举接口
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2887:3887

解决错误:ZooKeeper myid file is missing

在 /data/zookeeper 目录下, 创建三个文件夹, 分别为node1, node2, node3 并 分别在文件夹下创建myid文件, 并分别写入1,2,3

原因:集群中的每台ZK server都会有一个用于惟一标识自己的id,有两个地方会使用到这个id:myid文件和zoo.cfg文件中。myid文件存储在dataDir目录中,指定了当前server的server id。在zoo.cfg文件中,根据server id,配置了每个server的ip和相应端口。Zookeeper启动的时候,读取myid文件中的server id,然后去zoo.cfg 中查找对应的配置。

然后 开启3个窗口, 启动命令:

zkServer.sh start-foreground /home/..../conf/quorum/zoo-quorm-node1.cfg zkServer.sh start-foreground /home/..../conf/quorum/zoo-quorm-node2.cfg zkServer.sh start-foreground /home/..../conf/quorum/zoo-quorm-node3.cfg

然后开启客户端窗口:

zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

发现其他三个服务端的窗口:

2020-04-22 17:27:08,370 [myid:1] - INFO  [CommitProcessor:1:LearnerSessionTracker@116] - Committing global session 0x20001a64a8e0000
五、服务监控

可以使用java自带的jconsule进行服务监控

luslin@local:~/software/zookeeper/conf$ jconsole

在这里插入图片描述

选择zookeeper 进程,可以查看zookeeper节点信息,链接信息,创建的节点等信息

在这里插入图片描述

六、Zookeeper-Observer实现跨区域部署

Observer和Zookeeper机器其他节点唯一的交互是接收来自leader的inform消息,更新自己的本地存储,不参与提交和选举的投票过程

应用场景:
a. 提升读写性能:因为不参与提交和选举投票的过程,所以可以通过往集群中添加Observer节点来提高整个集群的读写性能

在这里插入图片描述

b.跨数据中心部署

在两地部署ZooKeeper节点来保证读写性能,如果不使用observer,那么每个请求都涉及propose、ack、commit三个跨区域消息。当使用后,只剩下一个inform跨区域消息

在这里插入图片描述

再增加一个4节点来演示observer模式

# 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.
# 这里3个文件不同, 分别为node1, node2, node3, node4
dataDir=/data/zookeeper/node1
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
# 这里127.0.0.1表示ip,2888表示quorum通讯端口 3888 用于选举接口
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2887:3887
server.3=127.0.0.1:2886:3886:observer

创建好myid文件后可以启动4个zookeepr 服务,可以在监控中看到4个服务,并且服务4 为observer模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值