Windows installation and use Zookeeper

Source: Internet
Author: User
Tags zookeeper zookeeper client

To get, Zookeeper installation is very simple, the following will be from the single-machine mode and cluster mode two aspects of Zookeeper Windows installation and configuration.

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.

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.

# 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 directorywhereThe snapshot isstored.# DoNot use/tmp forStorage,/tmp here isJust # example sakes.datadir=c:\\zookeeper-3.4.5\\datadatalogdir=c:\\zookeeper-3.4.5\\log# the port at which the Clients'll connectclientport=2181## be sure to read the maintenance sections of the the # Administrator guide before turning On autopurge.## http://zookeeper.apache.org/doc/current/zookeeperadmin.html#sc_maintenance# # of snapshots to retaininchdatadir#autopurge.snapretaincount=3# Purge Task Intervalinchhours# 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 Zookeeper accepts the client (the client here is not the client that connects the Zookeeper server, but the Follower server that is connected to Leader in the Zookeeper server cluster) The maximum number of heartbeat intervals that can be tolerated when the 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 messages, requests, and responses that are sent between Leader and Follower, The maximum length of time that can not exceed how many ticktime, 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 also configure a file myID, this file in the DataDir directory, the file has a data is a value, Zookeeper will read this file when it starts, and get the data inside to compare with the configuration information inside the ZOO.CFG 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, This znode is uniquely identified by the path to which it is located, such as Server1, the Znode is identified as/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 versioned, each znode The data stored in can have multiple versions, that is, one access path can store multiple copies of the 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 using 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 fails, Znode also deleted the /li>
      5. znode the directory name can be automatically numbered, such as APP1 already exist, and then created, will be automatically named 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, In a typical scenario, the following example introduces

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.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingZookeepernet;namespacezookeeperdemo{classWatcher:iwatcher { Public voidProcess (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.

Windows installation and use zookeeper

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.