Installation, configuration, startup, and use of ZooKeeper (1) -- Single-host mode, single-host zookeeper

Source: Internet
Author: User
Tags zookeeper client

Installation, configuration, startup, and use of ZooKeeper (1) -- Single-host mode, single-host zookeeper

ZooKeeper is easy to install. It works in standalone mode, cluster mode, and pseudo cluster mode. This blog aims to summarize how to install, configure, start, and use ZooKeeper in standalone mode:

1. install and configure ZooKeeper (in Windows)

A. Download the ZooKeeper compressed Installation File. Download the stable version here --Zookeeper-3.4.5.tar.gz

B. decompress the compressed file and decompress it to the C root directory. Open the decompressed folder:

 

C. Click the "conf" folder and you will see:


D. open the file named "zoo_sample.cfg" in Notepad. You can see the following content:

# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=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 connectclientPort=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
The following describes the preceding parameters:
TickTime: defines the interval between the ZooKeeper server or between the client and the server to maintain the heartbeat, that is, a heartbeat will be sent every tickTime millisecond. The above settings are 2000 milliseconds.
InitLimit: used to set the maximum heartbeat time ("Set Value" * tickTime) of the Follower server connected to the Leader in the ZooKeeper server cluster. After this time is exceeded, the Follower server in the ZooKeeper server cluster does not return any information, which indicates that the connection to the Follower server fails. The above settings are (10*2000) milliseconds. This attribute is used when ZooKeeper is in cluster mode or pseudo cluster mode.
SyncLimit: used to set the maximum length of time for the request and response between the Leader server and the Follower server in the ZooKeeper server cluster. The length of the time is ("Set Value" * tickTime) milliseconds. The above settings are (5*2000) milliseconds. This attribute is used when ZooKeeper is in cluster mode or pseudo cluster mode.
DataDir: directory in which ZooKeeper saves data. By default, ZooKeeper also stores log files that write data in this directory. Note: The directory cannot be/tmp
ClientPort: the port on which the client connects to the ZooKeeper server. Zookeeper listens to this port and uses this port to accept client access requests.

E. Create a new name named "zoo. cfg file (ZooKeeper will find the file named "zoo. cfg file and use it as the default configuration file), open it in notepad, copy the content in the original file named "zoo_sample.cfg" to the new "zoo. cfg file and make necessary modifications, such:

# The number of milliseconds of each ticktickTime=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=C:/zookeeper-3.4.5/data# the port at which the clients will connectclientPort=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

So far, ZooKeeper has been installed and configured in the Windows operating system, but it must be noted that ZooKeeper is written in Java. Therefore, you must install the Java environment before running ZooKeeper-configure JDK, the JDK version must be later than or equal to 1.6.

Ii. Start ZooKeeper

Open the "bin" folder shown in the first image above:


A. Start the ZooKeeper server: double-click the file named "zkServer. cmd" in the Windows operating system and run the file named "zkServer. sh" in the Linux operating system;

B. Start the ZooKeeper client. in the Windows operating system, double-click the file "zkCli. cmd" and run the file "zkCli. sh" with the command in the Linux operating system;

Note: The preceding two steps cannot be reversed; otherwise, the ZooKeeper client cannot be started successfully;

3. Use ZooKeeper

After you double-click the "zkServer. cmd" and "zkCli. cmd" files in a Windows operating system, two cmd Windows are displayed. Do not close them. Otherwise, the following program cannot run:

Package com. ghj. packageoftest; import org. apache. zookeeper. createMode; import org. apache. zookeeper. watchedEvent; import org. apache. zookeeper. watcher; import org. apache. zookeeper. zooDefs. ids; import org. apache. zookeeper. zooKeeper; public class ZooKeeperClient {public static void main (String [] args) throws Exception {Watcher watcher = new Watcher () {// monitor all triggered events public void process (WatchedEvent event ){ System. out. println ("triggered" + event. getType () + "event! ") ;}}; ZooKeeper zooKeeper = new ZooKeeper (" 127.0.0.1: 2181 ", 5000, watcher); // The first parameter is the connection address of the ZooKeeper server, if ZooKeeper is in cluster mode or pseudo cluster mode (that is, there are multiple ZooKeeper servers), separate each connection address with a comma. The syntax format of a single connection address is "Host IP: zooKeeper server port number "; // second parameter: session Timeout duration (unit: milliseconds) // third parameter: the Watcher object zooKeeper is used to monitor directory node data changes and subdirectory status changes. create ("/RootNode", "RootNodeData ". getBytes (), Ids. OPEN_ACL_UNSAFE, CreateMode. PERSISTENT); // create a directory node named "/RootNode. out. println (""/RootNode "node status:" + zooKeeper. exists ("/RootNode", true); // determines whether the specified directory node exists in System. out. println ("" RootNode "node data:" + new String (zooKeeper. getData ("/RootNode", false, null); // obtain the data zooKeeper on the "RootNode" node. create ("/RootNode/ChildNode1", "ChildNode1Data ". getBytes (), Ids. OPEN_ACL_UNSAFE, CreateMode. PERSISTENT); // create a sub-directory node named "ChildNode1" under the "RootNode" node, zooKeeper. create ("/RootNode/ChildNode2", "ChildNode2Data ". getBytes (), Ids. OPEN_ACL_UNSAFE, CreateMode. PERSISTENT); // create a subdirectory named "ChildNode2" at the same level as "ChildNode1" under the "RootNode" node. out. println ("all sub-directory nodes under the directory node" RootNode "include:" + zooKeeper. getChildren ("/RootNode", true); // retrieves zooKeeper from all sub-directories under the "RootNode" directory node. setData ("/RootNode/ChildNode2", "NewChildNode2Data ". getBytes (),-1); // modify the directory node data zooKeeper named "ChildNode2. delete ("/RootNode/ChildNode1",-1); // delete the "/RootNode/ChildNode1" directory node System. out. println (""/RootNode/ChildNode1 "node status:" + zooKeeper. exists ("/RootNode/ChildNode1", false); // checks whether the "/RootNode/ChildNode1" directory node has zooKeeper. delete ("/RootNode/ChildNode2",-1); // delete the "/RootNode/ChildNode2" directory node zooKeeper. delete ("/RootNode",-1); // delete the "/RootNode" directory node zooKeeper. close (); // close the connection with ZooKeeper }}

Sample Code downloaded]

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.