Zookeeper 初体验之——伪分布式安装(转)

时间:2022-08-30 23:23:01

原文地址: http://blog.csdn.net/salonzhou/article/details/47401069

简介

Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的*项目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。

Zookeeper 维护了大规模分布式系统中的常用对象,比如配置信息,层次化命名空间等,本文将从开发者的角度详细介绍 Zookeeper 的配置信息的意义以及 Zookeeper 的典型应用场景(配置文件的管理、集群管理、分布式队列、同步锁、Leader 选举、队列管理等)。

Zookeeper 安装与配置

本文采用 Zookeeper-3.4.0 以基础介绍它的安装步骤以及配置信息,最新的代码可以到 Zookeeper 的官网http://zookeeper.apache.org/下载。Zookeeper功能强大,但是安装却十分简单,下面重点以伪分布式模式来介绍 Zookeeper 的安装。

伪分布式模式安装

Zookeeper 安装模式包括:单机模式,伪分布式模式和完全的集群模式。单机模式最简单,本文将跳过单机模式安装(单机模式安装步骤参见 Zeekeeper 官方文档http://zookeeper.apache.org/doc/current/zookeeperStarted.html),伪分布式模式与集群模式配置差别不大,由于手头机器有限,所以本文采用了在单台机器上伪分布式安装。

本文在Ubuntu 12.04上操作,Java 环境为 OpenJDK 1.7。安装 Zookeeper 前首先下载你需要的版本,暂时解压到指定目录(本文解压至~/zookeeper/目录下),并修改配置(可能需要多次修改配置文件),本次伪分布式模拟 5 个 Zookeeper 节点,事先在/tmpzookeeper目录下建立5个文件夹,分别命名为:server001,server002,server003,server004,server005,然后在每个server00#文件夹下面新建 data 和 logs 子文件夹。

Zookeeper 的配置文件主要在 conf 目录,包括zoo.cfg (zoo_sample.cfg)和log4j.properties,修改 zoo_sample.cfg,重命名为zoo.cgf,打开zoo.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=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
#
# 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
Zookeeper 初体验之——伪分布式安装(转)

将内容修改为(server001节点的配置文件):

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=/tmp/zookeeper/server001/data
dataLogDir=/tmp/zookeeper/server001/logs
# the port at which the clients will connect
clientPort=2181
#
# 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=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775
Zookeeper 初体验之——伪分布式安装(转)
  • tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
  • dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
  • clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
  • initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒
  • syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒
  • server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

然后将此 zookeeper 包拷贝至 /tmp/zookeeper/server001/ 目录下,并在 /tmp/zookeeper/server001/data/ 下建立一个myid文件,文件内容为 1,

echo "1" >> /tmp/zookeeper/server001/data/myid

继续修改~/zookeeper/目录中的zookeeper配置文件文件(server002的配置文件,注意 clientPort=2182,与 server001 中的 clientPort=2181 不同,后续修改配置均需设置不同的 clientPort),内容如下:

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=/tmp/zookeeper/server002/data
dataLogDir=/tmp/zookeeper/server002/logs
# the port at which the clients will connect
clientPort=2182
#
# 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=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775
Zookeeper 初体验之——伪分布式安装(转)

然后将此 zookeeper 包拷贝至 /tmp/zookeeper/server002/ 目录下,并在 /tmp/zookeeper/server002/data/ 下建立一个myid文件,文件内容为 2,

echo "2" >> /tmp/zookeeper/server001/data/myid

依次修改配置文件,建立 server003,server004,server005节点文件夹,完成上述步骤后/tmp/zookeeper目录结构如下:

Zookeeper 初体验之——伪分布式安装(转)
forhappy@forhappy-lenovo:/tmp/zookeeper$ tree -d -L 2
.
├── server001
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server002
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server003
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server004
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
└── server005
├── data
├── logs
└── zookeeper-3.4.0
Zookeeper 初体验之——伪分布式安装(转)

然后依次进入每个文件夹节点的zookeeper目录中,启动zookeeper服务,

$ bin/zkServer.sh start

前面两个起的时候可能会有错误提示:

2012-11-21 01:28:12,848 - WARN  [QuorumPeer:/0:0:0:0:0:0:0:0:2184:QuorumCnxManager@379] - Cannot open channel to 3 at election address localhost/127.0.0.1:3890
Java.NET.ConnectException: Connection refused

2012-11-21 01:28:12,847 - WARN  [QuorumPeer:/0:0:0:0:0:0:0:0:2184:QuorumCnxManager@379] - Cannot open channel to 2 at election address localhost/127.0.0.1:3889
java.net.ConnectException: Connection refused

这个可以不用管,等服务都起来之后就不会有这个提示了.

如果一切顺利,Zookeeper 伪分布式模式安装成功,下面验证 Zookeeper 安装的正确性。

进入任意一个文件夹节点的zookeeper包所在的目录,执行一下命令:

$ bin/zkCli.sh  -server 127.0.0.1:2181

执行成功后:

Zookeeper 初体验之——伪分布式安装(转)
forhappy@forhappy-lenovo:/tmp/zookeeper/server001/zookeeper-3.4.0$ bin/zkCli.sh  -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181 Welcome to ZooKeeper! WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: 127.0.0.1:2181(CONNECTED) 0]
Zookeeper 初体验之——伪分布式安装(转)

