Download and use of zookeeper and simple demo

Source: Internet
Author: User
Tags constant numeric value zookeeper zookeeper client port number

1, download, http://zookeeper.apache.org/

2, configuration: see: http://www.cnblogs.com/yjmyzz/p/4587663.html

3, first download zookeeper compressed package from the official website, and then extract the downloaded zookeeper compressed package, found "Bin,conf,lib" and other directories. There is a running script in the bin directory, a configuration file is stored in the Conf directory, and a third-party library is stored in the Lib directory that is required to run.

First, standalone mode 1.1. Edit the configuration file

Under the "Conf" directory, create a new file named "Zoo.cfg" with the following contents:

<code class= "Hljs ini has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "hljs-setting" style= "Box-sizing:border-box;" >ticktime=<span class= "Hljs-value" style= "Box-sizing:border-box;" ><span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >2000</span></span></span> <span class= "hljs-setting" style= "Box-sizing:border-box;" >datadir=<span class= "Hljs-value" style= "Box-sizing:border-box;" >/home/dsl/tmp/zookeeper</span></span> <span class= "hljs-setting" style= "Box-sizing:border-box;" >clientport=<span class= "Hljs-value" style= "Box-sizing:border-box;" ><span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; " >2181</span></span></span></coDe><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li></ul>
1.2. Running Zookeeper Server

Execute the bin/zkserver.sh start command, run the zookeeper server process, and get the result as shown in Figure 1, indicating that the background runs the zookeeper server process successfully.

Figure 1

You can also execute the bin/zkserver.sh start-foreground command, run the zookeeper server process in a non-background, and get the results as shown in Figure 2.

Figure 2
1.3. Run the Zookeeper command line client

Execute the bin/zkcli.sh-server 127.0.0.1:2181 command, run the Zookeeper command-line client, get the API as shown in Figure 3, where you can invoke the Operation Znode node tree.

Figure 3
1.4. Invoke the API that operates the Znode node tree

Enter "1.3, run Zookeeper command line client" to get the interface, invoke operation Znode node tree API.
1. Get the initial Znode node
Initially, there is a Znode node with a path identified as "/zookeeper", which saves some metadata and cannot be deleted.
Execute LS/command to get the result as shown in Figure 4.

Figure 4

2. Create Znode node
Execute create/data "Data" command, create a znode node, the path is identified as "/data", contains the content "data". Then execute the LS/command to get the result as shown in Figure 5.

Figure 5

3. Delete the Znode node
Execute the delete/data command to delete the Znode node with the path identified as "/data". Then execute the LS/command to get the result as shown in Figure 6.

Figure 6
second, quorum mode

There are two forms of using the quorum mode: 1) Using multiple machines, running a zookeeper server process on each machine, 2) running multiple zookeeper server processes on that machine using a single machine. In a production environment, the first form is generally used, and in the practice environment, the second form is generally used. 2.1. Parameter Configuration

Quorum mode, to enable a zookeeper server process to function properly, you need to configure some parameters, the following are some common parameters.
1. Data Directory
Used to store process run data.
2. myid file under Data directory
Used to store a numeric value that is used as the identity of the zookeeper server process.
3. Listen for the port number of client side request
This port number is used to listen for client-side requests. (for "Client" See "ZooKeeper" Chapter 2 Getting to Grips with ZooKeeper Figure 2)
4. Listen for the port number of other zookeeper server process communication requests in the same zookeeper cluster
This port number is used to listen for communication requests from other zookeeper server processes in the zookeeper cluster.
5. Monitor the port number of "leader" election request in zookeeper cluster
This port number is used to listen for requests for "leader" elections within the zookeeper cluster. Note that this is an election of "leader" within the zookeeper cluster, regardless of the distributed application.

Parameter Configuration considerations:
1) within the same zookeeper cluster, the identity of the different zookeeper server processes needs to differ, that is, the values in the myID file need not be the same
2) using the 2nd form above to build a zookeeper cluster, you need to pay attention to "directory, port number" and other resources such as non-sharing, if sharing will cause the zookeeper server process does not work properly, such as "Data directory, several listening port number" can not be shared 2.2, Example

Using the 2nd form above to build a zookeeper cluster using quorum mode, cluster planning is shown in table 1.

Table 1

ZooKeeper Server process identity (values in myID files) Data Directory port number to listen for client-side requests port number to listen for communication requests from other zookeeper server processes in the same zookeeper cluster listen for the port number of the "leader" election request in the zookeeper cluster configuration file name
1 /home/dsl/bigdisk/apps/zookeeper-3.4.8/deploy/z1/data 2181 2222 2223 Z1.cfg
2 /home/dsl/bigdisk/apps/zookeeper-3.4.8/deploy/z2/data 2182 3333 3334 Z2.cfg
3 /home/dsl/bigdisk/apps/zookeeper-3.4.8/deploy/z3/data 2183 4444 4445 Z3.cfg

The corresponding directory structure is shown in Figure 1.

Figure 1

The file contents of the 3 files "Z1.cfg,z2.cfg,z3.cfg" are as follows.

