Zookeeper-5分钟快速掌握分布式应用程序协调服

974 阅读11分钟
原文链接: www.cnblogs.com

一、Zookeeper 安装

1.zookeeper支持brew安装。

➜  ~ brew info zookeeper
zookeeper: stable 3.4.10 (bottled), HEAD
Centralized server for distributed coordination of services
https://zookeeper.apache.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/zookeeper.rb
==> Requirements
Optional: python ✔
==> Options
--with-perl
    Build Perl bindings
--with-python
    Build with python support
--HEAD
    Install HEAD version
==> Caveats
To have launchd start zookeeper now and restart at login:
  brew services start zookeeper
Or, if you don't want/need a background service you can just run:
  zkServer start

 

通过brew install zookeeper 安装:

➜  ~ brew install zookeeper
Please wait for it to finish or terminate it to continue.
==> Downloading https://homebrew.bintray.com/bottles/zookeeper-3.4.10.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring zookeeper-3.4.10.sierra.bottle.tar.gz
==> Caveats
To have launchd start zookeeper now and restart at login:
  brew services start zookeeper
Or, if you don't want/need a background service you can just run:
  zkServer start
==> Summary
🍺  /usr/local/Cellar/zookeeper/3.4.10: 241 files, 31.4MB

 

在/usr/local/etc/zookeeper/目录下,已经有了缺省的配置文件

➜  zookeeper ll
total 32
-rw-r--r--  1 huangweijie  admin    67B  9  4 22:13 defaults
-rw-r--r--  1 huangweijie  admin   339B  9  4 22:13 log4j.properties
-rw-r--r--  1 huangweijie  admin   941B  9  4 22:13 zoo.cfg
-rw-r--r--  1 huangweijie  admin   941B  9  4 22:13 zoo_sample.cfg

 

zookeeper 默认配置文件内容:

# 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/data
# 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
dataDir=/usr/local/zk/data
dataLogDir=/usr/local/zk/dataLog        
clientPort=2181

 

启动服务

zkServer

1.zkServer --查看服务目录

➜  zookeeper zkServer
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Usage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}

 

2.zkServer start --启动服务

➜  zookeeper zkServer start
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED

 

3.zkServer status--查看服务状态

➜  zookeeper zkServer status
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Mode: standalone

 

4.zkServer stop --关闭服务

➜  zookeeper zkServer stop
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Stopping zookeeper ... STOPPED

 

zkClient

➜  ~ zkCli
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

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

 

 

其他操作系统的安装教程可参考:

www.cnblogs.com/wuxl360/p/5…

blog.csdn.net/morning99/a…

二、Zookeeper 集群

ZooKeeper分布式模式安装(ZooKeeper集群)也比较容易,这里说明一下基本要点。 首先要明确的是,ZooKeeper集群是一个独立的分布式协调服务集群,“独立”的含义就是说,如果想使用ZooKeeper实现分布式应用的协调与管理,简化协调与管理,任何分布式应用都可以使用,这就要归功于Zookeeper的数据模型(DataModel)和层次命名空间(Hierarchical Namespace)结构。

1、主机名称映射配置

本机安装将采用单机安装集群,因此在配置主机时我们将采用同一Ip,不同端口的操作,具体配置如下:

server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

 

在本次安装中,我们将使用三台zookeeper 服务器,分别使用的端口上述端口。

如果在开发中需要配置多台服务器,则可以将对应的ip修改为目标服务器地址:

server.1=10.192.1.120:2288:3388
server.2=10.192.1.121:2288:3388
server.3=10.192.1.122:2288:3388

 

除了使用Ip 直接访问外,也可以在本地设置host 域名解析,设置成相应的映射:
host 文件添加
10.192.1.120   slave-01  
10.192.1.121   slave-02  
10.192.1.122   slave-03 

 



zookeeper 配置文件:
server.1=slave-01:2888:3888  
server.2=slave-02:2888:3888  
server.3=slave-03:2888:3888

 

 

server.A=B:C:D