help 帮助:

Zookeeper 初体验之——伪分布式安装(转)
[zk: 127.0.0.1:2181(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
Zookeeper 初体验之——伪分布式安装(转)

至此,Zookeeper 安装完成

Zookeeper 初体验之——伪分布式安装(转)的更多相关文章

  1. Hadoop大数据初入门----haddop伪分布式安装

    一.hadoop解决了什么问题 hdfs 解决了海量数据的分布式存储,高可靠,易扩展,高吞吐量mapreduce 解决了海量数据的分析处理,通用性强,易开发,健壮性 yarn 解决了资源管理调度 二. ...

  2. ZooKeeper:win7上安装单机及伪分布式安装

    zookeeper是一个为分布式应用所设计的分布式的.开源的调度服务,它主要用来解决分布式应用中经常遇到的一些数据管理问题,简化分布式应用,协调及其管理的难度,提高性能的分布式服务. 本章的目的:如何 ...

  3. zookeeper伪分布式安装

    本文介绍zookeeper伪分布式安装. 所谓 “伪分布式集群” 就是在1台PC中启动多个zookeeper的实例.“完全分布式集群” 是每1台PC启动1个ZooKeeper实例. 由于我的测试环境P ...

  4. HBase伪分布式安装(HDFS)+ZooKeeper安装+HBase数据操作+HBase架构体系

    HBase1.2.2伪分布式安装(HDFS)+ZooKeeper-3.4.8安装配置+HBase表和数据操作+HBase的架构体系+单例安装,记录了在Ubuntu下对HBase1.2.2的实践操作,H ...

  5. HBase基础和伪分布式安装配置

    一.HBase(NoSQL)的数据模型 1.1 表(table),是存储管理数据的. 1.2 行键(row key),类似于MySQL中的主键,行键是HBase表天然自带的,创建表时不需要指定 1.3 ...

  6. Hadoop生态圈-hbase介绍-伪分布式安装

    Hadoop生态圈-hbase介绍-伪分布式安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HBase简介 HBase是一个分布式的,持久的,强一致性的存储系统,具有近似最 ...

  7. 第二章 伪分布式安装hadoop hbase

    安装单机模式的hadoop无须配置,在这种方式下,hadoop被认为是一个单独的java进程,这种方式经常用来调试.所以我们讲下伪分布式安装hadoop. 我们继续上一章继续讲解,安装完先试试SSH装 ...

  8. Hadoop单机和伪分布式安装

    本教程为单机版+伪分布式的Hadoop,安装过程写的有些简单,只作为笔记方便自己研究Hadoop用. 环境 操作系统 Centos 6.5_64bit   本机名称 hadoop001   本机IP ...

  9. hbase伪分布式安装(单节点安装)

    hbase伪分布式安装(单节点安装) http://hbase.apache.org/book.html#quickstart   1.    前提配置好java,环境java变量     上传jdk ...

随机推荐

  1. 执行大量的Redis命令,担心效率问题?用Pipelining试试吧~

    参考的优秀文章 Request/Response protocols and RTT 来源 原来,系统中一个树结构的数据来源是Redis,由于数据增多.业务复杂,查询速度并不快.究其原因,是单次查询的 ...

  2. 文件映射spring 使用classpath方式加载hibernate映射文件

    在改章节中,我们主要介绍文件映射的内容,自我感觉有个不错的建议和大家分享下 <!-- 批量指定到classpath下面 --> <property name="mappin ...

  3. 快递查询api&lpar;多接口方案&rpar;

    /** 本环境使用php+smarty,结合两种快递api调取快递数据 * 说明,先快递鸟调取数据,失败后再调取快递网的数据* 快递鸟 http://www.kdniao.com 快递网 http:/ ...

  4. nginx的autoindex-目录浏览还有其它两个参数

    不知的话,显示的时间是不一定是我们想要的.. http://blog.csdn.net/yuanchao99/article/details/16354163 Nginx打开目录浏览功能(autoin ...

  5. Asp&period;Net 禁用cookie后使用session

    原文地址:http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/using-cookie-less-session-in-Asp-Net/ H ...

  6. window下面配置sftp

    Windows  下 搭建 基于  ssh 的sftp 服务器,服务器端可以用 freesshd,F-secure server 等,filezilla server不可用,之前傻乎乎的用filezi ...

  7. appium API记录

    1,滑动屏幕 driver.swipe(start_x=720, start_y=1000, end_x=0, end_y=1000,duration=1000) ,注意开始坐标不能是最大值 2,获取 ...

  8. MariaDB快速批量插入数据的几种办法

    前言 当要向MariaDB中插入新的数据时,以下过程会影响插入所消耗的时间:(按时间消耗长短降序排序) 将数据sync到磁盘上(它是事务结束的一部分) 添加新的键值.索引越大,更新键值所消耗的时间就越 ...

  9. CAS 5&period;1&period;x 的搭建和使用(三)—— 通过官方示例来熟悉客户端搭建

    CAS单点登录系列: CAS 5.1.x 的搭建和使用(一)—— 通过Overlay搭建服务端 CAS5.1.x 的搭建和使用(二)—— 通过Overlay搭建服务端-其它配置说明 CAS5.1.x ...

  10. hdu4758 Walk Through Squares

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=4758 题目: Walk Through Squares Time Limit: 4000/20 ...