ZooKeeper Getting Started Guide 翻译

ZooKeeper 开始向导

  • 开始:用zookeeper协调分布式程序
    • 单例操作
    • 管理zookeeper存储
    • 连接zookeeper
    • 运行zookeeper
    • 以复制模式运行zookeeper
    • 其它优化

Getting Started:通过zookeeper协调分布式程序

这份文档包含了让你快速开始使用zookeeper的帮助信息。文章主要是针对初级想尝试使用zookeeper的开发者,其中包含了一些简单的例子,仅用一台zookeeper服务器,一些命令确认服务器正在运行,一个简单的程序样例。文章最后,为了方便,也有一些内容考虑到一些相对复杂些的例子。列如,以复制模式部署,优化事务。但是如果想运用到商业项目中。请参阅 ZooKeeper
Administrator‘s Guide
.

单例操作

以单例模式启动zookeeper服务器是简单的。zookeeper服务包含一个jar文件和一些配置。

启动zookeeper你首先需要一份配置文件,在下载的文件目录conf/zoo.cfg:

tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181

你可以随意命名这个文件的名字,但是为了描述的清楚我们就叫它 conf/zoo.cfg. 假如已经存在dataDir目录请改变dataDir的值。下面是对每一个字段意义的介绍

tickTime

zookeeper用到的时间单位是毫秒。这个时间好比是zookeeper的心跳时间,是最小会话单元的超时时间范围是这个时间的2倍。

dataDir

这个目录是存储内存中数据快照的位置,除非你特别说明,更新数据事物的log也在这个位置。

clientPort

客户端连接监听端口,你需要创建一个配置文件,启动ZooKeeper:bin/zkServer.sh start

ZooKeeper 的日志记录是通过log4j — 更多关于日志的讲解请看log4j官网。这里介绍的启动zookeeper是以单例模式。假如进程运行失败,zookeeper服务就会挂掉。单例模式启动对于开发环境来说是最好的,如果想已复制模式启动请看Running
Replicated ZooKeeper
.

Managing ZooKeeper Storage

对于长时间运行在生产环境的zookeeper服务,存储必须被额外的管理(dataDir and logs),关于这些请看maintenance

Connecting to ZooKeeper

如果zookeeper已经运行,你就可以通过下面几种选择进行连接

  • Java: Use

    bin/zkCli.sh -server 127.0.0.1:2181
    

    This lets you perform simple, file-like operations.

  • C: compile cli_mt (multi-threaded) or cli_st (single-threaded) by running make
    cli_mt or make cli_st in
    the src/c subdirectory in the ZooKeeper sources. See the README contained within src/c for full details.

    You can run the program from src/c using:

    LD_LIBRARY_PATH=. cli_mt 127.0.0.1:2181
    

    or

    LD_LIBRARY_PATH=. cli_st 127.0.0.1:2181
    

    This will give you a simple shell to execute file system like operations on ZooKeeper.

Once you have connected, you should see something like:

Connecting to localhost:2181
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
Welcome to ZooKeeper!
JLine support is enabled
[zkshell: 0]

如果在shell中运行,你可以子啊连接后键入 help ,这将返回客户端可以执行的命令列表

如下:

[zkshell: 0] help
ZooKeeper host:port cmd args
        get path [watch]
        ls path [watch]
        set path data [version]
        delquota [-n|-b] path
        quit
        printwatches on|off
        create path data acl
        stat path [watch]
        listquota path
        history
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        deleteall path
        setquota -n|-b val path

你可以尝试一些简单的命令来了解这个简单的命令行.
First, start by issuing the list command, as in ls, yielding:

[zkshell: 8] ls /[zookeeper]

接下来,创建一个咋弄的通过运行create /zk_test my_data.这个会创建一个新的anode和与这个anode关联的字符串数据”my_data” ,你可以看到下面的运行结果:

[zkshell: 9] create /zk_test my_data
Created /zk_test

通过运行 ls / 命令会看到展现当前的目录情况:

[zkshell: 11] ls /
[zookeeper, zk_test]

注意,这个zk_test目录已经被创建。你可以确认下和这个节点关联的数据通过运行get命令,如下:

[zkshell: 12] get /zk_test
my_data
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 5
mtime = Fri Jun 05 13:57:06 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0
dataLength = 7
numChildren = 0

我们也可以改变和zk_test关联的数据通过set命令,如下:

[zkshell: 14] set /zk_test junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0
[zkshell: 15] get /zk_test
junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0

(注意我们在设置zk_test的数据后又运行了get 
命令,数据的确改变了)。

最后通过delete 删除zk_test这个节点:

[zkshell: 16] delete /zk_test
[zkshell: 17] ls /
[zookeeper]
[zkshell: 18]

获取更多其它内容,请看Programmer‘s Guide.

Programming to ZooKeeper

ZooKeeper has a Java bindings and C bindings. They are functionally equivalent. The C bindings exist in two variants: single threaded and multi-threaded. These differ only
in how the messaging loop is done. For more information, see the Programming Examples in the
ZooKeeper Programmer‘s Guide
 for sample code using of the different APIs.

复制模式运行zookeeper

运行zookeeper以单例模式对于评估测试开发是很方便。但是在正式环境你应该以复制模式运行。每个zookeeper服务器都有一份同样的配置文件。这个文件和上面介绍单例模式使用的配置文件和类似,只是有一点小小的不同,如下:

tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888

