➜ ~ 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
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
➜ 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
# 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
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
➜ ~ 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]
其他操作系统的安装教程可参考:
http://www.cnblogs.com/wuxl360/p/5817489.html
http://blog.csdn.net/morning99/article/details/40426133
ZooKeeper分布式模式安装(ZooKeeper集群)也比较容易,这里说明一下基本要点。 首先要明确的是,ZooKeeper集群是一个独立的分布式协调服务集群,“独立”的含义就是说,如果想使用ZooKeeper实现分布式应用的协调与管理,简化协调与管理,任何分布式应用都可以使用,这就要归功于Zookeeper的数据模型(DataModel)和层次命名空间(Hierarchical Namespace)结构。
本机安装将采用单机安装集群,因此在配置主机时我们将采用同一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服务器之间的通信端口。
在安装完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
在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
在我们配置的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
在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支持某些特定的四字命令字母与其的交互。他们大多数是查询命令,用来获取Zookeeper服务的当前状态及相关信息。用户在客户端可以通过telnet或nc向Zookeeper提交相应的命令。
Zookeeper 命令 | 功能描述 |
---|---|
conf | 列出相关配置的详细信息 |
cons | 列出所有连接到客户端的回话详细信息 |
dump | 列出未经处理的会话和临时会话 |
envi | 输出关于服务环境的详细信息 |
reqs | 列出未经处理的请求 |
ruok | 测试服务是否处于正确状态 |
stat | 列出关于性能和连接的客户端列表 |
wchs | 列出服务器 watch 的详细信息 |
wchc | 通过session 列出服务器watch 的详细信息 |
wchp | 通过路径列出服务器wtch 的信息信息 |
调用样例:
bin目录下常用的脚本解释
zkServer 命令用于对服务器进行操作,主要命令为:
zkCli 为进入服务的客户端,与redis-cli 类似,当服务启动后,可以通过zkCli 进入服务客户端。常用命令为:
使用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
不使用参数时,将会默认创建永久节点
命令调用示例:
[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
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
delete path [version]
通过delete 命令可以删除目标节点内容:
[zk: localhost:2181(CONNECTED) 7] delete /zk
[zk: localhost:2181(CONNECTED) 8] ls /
[zookeeper]