Zookeeper Getting Started Guide Translation

Source: Internet
Author: User

Zookeeper start wizard
  • Start: Use zookeeper to coordinate distributed programs
    • Singleton operation
    • Manage zookeeper Storage
    • Connect to zookeeper
    • Run zookeeper
    • Run zookeeper in replication Mode
    • Other Optimizations
Getting Started: Coordinates distributed programs through zookeeper. This document contains help information that allows you to quickly start using zookeeper. This article mainly targets the developers who want to try to use zookeeper. It contains some simple examples: use only one zookeeper server, some commands to confirm that the server is running, and a simple program example. At the end of the article, for convenience, some content also takes into account some relatively complex examples. For example, deploy in replication mode to optimize transactions. But if you want to use it in a business project. See the zookeeper Administrator's Guide.
The Singleton operation is simple to start the zookeeper server in singleton mode. The Zookeeper service contains a jar file and some configurations. To start zookeeper, you first need a configuration file in the directory of the downloaded file. Conf/zoo. cfg: Ticktime = 2000 datadir =/var/lib/zookeeper clientport = 2181 you can name the file at will, but for the sake of clarity, we will call it Conf/zoo. cfg. If the datadir directory already exists, change the value of datadir. The following describes the meaning of each field. TicktimeThe time used by zookeeper is in milliseconds. This time is like the heartbeat time of zookeeper, which is twice the timeout time of the Minimum Session unit. DatadirThis directory is the location where data snapshots are stored in the memory. Unless otherwise specified, the log for updating data items is also located. ClientportYou need to create a configuration file to start the log of zookeeper: Bin/zkserver. Sh startzookeeper through log4j. For more information about the log, see the log4j official website. Zookeeper is started in singleton mode. If the process fails, the zookeeper service will be suspended. Single-instance startup is the best for the development environment. If you want to start the replication mode, see running replicated zookeeper. managing zookeeper storage for zookeeper services that run in the production environment for a long time, the storage must be managed (datadir and logs). For more information, see maintenance Connecting to zookeeperIf zookeeper is already running, you can use the following options to connect
  • 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 inSrc/CSubdirectory In the zookeeper sources. See the README containedSrc/CFor full details.

    You can run the program fromSrc/CUsing:

    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 shoshould see something like:

Connecting to localhost:2181log4j: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]        

If you run the command in shell, you can connect to it and Type HELP. This will return the list of commands that can be executed by the client as follows:
[zkshell: 0] helpZooKeeper 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        

You can try some simple commands to understand this simple command line.. First, start by issuing the LIST Command, as in LS, yielding: [zkshell: 8] ls/[zookeeper] Next, create an example by running create/zk_test my_data. this will create a new anode and the string data "my_data" associated with this anode. You can see the following running result:
[zkshell: 9] create /zk_test my_dataCreated /zk_test      

Run the LS/command to view the current directory:
[zkshell: 11] ls /[zookeeper, zk_test]        

Note that the zk_test directory has been created. You can confirm the data associated with the node by running the GET command, as shown below:
[zkshell: 12] get /zk_testmy_datacZxid = 5ctime = Fri Jun 05 13:57:06 PDT 2009mZxid = 5mtime = Fri Jun 05 13:57:06 PDT 2009pZxid = 5cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0dataLength = 7numChildren = 0        

We can also use the set command to change the data associated with zk_test, as shown below:
[zkshell: 14] set /zk_test junkcZxid = 5ctime = Fri Jun 05 13:57:06 PDT 2009mZxid = 6mtime = Fri Jun 05 14:01:52 PDT 2009pZxid = 5cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0dataLength = 4numChildren = 0[zkshell: 15] get /zk_testjunkcZxid = 5ctime = Fri Jun 05 13:57:06 PDT 2009mZxid = 6mtime = Fri Jun 05 14:01:52 PDT 2009pZxid = 5cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0dataLength = 4numChildren = 0      

(Note that we run the GET command again after setting the data of zk_test, and the data does change ). Delete the zk_test node:
[zkshell: 16] delete /zk_test[zkshell: 17] ls /[zookeeper][zkshell: 18]

For more information, see 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.

Run zookeeper in replication mode. Run zookeeper in singleton mode, which is convenient for evaluation and development. However, in the formal environment, you should run in replication mode. Each zookeeper server has the same configuration file. This file is similar to the configuration file used in the singleton mode described above, but it is a little different, as shown below:
tickTime=2000dataDir=/var/lib/zookeeperclientPort=2181initLimit=5syncLimit=2server.1=zoo1:2888:3888server.2=zoo2:2888:3888server.3=zoo3:2888:3888

This new property, InitlimitIs used to define the zookeeper connection to the leader timeout time, attribute SynclimitRestrict the expiration time of a leader. You can also specify TicktimeTime unit measurement. For example, initlimit is 5 ticks, and each tick is 2000 milliseconds, that is, 10 seconds. Attribute server. x lists the components of the zookeeper service. When the server is started, find the server in the myid file in the data directory. This file contains the server number encoded in ASCII. Finally, note the two port numbers after each server name: "2888" and "3888". These ports can be used to connect to each other. For example, a connection is required when data is updated in order. Especially when the zookeeper server is connected to the leader in sequence. When a new leader is born, the younger siblings will connect to the leader through the port number using the TCP protocol. Because the default leader also uses the TCP protocol, we must require another port to elect the leader. Is the second port number of the property server.
Note: If you want to test multiple zookeeper services on one machine, you need to specify the unique cluster name localhost and the Leader Election ports (for example, 2888: 3888,288 9: 3889,289 0: 3890 ). It is also necessary to isolate the datadir directories and different port numbers. (In this example, each running on a single machine has a configuration file)
Other OptimizationsOther configuration parameters can improve performance: there are a couple of other configuration parameters that can greatly increase performance: to reduce waiting and fast updates, it is important to have a transaction log directory. The default transaction log file is put together with the data snapshot and myid file. Attribute datalogdir can be named somewhere else
Last published: 08/07/2014 04: 18: 26 copyright? 2008-2013 the Apache Software Foundation.

Zookeeper Getting Started Guide Translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.