这个新的属性, initLimit是用来定义zookeeper连接到leader的超时时间,属性syncLimit 限制一个leader过时时间。对于这两种超时时间,你也可以指定用tickTime的时间单位计量。例如,initLimit是5
ticks,每个tick是2000毫秒,也就是10秒。

属性server.X 列出了zookeeper服务的组成。当服务器启动的时候,通过寻找在数据目录的myid文件知道是哪台服务器。这个文件含有以ASCII编码的服务器编号。

最后,注意在每个服务器名后的两个端口号: " 2888" and "3888”.通过这些端口可以彼此连接。例如,一个彼此连接是必的当按顺序更新数据时。尤其在zookeeper服务器依次连接到leader时候。当一个新的leader诞生时,小弟们会通过这个端口号利用tcp协议连接到leader。因为默认leader也用tcp协议,我们必须要求另外一个端口用于选举leader。就是属性server的第二个端口号。

注意

假如你想在一台机器上进行多个zookeeper服务测试,需要指出唯一的集群名localhost和那些leader选举端口(例如2888:3888,
2889:3889, 2890:3890 )。隔离各个dataDir目录和不同的端口号也是必须的。(在这个复制模式运行的例子里,每个运行在单一的机器都有一个配置文件)

其它优化

有其它的配置参数可以提高性能:There are a couple of other configuration parameters that can greatly increase performance:

为了减少等待和快速更新,它是重要的有个事物log目录。默认事物log文件是和数据快照和myid文件放在一起。属性dataLogDir可以指名别的地方

Last Published: 08/07/2014 04:18:26

Copyright ? 2008-2013 The Apache Software Foundation.

时间: 2024-07-31 02:47:43

ZooKeeper Getting Started Guide 翻译的相关文章

Apache ZooKeeper Getting Started Guide 翻译

ZooKeeper 開始向导 開始:用zookeeper协调分布式程序 单例操作 管理zookeeper存储 连接zookeeper 执行zookeeper 以复制模式执行zookeeper 其他优化 Getting Started:通过zookeeper协调分布式程序 这份文档包括了让你高速開始使用zookeeper的帮助信息. 文章主要是针对0基础想尝试使用zookeeper的开发人员,当中包括了一些简单的样例.仅用一台zookeeperserver,一些命令确认server正在执行,一个简

Dagger2 User's Guide(翻译)

概述 依赖注入(dependency injection)是一个对象为另一个对象提供依赖关系的技术手段.简单点说,就是一个对象(client)要依赖其它对象(services)才能完成工作,那么这个对象(client)就对其它对象(services)产生了依赖,而依赖注入就是把依赖(services)在需要的时候自动传给client,而不是client自己创建或者寻找services.也就是说客户对象(client)把提供依赖的职责交给了外部代码(注入器),注入器(injector)的注入代码会

Performance Tuning guide 翻译 || Performance Tuning Guide 11G中新增特性

Performance Tuning Guide 11G中新增特性 本章描述了Oracle11g Release2(11.2)中增加了哪些新的性能调整 特性,以及指向这些增加信息. 本章节描述的特性以及增强,包含了优化数据库性能的各个方面. 关于Oracle11gR2的所有新特性汇总,可以查看Oracle Database New Features Guide. 11.2.0.2中新增的新特性(关于性能调优) 新增的以及更新过的性能调整特性包括: 注:Resource Manager(资源管理器

Performance Tuning guide 翻译 || 前言

前言Preface 包括如下几个小节 l Audience l Documentation Accessibility l Related Documents l Conventions 目标人群Audience Oracle Database Performance Tuning Guide 针对的人群是DBA.这个指南描述了怎样使用命Oracle数据库性能工具(通过命令行使用)来优化数据库性能,以及调整SQL语句. 这个指南同样描述了 创建初始化一个数据库时关于性能方面的最佳实践,以及包括性

ZooKeeper Getting Started Guide

http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html What is ZooKeeper? ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kin

ZooKeeper Administrator's Guide A Guide to Deployment and Administration(吃别人嚼过的馍没意思,直接看官网资料)

Deployment System Requirements Supported Platforms Required Software Clustered (Multi-Server) Setup Single Server and Developer Setup Administration Designing a ZooKeeper Deployment Cross Machine Requirements Single Machine Requirements Provisioning

ZooKeeper Recipes and Solutions 翻译

ZooKeeper 秘诀 与解决方案 A Guide to Creating Higher-level Constructs with ZooKeeper Out of the Box Applications: Name Service, Configuration, Group Membership Barriers Double Barriers Queues Priority Queues Locks Shared Locks Recoverable Shared Locks Two-p

ZooKeeper开发手册中文翻译

本文假设你已经具有一定分布式计算的基础知识.你将在第一部分看到以下内容: ZooKeeper数据模型 ZooKeeper Sessions ZooKeeper Watches 一致性保证(Consistency Guarantees) 接下来的4小节讲述了程序开发的实际应用: 创建模块--ZooKeeper操作指引 编程语言接口 简单示例演示程序的结构 常见问题和故障 本文的附录中包含和ZooKeeper相关的有用信息. ZooKeeper的数据模型 ZooKeeper有一个类似分布式文件系统的

安卓API Guide翻译 (官方)Device Compatibility 设备相容性1.2

翻译不对的地方请大家指正.定期每两天更新一篇,欢迎关注. 1.2Device Compatibility 1.2设备相容性 Android is designed to run on many different types of devices, from phones to tablets and televisions. As a developer, the range of devices provides a huge potential audience for your app.