Windows installation and use Zookeeper

Source: Internet
Author: User
Tags zookeeper client

This article describes the Zookeeper is based on the stable version of 3.4.5, the latest version can be obtained through the official website http://hadoop.apache.org/zookeeper/, Zookeeper installation is very simple, The following describes the Windows installation and configuration of Zookeeper from two aspects of stand-alone mode and cluster mode.

csdn:http://download.csdn.net/detail/javadxz/7484051

The first thing you need to do is install the JDK, download it from Oracle's Java Web site, and install it very simply, no details.

Stand-alone mode

Single-machine installation is very simple, as long as the Zookeeper to obtain a compressed package and extract to a directory such as: C:\zookeeper-3.4.5\, Zookeeper startup script under the Bin directory, Windows startup script is Zkserver.cmd in the bin directory.

Before you execute the startup script, there are a few basic configuration items that need to be configured, Zookeeper configuration files in the Conf directory, this directory has zoo_sample.cfg and log4j.properties, what you need to do is Zoo_ Sample.cfg renamed Zoo.cfg because Zookeeper will find this file as the default profile at startup. The meanings of each configuration item in this configuration file are described in detail below.

# 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.# does not use/tmp for storage,/tmp Here is just # example Sakes.datadir=c:\\zookeeper-3.4.5\\datadataLogDir=c:\\zookeeper-3.4.5\\log# the port at which the clients would connectclientport=2181# # is sure to read the maintenance sections of the # Administrator guide before turning on autopurge.## Http://zookeepe r.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
    • 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.

When these configuration items are configured, you can start Zookeeper now, after starting to check whether Zookeeper is already in service, you can check with the Netstat–ano command to see if there is a ClientPort port number you configured in the Listening service.

Cluster mode

Zookeeper can not only provide services, but also support multi-machine cluster to provide services. In fact, Zookeeper also supports another way of pseudo-clustering, that is, you can run multiple Zookeeper instances on a single physical machine, and the following describes the installation and configuration of cluster mode.

The installation and configuration of the Zookeeper cluster mode is also not very complex, all you have to do is add a few configuration items. The cluster mode adds the following configuration items in addition to the above three configuration items:

Initlimit=5
synclimit=2
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 5*2000=10 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 2*2000=4 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 the server.

Data model

Zookeeper maintains a hierarchical relational data structure that is very similar to a standard file system, as shown in 1:

Zookeeper This data structure has the following features:

      1. Each subdirectory entry, such as Nameservice, is called Znode, and the Znode is uniquely identified by the path it is located in, such as Server1, which is the Znode identity/nameservice/server1
      2. Znode can have child node directories, and each znode can store data, note that ephemeral types of directory nodes cannot have child node directories
      3. Znode is a version of the data stored in each Znode can have multiple versions, that is, one access path can store multiple copies of data
      4. Znode can be a temporary node, once created this Znode client and the server lost contact, this znode will also be automatically deleted, Zookeeper client and server communication with a long connection, each client and server through the heartbeat to maintain connectivity, this connection status is called Session, if Znode is a temporary node, this session expires, Znode also deleted
      5. Znode directory name can be automatically numbered, such as App1 already exists, and then created, will be automatically named as App2
      6. Znode can be monitored, including changes in the data stored in this directory node, changes in sub-node directories, and so on, once the change can notify the settings monitoring client, this is the core feature of Zookeeper, Zookeeper Many of the functions are based on this feature is implemented, There will be examples in the following typical application scenarios

How to use

Zookeeper, as a distributed service framework, is mainly used to solve the consistency problem of application system in distributed cluster, it can provide data storage based on directory node tree like file system, but Zookeeper is not used to store data specifically. Its role is primarily to maintain and monitor the changes in the state of the data you store. Data-based cluster management can be achieved by monitoring changes in the state of these data.

Using Zookeeper with C # code

