Installation and use of substantially Zookeeper

First, deployment and operation

1.1 System Environment

For most Java open-source products, prior to deployment and operation, always we need to build a suitable environment typically includes an operating system and Java environment in two ways. This section describes the deployment and operating system environment ZooKeeper needs, also including the operating system and Java environment in two parts.

1.1.1 Operating System

First, you need to choose - a suitable operating system. Fortunately, ZooKeeper support for different platforms are good, are now able to run properly on most major operating systems such as GNU / Linux, SunSolaris, Win32 and MacOSX and so on.

1.1.2 Java environment

ZooKeeper is written in Java, so it supports operating environment requires Java environment, you can download 1.6 or
above version of Java

Second, stand-alone and cluster model

2.1 stand-alone mode

2.1.1 download and configure

Access Zookeeper Download downloaded after extracting archive. The official stable version recommended downloading.

Modify confdirectory under zoo_sample.cfg=> zoo.cfg. Note: You must modify, Zookeeper read default configuration file called the latter.

Open zoo.cfg, modifications dataDir configured to create their own directory.

dataDir= /Users/pengweiwei/Downloads/zookeeper-3.4.14/data

2.1.2 running on and off

Into the bin directory. Or with environmental variables.
run:./zkServer.sh start

weiweideMacBook-Pro:bin pengweiwei$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/pengweiwei/Downloads/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

shut down:./zkServer.sh stop

2.2 Cluster-Mode

2.2.1 Configuration

Each machine in the cluster configuration file zoo.cfg have joined configuration. Because in a clustered mode, each machine in the cluster needs perceived by the entire cluster is composed of several machines which, in the format:server.id=host:port:port

server.1=192.168.1.1:2888:3888
server.2=192.168.1.2:2888:3888
server.3=192.168.1.3:2888:3888

Wherein, id referred ServerID, the machine to identify the machine serial number in the cluster. Meanwhile, ZooKeeper on each machine, we need to create a myid file in the data directory (ie dataDir parameter specifies the directory), the file is only one line content, and is a number, and is the first line, that corresponds to ServerID numbers of each machine.

In ZooKeeper design, content in a cluster zoo.cfg files on all machines should be consistent. So it is best to use SVN or GIT to manage this file, to ensure that each machine can share a same configuration.

Myid also mentioned above, only a digital file that a Server ID. For example, server.1 of myid contents of the file is "1." Note, make sure myid digital file for each server in a different, and where their machines zoo.cfg and the server id = host:. Port: port of the same id value. Further, id the range of 1 to 255.

Follow the same steps as the other machines are configured and zoo.cfg myid file.

Then one by one were to start. carry out.

Three, Zookeeper use

First use ./zkCli.shthe connection at the client, the client connection to specify additional parameters can be clustered./zkCli.sh -server ip:port

3.1 Create

Use the create command, you can create a ZooKeeper node. Usage is as follows:

create [-s] [-e] path data acl

Wherein, -s or -e specified node characteristics are: nodes sequentially or temporary. By default, that is, without adding -s or -e parameter to create a lasting node.
Execute the following command:

[zk: localhost:2181(CONNECTED) 11] create -e /tmp xiaoming                                   
Created /tmp

After executing the above command, you create a called / tmp ZooKeeper nodes under the root node, and the data content of the node is "xiaoming". In addition, the last parameter create command is acl, which is used for access control, by default, without any access control. About Acl can view the
system ZooKeeper model (data model, node characteristics, version, Watcher, ACL) understand.

3.2 reading

Associated with the read command includes the ls command and the set command.
Use the ls command to list all sub-nodes under the ZooKeeper specified node. Of course, this command can see all child nodes of the specified node Dir level. Usage is as follows:

ls path [watch]

Wherein, path represents the data nodes specified node path.
Execute the following command:

[zk: localhost : 2181(CONNECTED) 0] ls /
[zookeeper]

The first deployment of ZooKeeper cluster, the default root node "/" Now there is a called / zookeeper retention node.

Use get command, you can get the data content and attribute information ZooKeeper specified node. Usage is as follows:

get path [watch]

Execute the following command:

[zk: localhost:2181(CONNECTED) 12] get /tmp
xiaoming
cZxid = 0x6
ctime = Sun Jan 26 20:12:04 CST 2020
mZxid = 0x6
mtime = Sun Jan 26 20:12:04 CST 2020
pZxid = 0x6
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000826c70a0001
dataLength = 8
numChildren = 0

3.3 update

Use the set command, you can update the data contents of the specified node. Usage is as follows:

set path data [version]

Which, data is new content to be updated. Note that, there is a later version SET command parameter in ZooKeeper, the data node is the version of the concept, this parameter is used to specify this update operation is based on which data version ZNode performed.
Execute the following command:

[zk: localhost:2181(CONNECTED) 13] set /tmp xiaoli
cZxid = 0x6
ctime = Sun Jan 26 20:12:04 CST 2020
mZxid = 0x7
mtime = Sun Jan 26 20:16:39 CST 2020
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x1000826c70a0001
dataLength = 6
numChildren = 0

After performing the above command, the contents of the node data of / tmp has been updated to the xiaoli. In the output, the value of the original DataVersion 0 becomes 1, this is just because the update version of the data leads to the occurrence node has changed.

3.4 Delete

Use the delete command, you can delete the specified node on the ZooKeeepr. Usage is as follows:

delete path [version]

The effect of this command version and set parameters in the command parameter is the same version.
Execute the following command:

[zk: localhost:2181(CONNECTED) 14] delete /tmp
[zk: localhost:2181(CONNECTED) 15] ls /
[zookeeper, user]

After executing the above command, you can put / zk-book this node successfully deleted. But the point to note here is that in order to remove a specified node, the node must exist no child nodes.

Published 46 original articles · won praise 10 · views 4310

Guess you like

Origin blog.csdn.net/weiwei_six/article/details/104088991