A:其中 A 是一个数字,表示这个是服务器的编号;

B:是这个服务器的 ip 地址;

C:Leader选举的端口;

D:Zookeeper服务器之间的通信端口。

2、创建配置文件

在安装完Zookeeper 后,我们可以在安装目录下找到zoo.cfg配置文件,其中主要的几个配置项内容如下:

#f 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.
dataDir=/usr/local/var/run/zookeeper/zoo2/data

# the port at which the clients will connect
clientPort=2182

 

  • client:监听客户端连接端口
  • tickTime:基本事件单元,这个时间是作为Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳;最小的session过期时间为2倍tickTime
  • syncLimit:fowller与leader之间的心跳时间
  • dataDir:存储内存中数据库快照的位置

在Zookeeper 的工作目录下创建zoo1.cfg,配置内容如下:

#f 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.
dataDir=/usr/local/var/run/zookeeper/zoo1/data

# the port at which the clients will connect
clientPort=2181

#the location of the log file

server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

 

拷贝两份到zoo2.cfg, zoo3.cfg 主要区别为,端口,dataDir需要修改。

Zookeeper的工作目录如下:

➜  zookeeper ll
total 56
-rw-r--r--  1 huangweijie  admin    67B  9  4 22:13 defaults
-rw-r--r--  1 huangweijie  admin   339B  9  4 22:13 log4j.properties
-rw-r--r--  1 huangweijie  admin   941B  9  4 22:13 zoo.cfg
-rw-r--r--  1 huangweijie  admin   512B  9 11 21:01 zoo1.cfg
-rw-r--r--  1 huangweijie  admin   512B  9 11 21:01 zoo2.cfg
-rw-r--r--  1 huangweijie  admin   512B  9 11 21:13 zoo3.cfg
-rw-r--r--  1 huangweijie  admin   941B  9  4 22:13 zoo_sample.cfg

 

快照存放目录创建完后如下:

➜  zookeeper ll
total 0
drwxr-xr-x  3 huangweijie  admin   102B  9 11 20:47 data
drwxr-xr-x  4 huangweijie  admin   136B  9 11 20:33 zoo1
drwxr-xr-x  4 huangweijie  admin   136B  9 11 20:33 zoo2
drwxr-xr-x  4 huangweijie  admin   136B  9 11 20:55 zoo3

 

3、设置myid

在我们配置的dataDir指定的目录下面,创建一个myid文件,里面内容为一个数字,用来标识当前主机,conf/zoo.cfg文件中配置的server.X中X为什么数字,则myid文件中就输入这个数字,例如:

zookeeper echo "1" zoo1/data/myid
zookeeper echo "2" zoo2/data/myid
zookeeper echo "3" zoo3/data/myid

 

4、启动Zookeeper 集群

在Zookeeper 的工作目录下,启动各个节点服务:

➜  zookeeper zkServer start zoo1.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo1.cfg
Starting zookeeper ... STARTED
➜  zookeeper zkServer start zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo2.cfg
Starting zookeeper ... STARTED
➜  zookeeper zkServer start zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo3.cfg
Starting zookeeper ... STARTED

 

启动完成后,可以通过 jps 或 zkServer status 查看服务是否启动:

➜  zookeeper jps
13024 QuorumPeerMain
13176 QuorumPeerMain
13007 QuorumPeerMain
13183 Jps

➜  zookeeper zkServer status zoo1.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo1.cfg
Mode: follower
➜  zookeeper zkServer status zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo2.cfg
Mode: leader

 

三、Zookeeper 命令

1、Zookeeper的四字命令

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

Zookeeper 命令 功能描述
conf 列出相关配置的详细信息
cons 列出所有连接到客户端的回话详细信息
dump 列出未经处理的会话和临时会话
envi 输出关于服务环境的详细信息
reqs 列出未经处理的请求
ruok 测试服务是否处于正确状态
stat 列出关于性能和连接的客户端列表
wchs 列出服务器 watch 的详细信息
wchc 通过session 列出服务器watch 的详细信息
wchp 通过路径列出服务器wtch 的信息信息