The use of zookeeper is mainly through the creation of its NuGet zookeepernet package under the Zookeeper instance, and call its interface method, the main operation is to znode the deletion and modification operation, monitoring znode changes and processing.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Zookeepernet; namespace zookeeperdemo{    class watcher:iwatcher    {public        void Process ( Watchedevent @event)        {            if @event. Type = = eventtype.nodedatachanged)            {                Console.WriteLine @Event. Path);}}}    
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingZookeepernet;namespacezookeeperdemo{classprogram {Static voidMain (string[] args) {//Create a zookeeper instance, the first parameter is the destination server address and port, the second parameter is the session time-out time, and the third is the callback method when the node changes            using(ZooKeeper ZK =NewZooKeeper ("127.0.0.1:2181",NewTimeSpan (0, 0, 0, 50000),NewWatcher ())) {var stat = ZK. Exists ("/root",true);////Create a node root, the data is MyData, no ACL permissions control, the node is permanent (that is, the client shutdown will not disappear)                //zk. Create ("/root", "MyData". GetBytes (), Ids.open_acl_unsafe, createmode.persistent);                //Create a childone znode under root, the data is childone without ACL permission control, the node is permanentZk. Create ("/root/childone","Childone". GetBytes (), Ids.open_acl_unsafe, createmode.persistent);//Gets the name of the child node under the/root node and returns list<string>Zk. GetChildren ("/root",true);//Get data under/root/childone node, return byte[]Zk. GetData ("/root/childone",true,NULL);//Modify the data under Node/root/childone, the third parameter is version, if it is-1, that will ignore the modified version of the data, directly changeZk. SetData ("/root/childone","Childonemodify". GetBytes (),-1);//Delete/root/childone This node, the second parameter is version, 1 words are deleted directly, ignoring versionZk. Delete ("/root/childone",-1); }        }    }}

Analysis

To create a connection:

1. Get the list of service hosts

2. Set the time-out period

3. Registering Client Events

4. Creating a request connection in a thread-safe manner (initiating a client request queue, looping the queue based on socket communication, performing different request actions based on the request type)

Request Flow:

Constructs the request head, constructs the request,reponse, constructs the response header, constructs the packet object, packet the object to be ready, puts the whole object into a outgoingqueue
Packet is put into the outgoingqueue, waiting for Sendthread to send the packet corresponding content to the server. Server processing has a 3-step readlength readconnectresult Readresponse in the Doio method until the Readresponse request is determined to end in the packet method.

Response Process:

The RESP of the ping request for the heartbeat, the RESP for the auth request, the RESP of the general interface request, if the interface request requires watcher, watcher when the content of notification attention changes

Lock-related part of the API method:

Creating a node: Create

Demo:zk. Create (Dir, Severname. GetBytes (), Ids.open_acl_unsafe, createmode.persistent);

Among them, Createmode is divided into 4 categories persistent, persistentsequential, ephemeral, ephemeralsequential

Persistent creating a persistent node, the node/data does not disappear after the machine closes the connection

Persistent_sequential If Path is a '/' ending with this path as the parent node, create a child node whose child node name is a sequential number; otherwise create a node whose name is '/' followed by a numeric string in the order of precedence, Also create persistent nodes

Ephemeral creates an instantaneous node, zookeeper clears the instantaneous node it creates when it senses a connected machine outage

Ephemeral_sequential the instantaneous sequence node, like the persistent_sequential, the difference is that it is instantaneous

Deleting a node delete

Demo:zk. Delete (Dir,-1);

The previous parameter represents the node name (typically used as a path), and the latter is the version number-1 for the full match

View node exists

Demo:zk. Exists (Dir, New MyWatch2 ());

Get Data GetData

Demo:zk. GetData (Dir, New MyWatch2 (), stat);

Gets the data for a node that can be injected into the watcher

Set Data SetData

Demo:zk. SetData (Dir, New byte[1], 1);

Gets the subordinate node collection GetChildren

Demo:zk. GetChildren (Dir, true);

Store

Znodes similar files and directories. But it is not a typical file system, zookeeper data is kept in memory, which means that zookeeper can achieve high throughput and low latency.

Watcher

Zookeeper there are two kinds of watches, one is data watches and the other is child watches. where GetData () and exists () and create () are added, data watches,getchildren () is added to child watches. Delete () involves the deletion of data and child nodes, which simultaneously triggers data watches and children watches.

Sample code Download: Zookeeperdemo.zip

Related Article

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.