Zookeeper installation, configuration, start-up and use (i)-standalone mode

Source: Internet
Author: User
Tags zookeeper client server port

Zookeeper installation is very simple, its operating mode is divided into single-machine mode, cluster mode and pseudo-cluster mode, this blog is designed to summarize the zookeeper stand-alone mode how to install, configure, start and use:

First, install the configuration zookeeper (under the Windows operating system)

A, download zookeeper compressed installation files, here to download stable version--zookeeper-3.4.5.tar.gz

B, unzip the compressed file, here unzip it to the C packing directory, open the Unpacked folder, get:


C, click on the folder named "Conf", you can see:


d, open a file named "Zoo_sample.cfg" with Notepad, you can see the following:

# The number of milliseconds of each tickticktime=2000# the number of ticks, the initial # synchronization phase can T akeinitlimit=10# the number of ticks so can pass between # Sending a request and getting an acknowledgementsynclimit=5# The directory where the snapshot is stored.# does not use/tmp for storage,/tmp here is just # example sakes.datadir=/tmp/z ookeeper# the port at which the clients would connectclientport=2181## be sure to read the maintenance section of the the # ADM Inistrator 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
Here are the necessary explanations for the above parameters:
Ticktime: A heartbeat that is used to define the heartbeat between the zookeeper servers or between the client and the server, that is, every ticktime milliseconds. The above setting is 2000 milliseconds.
Initlimit: Used to set the heartbeat of the follower server that is connected to leader in the Zookeeper server cluster, which can accept ("setpoint" *ticktime) milliseconds for the longest time. After that time, the follower server in the Zookeeper server cluster has not returned information, indicating that the follower server connection failed. The above setting is (10*2000) milliseconds. This property is for parameters that are used when zookeeper is in cluster mode or pseudo-cluster mode
Synclimit: Used to set the maximum time for requests and responses to send messages between leader servers and follower servers in a zookeeper server cluster with a length of ("SetPoint" *ticktime) milliseconds. The above setting is (5*2000) milliseconds. This property is for parameters that are used when zookeeper is in cluster mode or pseudo-cluster mode
Datadir:zookeeper the directory where the data is stored, by default zookeeper log files that write the data are also stored in this directory. Note: This directory cannot be/tmp
ClientPort: The client connects to the port of the zookeeper server, zookeeper listens on the port and accepts client access requests through that port

E. Create a new file named "zoo.cfg" in the Conf folder (zookeeper will find a file named "Zoo.cfg" at startup and use it as the default profile ), and open it with Notepad, with the original name "Zoo_ Sample.cfg "to the new" zoo.cfg "file and make the necessary modifications, such as:

# The number of milliseconds of each tickticktime=2000# the number of ticks, the initial # synchronization phase can T ake# initlimit=10# The number of ticks so can pass between # Sending a request and getting an acknowledgement# synclimit =5# the directory where the snapshot is stored.# does not use/tmp for storage,/tmp here is just # example sakes.datadir=c:  /zookeeper-3.4.5/data# the port at which the clients would 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 # # 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

Now that zookeeper is installed in the Windows operating system, it is important to note that zookeeper is written in Java, so it is necessary to install the Java environment before running zookeeper-Configure the JDK, and the JDK version is greater than or equal to 1.6.

Second, start zookeeper

Open the folder named "Bin" shown in the first picture above to get:


A, start the zookeeper server side: Double-click on the Windows operating system named "Zkserver.cmd" file, in the Linux operating system using the command to run the file named "zkserver.sh";

B. Start the Zookeeper client: double-click the file named "Zkcli.cmd" in the Windows operating system and use the command in the Linux operating system to run the file named "zkcli.sh";

Note: The above two steps cannot be reversed, otherwise the zookeeper client cannot start successfully;

Third, the use of zookeeper

When you double-click the "Zkserver.cmd" file and the "Zkcli.cmd" file in the Windows operating system, there will be two cmd windows, do not close yo, otherwise the following program will not 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 ;p Ublic class Zookeeperclient {public static void main (string[] args) throws exception{Watcher Watcher = new W Atcher () {///monitor all triggered events public void process (Watchedevent event) {System.out.println ("trigger The "+ event.gettype () +" Event!             ");        }        }; ZooKeeper ZooKeeper = new ZooKeeper ("127.0.0.1:2181", "Watcher");//First parameter: The connection address of the ZooKeeper server,                                                                             If zookeeper is a cluster mode or pseudo-cluster mode (that is, there are multiple zookeeper servers), the comma interval between each connection address is used, and the syntax format for a single connection address is "host Ip:zookeeper server port number";                                                                             Second parameter: Session timeout length (in milliseconds) The third parameter: The Watcher object Zookeeper.create ("/rootnode", "Rootnodedata") used to monitor the changes in the directory node data and the status of subdirectories. GetBytes (), Ids.open_acl_unsafe, createmode.persistent);//Create a directory node named "/rootnode" for Node System.out.println ("/rootnode" Node Status: "+ zo Okeeper.exists ("/rootnode", true));//Determines whether the specified directory node exists System.out.println (data on the "RootNode" node: "+new String ( Zookeeper.getdata ("/rootnode", false, null)));//Gets the data zookeeper.create on the "RootNode" node ("/rootnode/childnode1", "Child        Node1data ". 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 node named "ChildNode2" with the "ChildNode1" sibling under the "RootNode" Node System.out.println (all sub-directory nodes under directory node RootNode are: "+ Zookeeper.getchildren ("/rootnode", true)); Remove all subdirectory nodes under directory Node "RootNode" Zookeeper.setdata ("/rootnode/childnode2", "Newchildnode2data". GetBytes (),-1);//Modify the name " ChildNode2 "Directory node data zookeeper.delete ("/rootnode/childnode1 ",-1);//delete"/rootnode/childnode1 "Directory node SYSTEM.OUT.PR Intln ("/rootnode/childnode1" node-like"+ zookeeper.exists ("/rootnode/childnode1 ", false));//Determine if the"/rootnode/childnode1 "Directory node exists zookeeper.delete ("/rootn Ode/childnode2 ",-1);//delete"/rootnode/childnode2 "Directory Node Zookeeper.delete ("/rootnode ",-1);//delete"/rootnode "Directory node Zo Okeeper.close (); Close the connection to the zookeeper}}

0 min Download Sample code

Zookeeper installation, configuration, start-up and use (i)-standalone mode

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.