[TOC]
概述
关键字
- 分布式
- open source
- 协调服务(For 分布式应用程序)
- 配置维护
- 域名维护
- 分布式同步
- 组服务
起源
- Google的Chubby的开源实现
- Hadoop和Hbase的重要组件
- 算法基础是Fast Paxos的改进版
目标
- 封装好复杂易出错的关键服务
- 提供简单、高效、稳定的系统
接口
- 包含简单的原语集(原语:不可分割的机器指令集)
- 提供Java和C的接口
运转流程
- 选举Leader
- 同步数据
分治
状态机,参考Link
实战
部署伪集群
- 从zookeeper官网下载最新stable版本
- 解压并复制三份:
drwxr-xr-x 11 work work 4.0K May 20 19:37 zookeeper00
drwxr-xr-x 11 work work 4.0K May 20 19:49 zookeeper01
drwxr-xr-x 11 work work 4.0K May 20 19:49 zookeeper02
-rw-r--r-- 1 work work 34M May 20 19:29 zookeeper-3.4.10.tar.gz
- 分别添加配置和mypid如下:
-- zoo1
➜ tools cat zookeeper00/conf/zoo.cfg
tickTime=2000
dataDir=log
clientPort=8181
initLimit=5
syncLimit=2
server.1=127.0.0.1:8182:8183
server.2=127.0.0.1:8282:8283
server.3=127.0.0.1:8382:8383
➜ tools cat zookeeper00/log/myid
1
-- zoo2
➜ tools cat zookeeper00/conf/zoo.cfg
tickTime=2000
dataDir=log
clientPort=8281
initLimit=5
syncLimit=2
server.1=127.0.0.1:8182:8183
server.2=127.0.0.1:8282:8283
server.3=127.0.0.1:8382:8383
➜ tools cat zookeeper01/log/myid
2
-- zoo3
➜ tools cat zookeeper02/conf/zoo.cfg
tickTime=2000
dataDir=log
clientPort=8381
initLimit=5
syncLimit=2
server.1=127.0.0.1:8182:8183
server.2=127.0.0.1:8282:8283
server.3=127.0.0.1:8382:8383
➜ tools cat zookeeper02/log/myid
3
- 分别启动三个zookeeper
$ cd zookeeper00
$ bin/zkServer.sh start
$ cd ../zookeeper01
$ bin/zkServer.sh start
$ cd ../zookeeper02
$ bin/zkServer.sh start
-
检查进程
检查是否启动成功
$ bin/zkCli.sh -server 127.0.0.1:8181
...
Welcome to ZooKeeper!
...
[zk: 127.0.0.1:8181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
[zk: 127.0.0.1:8181(CONNECTED) 1] ls /
[zookeeper]
Python应用注册到此集群
- 编译并安装c版本的zookeeper client
$ cd zookeeper-3.4.10/src/c
# 这里指定安装目录,以便在非root权限下安装
$ ./configure --prefix=/home/work/local
$ make -j8
$ make install
# 需要修改环境变量以便能使用编译的zookeeper client
$ cat ~/.bashrc
export PATH="$HOME/local/bin:$PATH"
export LD_RUN_PATH="$HOME/local/lib:$LD_RUN_PATH"
export LD_LIBRARY_PATH="$HOME/local/lib:$LD_LIBRARY_PATH"
export LIBRARY_PATH="$HOME/local/lib:$LD_LIBRARY_PATH"
export C_INCLUDE_PATH="$HOME/local/include/zookeeper:$C_INCLUDE_PATH"
$ source ~/.bashrc
- 安装zookeeper python库参考链接
$ easy_install zkpython
# 或者
$ pip install zkpython