Php connects to zookeeper instance 1. after zookeeper is installed successfully, the corresponding startup script is started in the bin directory of zookeeper.
Start Server
./ZkServer. sh start
Start the client: (* Note: java must be installed for cli)
ZkCli. sh
2. PHP instance:
ClassZookeeperDemoExtendsZookeeper {
Public functionWatcher ($ 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 =NewZookeeperDemo ('2017. 0.0.1: 100 ');
$ Zoo-> get ('/test ',Array($ Zoo, 'Watcher '));
While(True){
Echo'.';
Sleep (2 );
}
Assignment of leader and worker tasks:
ClassWorkerExtendsZookeeper {
ConstCONTAINER = '/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 functionRegister (){
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 functionWatchPrevious (){
$ 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 newException (sprintf ("Something went very wrong! I can't find myself: % s/% s ",
Self: CONTAINER,
$ This-> znode ));
}
Public functionWatchNode ($ 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 functionIsLeader (){
Return$ This-> isLeader;
}
Public functionSetLeader ($ flag ){
$ This-> isLeader = $ flag;
}
Public functionRun (){
$ This-> register ();
While(True){
If($ This-> isLeader ()){
$ This-> doLeaderJob ();
}
Else{
$ This-> doWorkerJob ();
}
Sleep (2 );
}
}
Public functionDoLeaderJob (){
Echo"Leading \ n ";
}
Public functionDoWorkerJob (){
Echo"Working \ n ";
}
}
$ Worker =NewWorker ('127. 0.0.1: 100 ');
$ Worker-> run ();
You can start three php processes to view 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
After ctrl + c closes the leader process, it will find that process 2 and 3 will elect a new leader