Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to Zookeeper

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Zookeeper is a distributed coordination system.

Download link: mirror.cogentco.com/pub/apache/zookeeper/

1. Zookeeper Cluster Structure

(1) leader

It is the master node of the zk cluster. When a client registers data with zk, it must synchronize the data of all slave nodes in the cluster through the leader.

(2)follower

It is the slave node of zk cluster, keeps data, receives leader's request, participates in leader's election

(3)observer

It is a slave node of zk cluster, keeps data, receives leader's request, and does not participate in leader election.

2. Zookeeper's election mechanism

(1)When the cluster starts,

Server1 starts, first check whether there is a leader in the cluster, if not, elect yourself as the leader;

Server2 starts, check whether there is a leader in the cluster, if not, elect yourself as the leader,

In the second round of voting, server1j and server2 voted for the leader with the larger id, and server2 was elected as the leader.

(2)When the cluster is running

During operation, if the leader goes down, the remaining machines will enter the election state. The basis for re-election:

The version number of the data held by the node is given priority, and the latest one is taken as the leader;

If the data of each node is equally new, then the one with the largest id is elected as the leader;

3. mounting configuration

Unpack installation

[root@Darren2 zookeeper-3.4.10]# tar -zxvf zookeeper-3.4.10.tar.gz

[root@Darren2 zookeeper-3.4.10]# mkdir -p /usr/local/zookeeper-3.4.10/data

[root@Darren2 zookeeper-3.4.10]# echo 1 > data/myid

modify the configuration file

The 1 in server.1 corresponds to the myid file content, and each zookeeper must have a myid file.

[root@Darren2 zookeeper-3.4.10]# cd /usr/local/zookeeper-3.4.10/conf/

[root@Darren2 conf]# cp zoo_sample.cfg zoo.cfg

[root@Darren2 conf]# vim zoo.cfg

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/usr/local/zookeeper-3.4.10/data

clientPort=2181

server.1=Darren2:2888:3888

server.2=Darren3:2888:3888

server.3=Darren4:2888:3888

#Start zookeeper

[root@Darren2 bin]# ./ zkServer.sh start

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper-3.4.10/bin/../ conf/zoo.cfg

Starting zookeeper ... STARTED

[root@Darren2 bin]# ./ zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper-3.4.10/bin/../ conf/zoo.cfg

Mode: Leader

[root@Darren3 bin]# ./ zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper-3.4.10/bin/../ conf/zoo.cfg

Mode: Follower

4. zookeeper command

The essence of data in zookeeper is key-value. A piece of data in zookeeper is called a znode. A znode cannot be too large, usually within 10K. The official requirement is that the maximum is not more than 1M. If it is too large, it will cause the data of zookeeper cluster nodes to be unable to synchronize in real time and maintain data consistency.

key is represented by a path, such as: /dir1 value

znode type

(1)persistent: The default node type that exists once created, unless the data is manually deleted;

(2)ephemeral: transient node. If the client creating this data node loses contact with zk service, this data node will be automatically deleted by zk service;

(3)sequential: nodes with self-increasing sequence numbers, create sequential child nodes under the same node, zk will automatically concatenate a self-increasing sequence number for the child node name;

#Login to zk client

[root@Darren2 bin]# ./ zkCli.sh

[zk: localhost:2181(CONNECTED) 0] help

ZooKeeper -server host:port cmd args

stat path [watch]

set path data [version]

ls path [watch]

delquota [-n|-b] path

ls2 path [watch]

setAcl path acl

setquota -n|-b val path

history

redo cmdno

printwatches on|off

delete path [version]

sync path

listquota path

rmr path

get path [watch]

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

addauth scheme auth

quit

getAcl path

close

connect host:port

[zk: localhost:2181(CONNECTED) 13] create /dir1 a1

[zk: localhost:2181(CONNECTED) 16] ls /

[dir1, zookeeper]

[zk: localhost:2181(CONNECTED) 15] get /dir1

a1

cZxid = 0x7

ctime = Sun Nov 26 13:05:34 CST 2017

mZxid = 0x7

mtime = Sun Nov 26 13:05:34 CST 2017

pZxid = 0x7

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 2

numChildren = 0

#Create transient znodes, automatically deleted once the client quits

[zk: localhost:2181(CONNECTED) 17] create -e /dir2 b1

#Create sequence znode

[zk: localhost:2181(CONNECTED) 1] create -s /dir3 c1

Created /dir30000000003

[zk: localhost:2181(CONNECTED) 2] ls /

[dir1, zookeeper, dir30000000003]

[zk: localhost:2181(CONNECTED) 3] create -s /dir3 c1

Created /dir30000000004

[zk: localhost:2181(CONNECTED) 4] ls /

[dir1, zookeeper, dir30000000003, dir30000000004]

#Create transient sequence znode

[zk: localhost:2181(CONNECTED) 5] create -s -e /dir3 c1

#Event monitoring

[zk: localhost:2181(CONNECTED) 4] ls /dir1 watch

WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/dir1

5. Basic use of zookeeper client api

package zkdemo1;

import org.apache.zookeeper.ZooKeeper;

public class TestConnection {

public static void main(String[] args) throws Exception{

ZooKeeper zk = new ZooKeeper("192.168.163.102", 2000, null);

byte[] data = zk.getData("/dir1", false, null);

System.out.println(new String(data));

zk.close();

}

}

Output:

a1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report