赞
踩
Apache ZooKeeper™
Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的顶级项目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。
Zookeeper 维护了大规模分布式系统中的常用对象,比如配置信息,层次化命名空间等,本文将从开发者的角度详细介绍 Zookeeper 的配置信息的意义以及 Zookeeper 的典型应用场景(配置文件的管理、集群管理、分布式队列、同步锁、Leader 选举、队列管理等)。
Zookeeper-3.4.9
介绍它的安装步骤以及配置信息,最新的代码可以到 Zookeeper 的官网下载: 伪分布式模式安装:
Zookeeper 安装模式包括:单机模式
,伪分布式模式
和完全的集群模式
。单机模式最简单本文跳过(单机模式安装步骤参见 Zeekeeper 官方文档),伪分布式模式与集群模式配置差别不大! 为了帮助大家理解所以本文采用单台虚拟机来配置伪分布集群,
Docker
提供的官方镜像来配置集群环境本文在 CentOS 7.2
上操作,Java 环境为 jdk1.8.0_111
。
安装 Zookeeper 前首先下载你需要的版本,暂时解压到指定目录(本文解压至 /opt/source/
目录下),本次伪分布式模拟 3
个 Zookeeper,目录分配如下:
[hadoop@master source]$ mkdir /opt/source
[hadoop@master source]$ tar zxf /home/zookeeper-3.4.9.tar.gz -C /opt/source
[hadoop@master source]$ cp -a zookeeper zookeeper2 && cp -a zookeeper zookeeper3
[hadoop@master source]$ tree -d -L 1
.
├── zookeeper
├── zookeeper2
└── zookeeper3
[hadoop@master ~]$ tree -d -L 1 /data
/data
├── zookeeper
├── zookeeper2
└── zookeeper3
zoo.cfg
,并修改对应目录下的zoo.cfg。[hadoop@master source]$ egrep -v "^#|^$" zookeeper/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper
clientPort=2181
server.1=master:3331:4881
server.2=master:3332:4882
server.3=master:3333:4883
[hadoop@master source]$ egrep -v "^#|^$" zookeeper2/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper2
clientPort=2182
server.1=master:3331:4881
server.2=master:3332:4882
server.3=master:3333:4883
[hadoop@master source]$ egrep -v "^#|^$" zookeeper3/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper3
clientPort=2183
server.1=master:3331:4881
server.2=master:3332:4882
server.3=master:3333:4883
[hadoop@master ~]$ echo "1" > /data/zookeeper/myid
[hadoop@master ~]$ echo "2" > /data/zookeeper2/myid
[hadoop@master ~]$ echo "3" > /data/zookeeper3/myid
然后依次启动服务:
[hadoop@master ~]$ /opt/source/zookeeper/bin/zkServer.sh start
[hadoop@master ~]$ /opt/source/zookeeper2/bin/zkServer.sh start
[hadoop@master ~]$ /opt/source/zookeeper3/bin/zkServer.sh start
[hadoop@master ~]$ netstat -tnlp |grep 'java'
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 192.168.11.130:4881 0.0.0.0:* LISTEN 3751/java
tcp 0 0 192.168.11.130:4882 0.0.0.0:* LISTEN 3779/java
tcp 0 0 192.168.11.130:4883 0.0.0.0:* LISTEN 3820/java
tcp 0 0 0.0.0.0:52021 0.0.0.0:* LISTEN 3820/java
tcp 0 0 0.0.0.0:42968 0.0.0.0:* LISTEN 3751/java
tcp 0 0 0.0.0.0:45241 0.0.0.0:* LISTEN 3779/java
tcp 0 0 192.168.11.130:3332 0.0.0.0:* LISTEN 3779/java
tcp 0 0 0.0.0.0:2181 0.0.0.0:* LISTEN 3751/java
tcp 0 0 0.0.0.0:2182 0.0.0.0:* LISTEN 3779/java
tcp 0 0 0.0.0.0:2183 0.0.0.0:* LISTEN 3820/java
验证测试:
[hadoop@master ~]$ /opt/source/zookeeper/bin/zkCli.sh -server master:2181
Welcome to ZooKeeper!
JLine support is enabled
2016-12-26 15:20:48,698 [myid:] - INFO [main-SendThread(master:2181):ClientCnxn$SendThread@876] - Socket conn
ection established to master/192.168.11.130:2181, initiating session2016-12-26 15:20:48,725 [myid:] - INFO [main-SendThread(master:2181):ClientCnxn$SendThread@1299] - Session es
tablishment complete on server master/192.168.11.130:2181, sessionid = 0x15939ddfedb0001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: master:2181(CONNECTED) 1] create /test 123
另开一个终端:
[root@master ~]# /opt/source/zookeeper3/bin/zkCli.sh -server master:2183
Welcome to ZooKeeper!
JLine support is enabled
2016-12-26 15:21:40,669 [myid:] - INFO [main-SendThread(master:2183):ClientCnxn$SendThread@876] - Socket conn
ection established to master/192.168.11.130:2183, initiating session2016-12-26 15:21:40,691 [myid:] - INFO [main-SendThread(master:2183):ClientCnxn$SendThread@1299] - Session es
tablishment complete on server master/192.168.11.130:2183, sessionid = 0x35939de0e410001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: master:2183(CONNECTED) 1] get /test
123
cZxid = 0x100000009
ctime = Mon Dec 26 15:21:12 CST 2016
mZxid = 0x100000009
mtime = Mon Dec 26 15:21:12 CST 2016
pZxid = 0x100000009
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
更多详细信息请查看官方文档
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。