Upload
matt-ho
View
93
Download
0
Embed Size (px)
Citation preview
ZookeeperMatt @ JCConf 2016
About me
https://github.com/methodho
https://www.facebook.com/evermatt
mailto:[email protected]://github.com/methodhohttps://www.facebook.com/evermatthttp://www.softleader.com.tw/
ZooKeeper
ZooKeeper
Java programming to ZooKeeper
o.a.z.ZooKeeper (3.4.9)
o.a.c.f.r.l.LeaderLatch (2.11.0)
Because coordinating distributed systems is a Zoo
Apache ZooKeeper wiki
WHAT IS ZOOKEEPER
https://zookeeper.apache.org/doc/current/zookeeperOver.html
WHAT IS ZOOKEEPER
https://zookeeper.apache.org/doc/current/zookeeperOver.html
server
/bin/zkServer.sh start
zookeeper server
/bin/zkCli.sh [-server localhost:2181]
server
/bin/zkServer.sh stop
ZOOKEEPER CLI
znode
create [-s] [-e] path data
znode
delete path
rmr path
ZOOKEEPER CLI
znode children
ls path [watch]
ls2 path [watch]
znode data (byte[])
set path data [watch]
get path [watch]
ZOOKEEPER CLI
CLIENT
CLIENT
CLIENTZooKeeperServer
ZOOKEEPER WATCH
ZOOKEEPER WATCH
CLIENT
CLIENT
CLIENTZooKeeperServer
watch
watch
watch
ZOOKEEPER WATCH
CLIENT
CLIENT
CLIENT
Server
ZooKeeperServer
watch
watch
watch
Event
Triggered
A MCV EXAMPLE (MODEL 2)
Controller(Servlet/Filter)
View(JSP)
Model(JavaBean)
^_^
...?
BEFORE ZOOKEEPER
ap
hardcode
properties
passes argument ...
cons?
AP1
(run=false)
AP3
(run=false)
AP2
(run=true)
BEFORE ZOOKEEPER
ap
db table control
db lock
cons?
AP1 AP3AP2
DB
runbyAP2
BEFORE ZOOKEEPER
:
...
https://zookeeper.apache.org/doc/trunk/recipes.html
BEFORE ZOOKEEPER
ZooKeeper Recipes!
https://zookeeper.apache.org/doc/trunk/recipes.html
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
LEADER ELECTION
ZookeeperServer
/
/SOME-PATH
/ZNODE_1
CLIENT_1 /ZNODE_2
CLIENT_2
/ZNODE_3
CLIENT_3
A simple way:
Use SEQUENCE & EPHEMERAL znode
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
LEADER ELECTION
ZookeeperServerAdvance solution
/
/SOME-PATH
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
LEADER ELECTION
ZookeeperServer
/
/SOME-PATH
/ZNODE_1
CLIENT_1
Advance solution
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
LEADER ELECTION
ZookeeperServer
/
/SOME-PATH
/ZNODE_1
CLIENT_1 /ZNODE_2
CLIENT_2
Advance solution
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
LEADER ELECTION
ZookeeperServer
/
/SOME-PATH
/ZNODE_1
CLIENT_1 /ZNODE_2
CLIENT_2
/ZNODE_3
CLIENT_3
Advance solution
https://zookeeper.apache.org/doc/trunk/recipes.html#sc_leaderElection
IMPLEMENTING LEADER ELECTION
Start(1)
Connecttozookeeperserver
(2)Createparentifnot
exist(3)
Create-e-snodez
Nodejfound
End(6)
Youareleader
(7)Watchfornodej
Y
N
(5)Findnodej,wherejisthelargestseq
andj