Windows install and use zookeeper (ZooKeeperNet)

Before finishing an article " ZooKeeper distributed lock service ", Zookeeper described in this article is based on the 3.4.5 stable release, based on the latest version through the official website http://hadoop.apache.org/zookeeper/ to get, Zookeeper installation is very simple, from the following two stand-alone mode and trunked mode introduces Windows installation and configuration of Zookeeper.

First you need to install JdK, downloaded from Oracle's Java Web site, the installation is very simple, not elaborate.

Stand-alone mode

Stand-alone installation is very simple, just get to the Zookeeper archive and extract it to a directory such as: C: \ zookeeper-3.4.5 \ under, Zookeeper startup script in the bin directory, startup scripts under Windows is zkServer.cmd.

Before you run the startup script, there are a few basic configuration items need to configure, Zookeeper configuration files in the conf directory, there are zoo_sample.cfg and log4j.properties this directory, you need to do it is to be renamed zoo_sample.cfg as zoo.cfg, because Zookeeper at startup will find this file as the default profile. The following details about the significance of the various configuration items in the configuration file.

# The number of milliseconds of each tick
tickTime=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
dataLogDir=C:\\zookeeper-3.4.5\\log
# the port at which the clients will connect
clientPort=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
  • tickTime: This is the time between Zookeeper as a server or a client and a server to maintain the heartbeat intervals, that is, each time tickTime will send a heartbeat.
  • dataDir: Zookeeper name suggests is to save the data directory, by default, the log file Zookeeper write data is also stored in this directory.
  • dataLogDir: As the name suggests is the directory to save the log file Zookeeper
  • clientPort: This port is the port the client server connection Zookeeper, Zookeeper listens to this port, interview the client's request.

When these configuration items is configured, you can now start Zookeeper, and after starting to check whether Zookeeper already in service, by netstat - ano command to see if there clientPort port number you configured in the listening service.

image

Cluster Mode

Zookeeper not only stand-alone service, but also supports multi-machine clustered to provide services. Indeed Zookeeper otherwise supports Pseudo cluster, i.e. Zookeeper can run multiple instances on a single physical machine, will be described below configuration and installation of trunked mode.

Zookeeper mode cluster installation and configuration is not very complicated to do is increase the number of configuration items. In addition to the above three cluster model configuration items also increase the number of configuration items below:

initLimit=5 
syncLimit=2 
server.1=192.168.211.1:2888:3888 
server.2=192.168.211.2:2888:3888

    • How much time can stand up to this configuration item is used to configure Zookeeper accept client (here the client is not connected to the user client server Zookeeper, Zookeeper but a cluster of servers connected to the Leader of the Follower server) initialize the connection: initLimit heartbeat number of intervals. When you have more than 10 heartbeat time (that is, tickTime) the length Zookeeper server has not received client-side return information, it indicates that the client connection fails. The total length of time is 2000 * 5 = 10 seconds
    • syncLimit: sending messages between the configuration item identifier Leader and Follower, request and response time length, the length of the longest number tickTime not exceed, the total length of time is 2 * 2000 = 4 seconds
    • server.A = B: C: D: wherein A is a number, indicates that the server is the first few numbers; B is ip address of the server; C represents the port of the server in the cluster Leader exchanging information server; D It represents the case in the cluster Leader server being down, needs a port to re-election to elect a new Leader, and this port is the port used to perform mutual communication server election. If a pseudo-cluster configuration, since B is the same, so that different instances Zookeeper communication port number not be the same, so that they give different port numbers assigned.
    • In addition to changing zoo.cfg configuration file, also configure a clustered mode file myid, dataDir this file in the directory, which will have a data file that is the value of A, will read this file when Zookeeper start, get inside zoo.cfg data configuration information which in the end is to determine the comparative server.

Data Model

Zookeeper maintains a data structure having a hierarchical relationship, it is very similar to a standard file system, shown in Figure 1:

    • zookeeper

This data structure has the following Zookeeper these characteristics:

    1. Each subdirectory 00216 NameService are referred to as znode, this znode is in the path of its unique identification, such as this znode identified as Server1 / NameService / Server1
    2. znode directory can have child nodes, and each znode can store data, the type of attention EPHEMERAL directory node directory can not have child nodes
    3. znode is version, the data stored in each znode can have multiple versions, i.e. a path to access data can be stored in multiple parts
    4. znode node may be temporary, once created this znode the client and the server lost contact, this znode will be automatically deleted, the client and server communications using long Zookeeper connections, each client and server to keep the connection heartbeat, this called session connection status, if znode temporary node, this session failed, znode also deleted
    5. znode directory names can be automatically numbered, as App1 already exist, then create, it will automatically be named App2
    6. znode can be monitored, including modifications, variations and other data stored in this directory node child node directory, once the change notification may be provided to monitor the client, this is a core feature of Zookeeper, Zookeeper many features are based on this characteristic to achieve , followed by typical examples of application scenarios will be described

