The definition of Zookeeper can be explained in a word: the Distributed Service Framework Zookeeper-manages data in a distributed environment. The following is an analysis of the framework, starting with the installation.
1. Installation
1. Download the compressed package and extract it to D:\Program Files (x86) \zookeeper-3.4.12
2. Create a new data and log folder under the D:\Program Files (x86) \zookeeper-3.4.12 directory
3. Copy the Zoo_sample.cfg file in the Conf directory to the same directory, rename it to Zoo.cfg (Zookeeper will find this file as the default profile at startup), modify the path of the new directory above DataDir and Datalogdir
4. Start the test.
Service-side startup
After booting, port view can see that zookeeper started a Java service via port 2181
Start the client connection, success
2. Zookeeper configuration file
The contents of the current configuration file zoo.cfg are as follows
#The number of milliseconds of each tickticktime=2000#The number of ticks that 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.#Don't use/tmp for storage,/tmp here's just#example sakes.Datadir=d:\\program Files (x86) \\zookeeper-3.4.12\\data Datalogdir=d:\\program Files (x86) \\zookeeper-3.4.12\\log#The port at which the clients would connectclientport=2181#The maximum number of client connections.#increase this if you need to handle more clients#maxclientcnxns=60##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
In stand-alone mode, the main configuration item functions:
- Ticktime: This time is the time interval between the Zookeeper server or between the client and the server to maintain the heartbeat, that is, each ticktime time sends a heartbeat.
- DataDir: As the name implies is Zookeeper to save the data directory, by default, Zookeeper will write the data log file is also stored in this directory.
- Datalogdir: As the name implies Zookeeper the directory where the log files are saved
- ClientPort: This port is the port that the client connects to the Zookeeper server, Zookeeper listens to the port and accepts the client's access request.
3. Cluster mode
Zookeeper's installation mode is divided into three types, namely: Single machine mode (stand-alone), cluster mode and cluster pseudo-distribution mode. The single-machine mode is shown above, and the service is cluster mode through multiple machines, and a computer can actually perform pseudo-cluster mode, that is, running multiple Zookeeper instances on a physical machine.
The cluster mode is set by adding configuration items from the configuration file Zoo.cfg, with the following key configuration items:
initlimit=10 synclimit=5 server. 1=192.168.211.1:2888:3888 server. 2=192.168.211.2:2888:3888
- Initlimit: This configuration item is used to configure the Zookeeper accept client (the client here is not the client that connects the Zookeeper server, but the Leader that is connected to Follower in the Zookeeper server cluster) Server) The maximum number of heartbeat intervals that can be tolerated when a connection is initialized. The client connection failed when the Zookeeper server has not received the return information of the client after 10 heartbeats (that is, ticktime) length. The total length of time is 10*2000=20 seconds.
- Synclimit: This configuration item identifies the length of time that a message, request and response is sent between Leader and Follower, the maximum number of ticktime, and the total length of time is 5*2000=10 seconds
- Server. A=b:c:d: Where A is a number, indicating this is the first server, B is the IP address of this server, C is the server and the Leader server in the cluster to exchange information on the port; D means that in case the Leader server in the cluster hangs, a port is needed to re-elect , select a new Leader, which is the port that the server communicates with each other when the election is performed. If it is a pseudo-cluster configuration, because B is the same, so different Zookeeper instance communication port numbers can not be the same, so they should be assigned a different port number.
In addition to modifying the Zoo.cfg configuration file, in the cluster mode to configure a file myID, the file in the DataDir directory, the file contains a data is a value, Zookeeper startup will read this file, get the data inside and zoo.cfg The configuration information is compared to determine which server is in the end.
Single-machine pseudo-cluster mode configuration steps are as follows:
3.1 Configuration file Modification
Conf directory copy three times zoo.cfg to the same directory, respectively named Zoo1.cfg,zoo2.cfg,zoo3.cfg, the three files are modified as follows
Zoo1.cfg
Zoo2.cfg
Zoo3.cfg
Corresponding, the need to create a myID file under their respective datadir (no suffix prefix), the content is three-to-one, which is the number of servers.
3.2 Modifying the server-side boot configuration
The bin directory is copied three times zkserver.cmd to the same directory, respectively named Zkserver1.cmd,zkserver2.cmd,zkserver3.cmd, the three files are modified as follows
Zkserver1.cmd
Zkserver2.cmd
Zkserver3.cmd
3.3 Starting the Service
CMD start three zkserver, here I am open three cmd window start, order 1-2-3, three zkserver not full start will report the following error, this is zookeeper leader election algorithm exception information, when the node is not started to complete, Leader does not work properly, this error message can be ignored and is normal after other nodes have been started.
Three ports all start up
4. Java Connectivity Test
Import org.apache.zookeeper.WatchedEvent; Import Org.apache.zookeeper.Watcher; Public class Implements watcher{ publicvoid process (watchedevent arg0) { System.out.println ( "========================"); System.out.println ("path:" +Arg0.getpath ()); System.out.println ("type:" +Arg0.gettype ()); System.out.println ("state:" +arg0.getstate ());} }
When you create a zookeeper instance, if you have multiple connections, separate them with commas.
Importjava.io.IOException;ImportOrg.apache.zookeeper.CreateMode;Importorg.apache.zookeeper.KeeperException;Importorg.apache.zookeeper.ZooDefs;ImportOrg.apache.zookeeper.ZooKeeper;ImportOrg.apache.zookeeper.data.Stat;Importcom.zang.WatcherTest; Public classApp { Public Static voidMain (string[] args)throwsIOException, Keeperexception, interruptedexception {//Create a Zookeeper instance, the first parameter is the destination server address and port, the second parameter is the session time-out time, the third is the callback method when the node changesZooKeeper ZK =NewZooKeeper ("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 30000,Newwatchertest ()); String node= "/node2"; Stat Stat= zk.exists (node,false); if(NULL==stat) { //Create a node, the data is test, do not control ACL permissions, the node is a permanentString Createresult = zk.create (node, "test". GetBytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE, createmode.persistent); System.out.println (Createresult); } //get the data under the/node2/test node and return to byte[] byte[] B = zk.getdata (node,false, stat); System.out.println (NewString (b)); Zk.close (); }}
Ref: 79192819
https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/
Zookeeper Installation and configuration (under Windows system)