Zookeeper In Action

Preview:

DESCRIPTION

Getting started with Apache ZooKeeper

Citation preview

ZooKeeper In ActionJuven Xu (许晓斌)

• Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly reliable distributed coordination.

Requirements

• JDK 1.6 & JAVA_HOME

• Maven (we will need to write some java code)

Installation

• Download: http://zookeeper.apache.org/releases.html

• mv conf/zoo_sample.cfg conf/zoo.cfg

• edit zoo.cfg: dataDir=/my/zk/dataDir

• bin/zkServer.sh start

ZooKeeper Data Tree

ZooKeeper API

bin/zkCli.sh

• zkCli.sh -server localhost:2181

Exercise #1

• install zookeeper, start it, and create a znode /test/node1 with value “hello-world”

Problem with Polling

ZooKeeper Watch

Coding for Watch

Coding for Watch

Exercise #2

• Write your own watch, print new value on each time znode /test/znode1 changes

ZooKeeper Ensemble

To Setup an Ensemble• conf/zoo.cfg: //on all servers

server.1=10.20.157.1:2888:3888 server.2=10.20.157.2:2888:3888 server.3=10.20.157.3:2888:3888

• conf/zoo.cfg: //this location is important dataDir=/home/admin/zookeeper/zkData

• echo 1 > ${dataDir}/myid //on server.1echo 2 > ${dataDir}/myid //on server.2echo 3 > ${dataDir}/myid //on server.3

To Setup an Ensemble

• If you want to set up ensemble on one machine, make sure these values are different:

• dataDir

• clientPort

• the ports of server.{1-5}

Client of a Ensemble

Exercise #3• Find 2 friends, set up an Ensemble with them.

• Update your client, registering all the 3 servers.

• Kill one server, see if ensemble and client still works

• Kill another server (now only one left), see how it goes.