<code class= "Hljs makefile has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " ># z1.cfg</span> <span class= "hljs-constant" style= "Box-sizing:border-box;" >ticktime</span>=2000 <span class= "hljs-constant" style= "Box-sizing:border-box;" >initlimit</span>=10 <span class= "hljs-constant" style= "Box-sizing:border-box;" >synclimit</span>=5 <span class= "hljs-constant" style= "Box-sizing:border-box;" >datadir</span>=/home/dsl/bigdisk/apps/zookeeper-3.4.8/deploy/z1/data <span class= "Hljs-constant" Style= "Box-sizing:border-box;" >clientport</span>=2181 <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " The "x" in ># server.x represents ZIdentification of the Ookeeper server process </span> <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; "
># communication between Zookeeper server processes in the same zookeeper cluster can not only use specific point IP addresses, but also use multicast addresses </span> server.1=127.0.0.1:2222:2223 server.2=127.0.0.1:3333:3334 server.3=127.0.0.1:4444:4445 </code><ul class= "pre-numbering" style= " Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sIzing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li><li style= "Box-sizing:border-box; padding:0px 5px; " >9</li><li style= "Box-sizing:border-box; padding:0px 5px; " >10</li><li style= "Box-sizing:border-box; padding:0px 5px; " >11</li><li style= "Box-sizing:border-box; padding:0px 5px; " >12</li><li style= "Box-sizing:border-box; padding:0px 5px; " >13</li></ul>
<code class= "Hljs makefile has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " ># z2.cfg</span> <span class= "hljs-constant" style= "Box-sizing:border-box;" >ticktime</span>=2000 <span class= "hljs-constant" style= "Box-sizing:border-box;" >initlimit</span>=10 <span class= "hljs-constant" style= "Box-sizing:border-box;" >synclimit</span>=5 <span class= "hljs-constant" style= "Box-sizing:border-box;" >datadir</span>=/home/dsl/bigdisk/apps/zookeeper-3.4.8/deploy/z2/data <span class= "Hljs-constant" Style= "Box-sizing:border-box;" >clientport</span>=2182 <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " The "x" in ># server.x represents ZIdentification of the Ookeeper server process </span> <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; "
># communication between Zookeeper server processes in the same zookeeper cluster can not only use specific point IP addresses, but also use multicast addresses </span> server.1=127.0.0.1:2222:2223 server.2=127.0.0.1:3333:3334 server.3=127.0.0.1:4444:4445 </code><ul class= "pre-numbering" style= " Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sIzing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li><li style= "Box-sizing:border-box; padding:0px 5px; " >9</li><li style= "Box-sizing:border-box; padding:0px 5px; " >10</li><li style= "Box-sizing:border-box; padding:0px 5px; " >11</li><li style= "Box-sizing:border-box; padding:0px 5px; " >12</li><li style= "Box-sizing:border-box; padding:0px 5px; " >13</li></ul>
<code class= "Hljs makefile has-numbering" style= "display:block; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " ># z3.cfg</span> <span class= "hljs-constant" style= "Box-sizing:border-box;" >ticktime</span>=2000 <span class= "hljs-constant" style= "Box-sizing:border-box;" >initlimit</span>=10 <span class= "hljs-constant" style= "Box-sizing:border-box;" >synclimit</span>=5 <span class= "hljs-constant" style= "Box-sizing:border-box;" >datadir</span>=/home/dsl/bigdisk/apps/zookeeper-3.4.8/deploy/z3/data <span class= "Hljs-constant" Style= "Box-sizing:border-box;" >clientport</span>=2183 <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; " The "x" in ># server.x represents ZIdentification of the Ookeeper server process </span> <span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box; "
># communication between Zookeeper server processes in the same zookeeper cluster can not only use specific point IP addresses, but also use multicast addresses </span> server.1=127.0.0.1:2222:2223 server.2=127.0.0.1:3333:3334 server.3=127.0.0.1:4444:4445</code><ul class= "pre-numbering" style= " Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); " ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-siZing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li><li style= "Box-sizing:border-box; padding:0px 5px; " >9</li><li style= "Box-sizing:border-box; padding:0px 5px; " >10</li><li style= "Box-sizing:border-box; padding:0px 5px; " >11</li><li style= "Box-sizing:border-box; padding:0px 5px; " >12</li></ul>
2.2.1, running zookeeper Server

Perform bin/zkserver.sh start deploy/z1/z1.cfg,bin/zkserver.sh start deploy/z2/z2.cfg and bin/zkserver.sh start deploy/z3/respectively The Z3.CFG command runs the 3 Zookeeper server processes configured above. 2.2.2, running zookeeper command line client

Executes the Bin/zkcli.sh-server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 command to establish a connection session zookeeper client-to-zookeeper cluster. 2.2.3, invoking an API that operates Znode node tree

As with "1.4, invoke the Operation Znode node tree of the API," 2.2.2, run zookeeper command-line client in the interface, the Operation znode the node tree API can be invoked.



Note:
According to the ZooKeeper Chapter 2 Getting to Grips with ZooKeeper, the "ZooKeeper command-line client" essentially uses the APIs provided by the Java version of the driver library or the C version of the driver library.


Reference documents:
[1]https://zookeeper.apache.org/doc/r3.4.8/zookeeperstarted.html
[2]http://blog.csdn.net/dslztx/article/details/51077606

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.