调用样例:

  1. echo stat|nc 127.0.0.1 2181 来查看哪个节点被选择作为follower或者leader
  2. echo ruok|nc 127.0.0.1 2181 测试是否启动了该Server,若回复imok表示已经启动。
  3. echo dump| nc 127.0.0.1 2181 ,列出未经处理的会话和临时节点。
  4. echo kill | nc 127.0.0.1 2181 ,关掉server
  5. echo conf | nc 127.0.0.1 2181 ,输出相关服务配置的详细信息。
  6. echo cons | nc 127.0.0.1 2181 ,列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息。
  7. echo envi |nc 127.0.0.1 2181 ,输出关于服务环境的详细信息(区别于 conf 命令)。
  8. echo reqs | nc 127.0.0.1 2181 ,列出未经处理的请求。
  9. echo wchs | nc 127.0.0.1 2181 ,列出服务器 watch 的详细信息。
  10. echo wchc | nc 127.0.0.1 2181 ,通过 session 列出服务器 watch 的详细信息,它的输出是一个与 watch 相关的会话的列表。
  11. echo wchp | nc 127.0.0.1 2181 ,通过路径列出服务器 watch 的详细信息。它输出一个与 session 相关的路径。

2、Zookeeper的 Shell 操作

bin目录下常用的脚本解释

  • zkCleanup  清理Zookeeper历史数据,包括食物日志文件和快照数据文件
  • zkCli     Zookeeper的一个简易客户端
  • zkEnv    设置Zookeeper的环境变量
  • zkServer   Zookeeper服务器的启动、停止、和重启脚本

2.1、ZkServer

zkServer 命令用于对服务器进行操作,主要命令为:

  • zkServer start 启动服务
  • zkServer stop 停止服务
  • zkServer status 查看服务状态

2.2、ZkCli

zkCli 为进入服务的客户端,与redis-cli 类似,当服务启动后,可以通过zkCli 进入服务客户端。常用命令为:

  • zkCli 进入本机默认服务,可以通过指定配置文件进入该服务
  • zkCli -server ip:port 远程连接别的主机的服务

2.3、ZNodes

2.3.1 create

使用create 命令,可以创建一个zookeeper节点,如:

create [-s] [-e] path data acl

 

其中,-s或-e分别指定节点特性,顺序或临时节点,若不指定,则表示持久节点;acl用来进行权限控制。

a、创建顺序节点:

create -s /zk-temp 123

 

执行命令后,会在目录下创建 zk-test 节点,需要注意的是,由于加入了 **-e ** 参数,执行命令后,生成的节点目录名会再后续加上一串数字:  zk-test00000019

b、创建临时节点:

create -e /zk-temp 123

 

临时节点在客户端回话结束后(quit),将会自动删除。再次进入客户端时,将不存在刚创建的临时节点

c、创建永久节点:

create /zk-temp 123

 

不使用参数时,将会默认创建永久节点

2.3.2 读取节点

  • ls path [watch] :查看当前Zookeeper中所包含的内容
  • get path [watch] :获取节点数据内容和属性
  • ls2 path [watch] :ls 和 get 的功能结合

命令调用示例:

[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]

[zk: localhost:2181(CONNECTED) 4] get /zk
myData
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000006
mtime = Fri Oct 17 03:54:20 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

 

2.3.3 Set

set path data [version]

使用Set 命令,可以更新指定节点的数据内容,例如:

[zk: localhost:2181(CONNECTED) 5] set /zk Jayce
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000007
mtime = Fri Oct 17 03:55:50 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0

[zk: localhost:2181(CONNECTED) 6] get /zk
Jayce
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000007
mtime = Fri Oct 17 03:55:50 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0

 

2.3.3 Delete

delete path [version]

通过delete 命令可以删除目标节点内容:

[zk: localhost:2181(CONNECTED) 7] delete /zk
[zk: localhost:2181(CONNECTED) 8] ls /
[zookeeper]

 

0
0
« 上一篇:Spring-boot:快速搭建微框架服务