zookeeper in action

18
ZooKeeper In Action Juven Xu (许晓斌)

Upload: juvenxu

Post on 05-Dec-2014

1.217 views

Category:

Technology


0 download

DESCRIPTION

Getting started with Apache ZooKeeper

TRANSCRIPT

Page 1: Zookeeper In Action

ZooKeeper In ActionJuven Xu (许晓斌)

Page 2: Zookeeper In Action

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

Page 3: Zookeeper In Action

Requirements

• JDK 1.6 & JAVA_HOME

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

Page 4: Zookeeper In Action

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

Page 5: Zookeeper In Action

ZooKeeper Data Tree

Page 6: Zookeeper In Action

ZooKeeper API

Page 7: Zookeeper In Action

bin/zkCli.sh

• zkCli.sh -server localhost:2181

Page 8: Zookeeper In Action

Exercise #1

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

Page 9: Zookeeper In Action

Problem with Polling

Page 10: Zookeeper In Action

ZooKeeper Watch

Page 11: Zookeeper In Action

Coding for Watch

Page 12: Zookeeper In Action

Coding for Watch

Page 13: Zookeeper In Action

Exercise #2

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

Page 14: Zookeeper In Action

ZooKeeper Ensemble

Page 15: Zookeeper In Action

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

Page 16: Zookeeper In Action

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}

Page 17: Zookeeper In Action

Client of a Ensemble

Page 18: Zookeeper In Action

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.