1, after the installation successful zookeeper, in the Zookeeper Bin directory has started the corresponding startup script
Start the server
./zkserver.sh Start
Start Client: (* Note: CLI requires Java installation)
zkcli.sh
2, php Example:
class Zookeeperdemo extends Zookeeper {
Public function Watcher ($i, $type, $key) {
Echo "Insider watcher\n";
Watcher gets consumed so we need to set a new one
$this->get ('/test ', array ($this, ' Watcher '));
}
}
$zoo = new zookeeperdemo (' 127.0.0.1:2181 ');
$zoo->get ('/test ', array ($zoo, ' Watcher '));
while ( true ) {
Echo '.' ;
Sleep (2);
}
Assignment of leader and worker tasks:
class Worker extends Zookeeper {
Const CONTAINER = '/cluster ';
protected $acl = array (
Array (
' Perms ' = Zookeeper:: Perm_all,
' Scheme ' = ' world ',
' id ' = ' anyone '));
private $isLeader = false ;
private $znode;
Public function __construct ($host = ", $watcher _CB = null , $recv _timeout = 10000) {
parent :: __construct ($host, $watcher _CB, $recv _timeout);
}
Public Function Register () {
if (! $this->exists ( self:: CONTAINER)) {
$this->create ( self:: CONTAINER, null , $this-ACL);
}
$this->znode = $this->create ( self:: CONTAINER. '/w-',
null ,
$this->acl,
Zookeeper:: Ephemeral | Zookeeper::sequence);
$this-Znode = Str_replace ( self:: CONTAINER. '/', ', $this-Znode);
printf ("I ' m registred as:%s\n", $this-Znode);
$watching = $this->watchprevious ();
if ($watching = = $this Znode) {
printf ("Nobody here, I ' m The leader\n");
$this->setleader ( true );
}
Else {
printf ("I ' m watching%s\n", $watching);
}
}
Public function watchprevious () {
$workers = $this->getchildren ( self:: CONTAINER);
Sort ($workers);
$size = sizeof ($workers);
for ($i = 0; $i < $size; $i + +) {
if ($this, Znode = = $workers [$i]) {
if ($i > 0) {
$this->get ( self:: CONTAINER. '/' . $workers [$i-1], array ($this, ' Watchnode '));
return $workers [$i-1];
}
return $workers [$i];
}
}
throw New Exception (sprintf ("Something went very wrong! I can ' t find myself:%s/%s ",
Self:: CONTAINER,
$this, Znode));
}
Public function watchnode ($i, $type, $name) {
$watching = $this->watchprevious ();
if ($watching = = $this Znode) {
printf ("I ' m The new leader!\n");
$this->setleader ( true );
}
Else {
printf ("Now I ' m watching%s\n", $watching);
}
}
Public function Isleader () {
return $this, Isleader;
}
Public function Setleader ($flag) {
$this-Isleader = $flag;
}
Public function run () {
$this->register ();
while ( true ) {
if ($this->isleader ()) {
$this->doleaderjob ();
}
Else {
$this->doworkerjob ();
}
Sleep (2);
}
}
Public function Doleaderjob () {
Echo "Leading\n";
}
Public function Doworkerjob () {
Echo "Working\n";
}
}
$worker = new worker (' 127.0.0.1:2181 ');
$worker->run ();
You can start 3 PHP processes to see the script running.
Process 1:
[Root@localhost zookeeper]# php-f worker.php
I ' m registred as:w-0000000010
Nobody here, I ' m the leader
Leading
Process 2:
[Daniel.luo@localhost zookeeper]$ php-f worker.php
I ' m registred as:w-0000000011
I ' m watching w-0000000010
Working
Process 3:
[Daniel.luo@localhost zookeeper]$ php-f worker.php
I ' m registred as:w-0000000012
I ' m watching w-0000000011
Working
CTRL + C closes the leader process and discovers that processes 2 and 3 will elect a new leader