Zookeeper官方文档

ZooKeeper Getting Started Guide

Getting Started: Coordinating Distributed Applications with      ZooKeeper

This document contains information to get you started quickly with    ZooKeeper. It is aimed primarily at developers hoping to try it out, and    contains simple installation instructions for a single ZooKeeper server, a    few commands to verify that it is running, and a simple programming    example. Finally, as a convenience, there are a few sections regarding    more complicated installations, for example running replicated    deployments, and optimizing the transaction log. However for the complete    instructions for commercial deployments, please refer to the ZooKeeper    Administrator‘s Guide.

Pre-requisites

See System Requirements in the Admin guide.

Download

To get a ZooKeeper distribution, download a recent        stable release from one of the Apache Download        Mirrors.

Standalone Operation

Setting up a ZooKeeper server in standalone mode is      straightforward. The server is contained in a single JAR file,      so installation consists of creating a configuration.

Once you‘ve downloaded a stable ZooKeeper release unpack      it and cd to the root

To start ZooKeeper you need a configuration file. Here is a sample,      create it in conf/zoo.cfg:

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

This file can be called anything, but for the sake of this      discussion call      it conf/zoo.cfg. Change the      value of dataDir to specify an      existing (empty to start with) directory.  Here are the meanings      for each of the fields:

  • tickTime
  • the basic time unit in milliseconds used by ZooKeeper. It is            used to do heartbeats and the minimum session timeout will be            twice the tickTime.
  • dataDir
  • the location to store the in-memory database snapshots and,            unless specified otherwise, the transaction log of updates to the            database.
  • clientPort
  • the port to listen for client connections

Now that you created the configuration file, you can start      ZooKeeper:

bin/zkServer.sh start

ZooKeeper logs messages using log4j -- more detail      available in the      Logging      section of the Programmer‘s Guide. You will see log messages      coming to the console (default) and/or a log file depending on      the log4j configuration.

The steps outlined here run ZooKeeper in standalone mode. There is      no replication, so if ZooKeeper process fails, the service will go down.      This is fine for most development situations, but to run ZooKeeper in      replicated mode, please see Running Replicated      ZooKeeper.

Managing ZooKeeper Storage

For long running production systems ZooKeeper storage must      be managed externally (dataDir and logs). See the section on      maintenance for      more details.

Connecting to ZooKeeper

Once ZooKeeper is running, you have several options for connection      to it:

  • 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]

From the shell, type help to get a listing of commands that can be executed from the client, as in:

[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

From here, you can try a few simple commands to get a feel for this simple command line interface.  First, start by issuing the list command, as      in ls, yielding:

[zkshell: 8] ls /
[zookeeper]

Next, create a new znode by running create /zk_test my_data. This creates a new znode and associates the string "my_data" with the node.      You should see:

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

Issue another ls / command to see what the directory looks like:

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

Notice that the zk_test directory has now been created.

Next, verify that the data was associated with the znode by running the get command, as in:

[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

We can change the data associated with zk_test by issuing the set command, as in:

[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

(Notice we did a get after setting the data and it did, indeed, change.

Finally, let‘s delete the node by issuing:

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

That‘s it for now.  To explore more, continue with the rest of this document and see the 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.

Running Replicated ZooKeeper

Running ZooKeeper in standalone mode is convenient for evaluation,      some development, and testing. But in production, you should run      ZooKeeper in replicated mode. A replicated group of servers in the same      application is called a quorum, and in replicated      mode, all servers in the quorum have copies of the same configuration      file. The file is similar to the one used in standalone mode, but with a      few differences. Here is an example:

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

The new entry, initLimit is      timeouts ZooKeeper uses to limit the length of time the ZooKeeper      servers in quorum have to connect to a leader. The entry syncLimit limits how far out of date a server can      be from a leader.

With both of these timeouts, you specify the unit of time using      tickTime. In this example, the timeout      for initLimit is 5 ticks at 2000 milleseconds a tick, or 10      seconds.

The entries of the form server.X list the      servers that make up the ZooKeeper service. When the server starts up,      it knows which server it is by looking for the file      myid in the data directory. That file has the      contains the server number, in ASCII.

Finally, note the two port numbers after each server       name: " 2888" and "3888". Peers use the former port to connect       to other peers. Such a connection is necessary so that peers       can communicate, for example, to agree upon the order of       updates. More specifically, a ZooKeeper server uses this port       to connect followers to the leader. When a new leader arises, a       follower opens a TCP connection to the leader using this       port. Because the default leader election also uses TCP, we       currently require another port for leader election. This is the       second port in the server entry.

Note

If you want to test multiple servers on a single        machine, specify the servername        as localhost with unique quorum &        leader election ports (i.e. 2888:3888, 2889:3889, 2890:3890 in        the example above) for each server.X in that server‘s config        file. Of course separate dataDirs and        distinct clientPorts are also necessary        (in the above replicated example, running on a        single localhost, you would still have        three config files).

Other Optimizations

There are a couple of other configuration parameters that can      greatly increase performance:

  • To get low latencies on updates it is important to          have a dedicated transaction log directory. By default          transaction logs are put in the same directory as the data          snapshots and myid file. The dataLogDir          parameters indicates a different directory to use for the          transaction logs.
  • [tbd: what is the other config param?]
时间: 2024-11-03 18:46:35

Zookeeper官方文档的相关文章

<译>Zookeeper官方文档

apache原文地址:http://zookeeper.apache.org/doc/trunk/zookeeperOver.html ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications Design Goals Data model and the hierarchical namespace Nodes and ephemeral nodes Conditional upda

[译]Polymer官方文档-布局元素

原文链接: Layout elements(注:有时需翻墙或换hosts)翻译日期: 2014年8月2日翻译人员: 铁锚 译注: 点击下载本教程的示例代码: Polymer布局元素Demo相关的代码文件在 layout 目录下. 0. 准备- Chrome浏览器模拟移动设备Chrome浏览器(要求最新版,如34以上版本)模拟移动设备的方法: 打开一个标签页,按 F12,(或者在页面空白区域点鼠标右键,审查元素),打开调试窗口. 然后在将光标移动到调试窗口中, 按 ESC键,或者点击右上角的 dr

ZooKeeper官方文档资源

一般来说官方的文档是最权威的. 入口:http://zookeeper.apache.org/ 在右侧即可进入相应版本文档: 如果想要看主干的文章,入口如下,主干是最稳当的版本:http://zookeeper.apache.org/doc/trunk/

zookeeper 官方文档——综述

Zookeeper: 一个分布式应用的分布式协调服务 zookeeper 是一个分布式的,开源的协调服务框架,服务于分布式应用程序. 它暴露了一系列基础操作服务,因此,分布式应用能够基于这些服务构建出更高级别的服务,比如,同步,配置管理,命名和分组服务. zookeeper设计上易于编码,数据模型构建在我们熟悉的树形结构目录风格的文件系统中. zookeeper运行在java中,同时支持java和C 语言.正确的实现协调服务是公认的难干的差事. 他们及其容易出错,比如资源竞争和死锁. zooke

Unity性能优化(2)-官方文档简译

本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 简介 如果游戏运行缓慢,卡顿,我们知道游戏存在性能问题.在我们尝试解决问题前,需要先知道引起问题的原因.不同问题需要不同的解决方案.如果我们靠猜测或者其他项目的经验去解决问题,那么我们可能会浪费很多时间,甚至使得问题更严重. 这时我们需要性能分析,性能分析程序测量游戏运行时的各个方面性能.通过性能分析工具,我们能够透过

Docker安全性——官方文档[译]

Docker安全性--官方文档[译] 本文译自Docker官方文档:https://docs.docker.com/articles/security/ 在审查Docker的安全时,需要考虑三个主要方面:?容器内在的安全性,由内核命名空间和cgroup中实现;?docker守护程序本身的攻击面;?加固内核安全特性,以及它们如何与容器中互动. 内核 命名空间 Kernel Namespace Docker容器中非常相似LXC容器,并且它们都具有类似的安全功能.当您以"docker run"

由浅入深Zookeeper详解(参考官方文档)

[老哥我最近接到个任务研究一下Zookeeper,对于我这个Linux运维领域的小菜鸟来说也是刚刚听到这个名字,为了养成良好的文档整理和学习能力,我人生第一次开通了博客并把这次的研究经历记录了下来,以后我会不定期的记录下来我对技术领域的探索,希望热爱Linux运维志同道合的兄弟们多指教,一同进步成长.(ps:我本人平时比较沉默,善于观察思考,对历史人物颇有见解,但是一旦说起话来就会滔滔不绝,谁让我曾经的梦想是当一名教师呢!哈哈!)同时,送给大家一句话,人生是一场马拉松比赛,只有坚持到最后的人,才

【译】StackExchange.Redis官方文档-中文版(序)

StackExchange.Redis官方文档-中文版(序) Intro 最近想深入学习一些 Redis 相关的东西.于是看了看官方的项目,发现里面有一份文档,于是打算翻译成中文,方便大家学习参考,如果有什么翻译不准确的地方,欢迎大家指出. 翻译进度 文档还有一部分还未翻译完,我会争取在三月中旬前将剩下的部分翻译结束. 翻译进度详见:https://github.com/WeihanLi/StackExchange.Redis-docs-cn 文档地址: 原文文档地址: https://gith

HBase 官方文档0.90.4

HBase 官方文档0.90.4 Copyright ? 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision 0.90.4 配置,数据模型使用入门 Abstract 这是 Apache HBase的官方文档, Hbase是一个分布式,版本化(versioned),构建在 Apache Hadoop和 Apache ZooKeeper上的列数据库. 我(译者)熟悉Hbase的源代码,从事Hbase