how to use

Zookeeper as a distributed service framework, mainly used to solve the consistency problem of distributed application systems in the cluster, which provides data storage node-based directory tree way similar to a file system, but not one designed to store data Zookeeper it's main function is to maintain and monitor the status of your stored data changes. By monitoring changes in the state of the data, which can achieve based cluster management data.

By using the C # code zookeeper

Zookeeper used mainly performed by creating examples thereof Zookeeper Nuget ZooKeeperNet the packet and calls the interface method thereof, the main operation is the operation of znode of additions and deletions, as well as monitor changes znode process.

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); } } } }
the System the using;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text; the using ZooKeeperNet; namespace ZookeeperDemo { class Program { static void the Main ( String [] args) { // create a Zookeeper instance, the first parameter destination server address and port, the second parameter is the Session timeout callback method when the third node changes the using (ZK = the ZooKeeper new new the ZooKeeper ( "127.0.0.1:2181", new new the TimeSpan (0, 0, 0 , 50000), new new Watcher ())) {var = zk.Exists STAT ( "/ the root", to true); //// Create the root node, the data is mydata, no ACL right control node is permanent (ie, client shutdown, it will not disappear) //zk.Create("/root "," mydata ".GetBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

// Create the root following a childone znode, data childone, not ACL access control, node permanent zk.Create ( "/ root / childone", "childone" .GetBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode. Persistent); // child node name in the acquired / root node, return List <String> zk.GetChildren ( "/ root", to true); // data acquired under the / root / childone node, returns byte [] zk. the GetData ( "/ the root / childone", to true, null); data under // modify node / root / childone, third parameter version, if it is -1, it will ignore the modified version of the data, to get rid of direct zk.SetData ( "/ root / childone", "childonemodify" .GetBytes (), -1); // delete / root / childone this node, the second parameter version, then delete -1, ignoring version zk. the Delete ( "/ the root / childone", -1);}}}}

 

Analysis

Create a connection:

1. Obtain Service Host List

2. Set timeout

3. Register client event

4. Create a thread-safe manner connection request (start the client request queue, circular queue socket based communication, perform different actions according to the request type of request)

Request process:

Configuration request header configuration request, reponse, in response to the first configuration, configured Packet objects, packet ready well after the object, the entire object into a outgoingQueue
packet outgoingQueue is placed in the transmission packet corresponding to the wait SendThread content to the server. Dividing server 3 in step ReadLength ReadConnectResult ReadResponse doio process until ReadResponse method to determine the end request packet.

Response process:

For heartbeat ping request resp, for the auth request resp, resp generic interface request, if the interface request requires a watcher, when the content watcher concerned there is a change notification

Lock relevant parts of the API methods:

Create a node: create

demo:zk.Create(Dir, severname.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

Wherein CreateMode divided into four categories Persistent, PersistentSequential, Ephemeral, EphemeralSequential

PERSISTENT in a persistent node, the node corresponding to the machine off the connection / data does not disappear

If PATH PERSISTENT_SEQUENTIAL ends in '/' as a parent node places the PATH, to create a child node, the child node name which is arranged in chronological order according to a value; otherwise, create a name '/' characters plus behind chronological order the value of the string node, create the same lasting node

EPHEMERAL create transient nodes, Zookeeper perception After connecting the machine downtime clears it creates instantaneous node

The instantaneous wear member EPHEMERAL_SEQUENTIAL order of nodes, and PERSISTENT_SEQUENTIAL same, except that it is instantaneous

Delete nodes delete

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

Before the name of a parameter representative of the node (typically used as a path), the version number is -1 indicates a match-

View node exists

demo : zk.Exists(Dir, new MyWatch2());

Get Data getData

demo :zk.GetData(Dir, new MyWatch2(), stat);

Get a node of the data, may be injected watcher 

Set data setData

demo : zk.SetData(Dir, new byte[1], 1);

Get lower node set GetChildren

demo :zk.GetChildren(Dir, true);

storage

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

watcher

Zookeeper two watches, one is the data watches, the other is a child watches. Wherein, getData () and exists () and create () adds the like data watches, getChildren () adds child watches. And delete () and delete the data related to child nodes, and will also trigger data watches child watches.

 Transfer: https: //www.cnblogs.com/shanyou/p/3221990.html

Guess you like

Origin www.cnblogs.com/cuihongyu3503319/p/12532220.html