分享一个朋友的人工智能教程。比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。

一:简介

分布式应用程序可以基于Zookeeper实现例如配置管理、数据发布/订阅、负载均衡、命名服务、协调通知、集群管理、Master选举、分布式锁、分布式队列等功能。

数据节点:

Zookeeper存储数据是以树形结构存储的,类似linux的目录,最上层是“/”也就是根节点,可以在根节点上创建子节点,在子节点上再创建子节点。在Zookeeper中每个节点被称为一个Znode,每个Znode包含:节点名称、节点值、值的长度、节点创建时间、节点修改时间、版本号、子节点数量等属性,可以创建节点,修改节点,删除节点等操作。

单机搭建zookeeper伪集群_zookeeper


单机搭建zookeeper伪集群_zookeeper_02

单机搭建zookeeper伪集群_zookeeper_03

Watcher(事件监听器)

事件监听是Zookeeper中非常重要的一个特性,用来监听节点的操作,当对节点做了操作(例如创建节点、修改节点、删除节点等)就会触发节点上的监听,当监听到节点发生了变化就可以做一些相应的处理操作。该
机制是ZooKeeper实现分布式协调服务的重要特性。

集群角色
  • Leader : 主节点,就是我们平时所说的master节点,用于提供读写服务
  • Follower: 从节点,就是我们平时所说的slaver节点,提供读服务、leader选举等
  • Observer: 从节点,一种特殊的Follower, 提供读服务, 不参与leader的选举
ZAB协议

二:单机搭建zookeeper集群

所谓单机搭建zookeeper集群其实就是在一台机器上启动多个zookeeper,在启动每个zookeeper时分别使用不同的配置文件zoo.cfg来启动,每个配置文件使用不同的配置参数(clientPort端口号、dataDir数据目录、dataLogDir数据日志目录)在同一台机器上启动多次。

1. 配置多个zoo.cfg配置文件

zoo.cfg默认在/usr/local/etc/zookeeper路径下(不同的操作系统路径不一样,这里用的是mac的路径),在这个路径下创建zoo1.cfg、zoo2.cfg、zoo3.cfg三个配置文件。

zoo1.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=/usr/local/var/run/zookeeper/zk1/data
dataLogDir=/usr/local/var/run/zookeeper/zk1/logs
# 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=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

zoo2.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=/usr/local/var/run/zookeeper/zk2/data
dataLogDir=/usr/local/var/run/zookeeper/zk2/logs
# the port at which the clients will connect
clientPort=2182
# 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=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

zoo3.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=/usr/local/var/run/zookeeper/zk3/data
dataLogDir=/usr/local/var/run/zookeeper/zk3/logs
# the port at which the clients will connect
clientPort=2183
# 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=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
  • tickTime: 基本事件单元,以毫秒为单位。这个时间是作为zookeeper服务器之间或客户端与服务器间维持心跳的时间。也就是每隔tickTime时间就会发送一个心跳
  • dataDir: 存储内存中数据库快照的位置,就是zookeeper保存数据的目录,默认情况下,zookeeper将数据的日志问也保存在这个目录里
  • clientPort: 客户端连接zookeeper服务器的端口,默认是2181,zookeeper会监听这个端口,接收客户端的访问请求
  • initLimit: 这个配置项是用来配置zookeeper接收客户端初始化连接能忍受多少个心跳时间间隔数。当已经超过10个心跳的时间(tickTime)长度后,zookeeper服务器还没有接收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000 = 20 秒
  • syncLimit: 这个配置项标识Leader和Follower之间发送消息,请求和应答的长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是 5 * 2000 = 10秒
  • server.myid=IP:Port1:Port2, myid是服务器的编号,一个正整数,一般是0、1、2、3等待,port1表示的是服务器与集群中的Leader服务器交换信息的端口,一般用2288,Port2表示的是万一集群中的Leader服务器宕机了,需要一个端口来重新进行宣讲,选出一个新的Leader,一般用3388

2. 创建data目录和logs目录

创建/usr/local/var/run/zookeeper/zk1/data/myid文件

创建/usr/local/var/run/zookeeper/zk2/data/myid文件

创建/usr/local/var/run/zookeeper/zk3/data/myid文件

在zk1、zk2、zk3下创建一个logs目录

三:分别启动3个zookeeper

# 启动3个zookeeper服务
zkServer start /usr/local/etc/zookeeper/zoo1.cfg
zkServer start /usr/local/etc/zookeeper/zoo2.cfg
zkServer start /usr/local/etc/zookeeper/zoo3.cfg

# 查看每个zookeeper对应的角色
zkServer status /usr/local/etc/zookeeper/zoo1.cfg
zkServer status /usr/local/etc/zookeeper/zoo2.cfg
zkServer status /usr/local/etc/zookeeper/zoo3.cfg

# 停止zookeeper服务
zkServer stop /usr/local/etc/zookeeper/zoo1.cfg

单机搭建zookeeper伪集群_服务器_04

单机搭建zookeeper伪集群_服务器_05

四:ZooKeeper命令行

# 连接服务器 zkCli -server IP:PORT
zkCli -server 127.0.0.1:2181

# 帮助命令
help

# 获取根节点列表,默认会有一个zookeeper节点,节点列表就像linux目录一样,根节点为/
ls /

# 创建znode并关联值, create /节点名称 “值”
create /myZnode "my znode"

# 获取znode
get /myZnode

# 修改znode
set /myZnode "my znode value string"

# 删除单个znode
delete /myZnode

# 递归删除(删除myZnode的子节点和myZnode节点)
rmr /myZnode

单机搭建zookeeper伪集群_服务器_06

单机搭建zookeeper伪集群_服务器_07

单机搭建zookeeper伪集群_服务器_08

IDEA Zookeeper插件

单机搭建zookeeper伪集群_客户端_09

单机搭建zookeeper伪集群_Zookeeper_10

单机搭建zookeeper伪集群_zookeeper_11

五:命令

ZooKeeper 支持某些特定的四字命令字母与其的交互。它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息。用户在可以通过 telnet 或 nc 向 ZooKeeper 提交相应的命令。

ZooKeeper 四字命令

功能描述

conf

输出相关服务配置的详细信息。

cons

列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息。包括“接受 / 发送”的包数量、会话 id 、操作延迟、最后的操作执行等等信息。

dump

列出未经处理的会话和临时节点。

envi

输出关于服务环境的详细信息(区别于 conf 命令)。

reqs

列出未经处理的请求

ruok

测试服务是否处于正确状态。如果确实如此,那么服务返回“imok ”,否则不做任何相应。

stat

输出关于性能和连接的客户端的列表。

wchs

列出服务器 watch 的详细信息。

wchc

通过 session 列出服务器 watch 的详细信息,它的输出是一个与watch 相关的会话的列表。

wchp

通过路径列出服务器 watch 的详细信息。它输出一个与 session相关的路径。




单机搭建zookeeper伪集群_客户端_12