这两天在捣腾zookeeper 分布式

环境:centos7, java 1.8.0_171(预先已安装)

A主机:192.168.0.106

B主机:192.168.0.107

C主机:192.168.0.108

zookeeper版本:apache-zookeeper-3.5.5-bin.tar.gz

下载地址:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz

zookeeper 安装:

在三台机子上分别下载安装,解压到 /opt目录

A、B、C#tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz -C /opt

A、B、C#cd /opt/apache-zookeeper-3.5.5-bin

A、B、C#cp conf/zoo_sample.cfg  conf/zoo.cfg

A、B、C# vim conf/zoo.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181

server.1=192.168.0.106:2888:3888
server.2=192.168.0.107:2889:3889
server.3=192.168.0.108:2890:3890

A#echo '1' > /tem/zookeeper/myid

B#echo '2' > /tem/zookeeper/myid

C#echo '3' > /tem/zookeeper/myid

依次运行

A、B、C#/bin/bash /opt/apache-zookeeper-3.5.5-bin/bin/zkServer.sh  start  #启动

A、B、C#/bin/bash /opt/apache-zookeeper-3.5.5-bin/bin/zkServer.sh  status #查看状态

A####
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader

B####
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

C####
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

至此zookeeper分布式集群搭建完成

 

PHP 7.2 扩展安装

注意:我第一次是在Ubuntu 17上装PHP扩展, 装到最后报错,google了下有很多人也遇到过这个问题,有说ubuntu系统不支持的,如是我换了台centos安装zookeeper扩展

make  all-am
make[1]: Entering directory '/root/apache-zookeeper-3.5.5/zookeeper-client/zookeeper-client-c'
/bin/bash ./libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I.  -I./include -I./tests -I./generated   -Wall -Werror -Wdeclaration-after-statement  -g -O2 -D_GNU_SOURCE -MT zookeeper.lo -MD -MP -MF .deps/zookeeper.Tpo -c -o zookeeper.lo `test -f 'src/zookeeper.c' || echo './'`src/zookeeper.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I./include -I./tests -I./generated -Wall -Werror -Wdeclaration-after-statement -g -O2 -D_GNU_SOURCE -MT zookeeper.lo -MD -MP -MF .deps/zookeeper.Tpo -c src/zookeeper.c  -fPIC -DPIC -o .libs/zookeeper.o
src/zookeeper.c: In function ‘print_completion_queue’:
src/zookeeper.c:2542:5: error: null argument where non-null required (argument 1) [-Werror=nonnull]
     fprintf(LOGSTREAM,"Completion queue: ");
     ^~~~~~~
src/zookeeper.c:2544:9: error: null argument where non-null required (argument 1) [-Werror=nonnull]
         fprintf(LOGSTREAM,"empty\n");
         ^~~~~~~
src/zookeeper.c:2550:9: error: null argument where non-null required (argument 1) [-Werror=nonnull]
         fprintf(LOGSTREAM,"%d,",cptr->xid);
         ^~~~~~~
src/zookeeper.c:2553:5: error: null argument where non-null required (argument 1) [-Werror=nonnull]
     fprintf(LOGSTREAM,"end\n");
     ^~~~~~~
cc1: all warnings being treated as errors
Makefile:1032: recipe for target 'zookeeper.lo' failed
make[1]: *** [zookeeper.lo] Error 1
make[1]: Leaving directory '/root/apache-zookeeper-3.5.5/zookeeper-client/zookeeper-client-c'
Makefile:691: recipe for target 'all' failed
make: *** [all] Error 2

安装环境:

Centos7 , PHP7.2 , Nginx

一,首先下载Cppunit 测试库

https://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/cppunit-1.12.1.tar.gz/download

#tar -zxvf cppunit-1.12.1.tar.gz

#./configure

#make && make install

 

二,下载zookeeper二进制源码 , 安装zookeeper client C库

# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5.tar.gz

# tar -zxvf apache-zookeeper-3.5.5.tar.gz #解压

#cd /root/apache-zookeeper-3.5.5/zookeeper-client/zookeeper-client-c  #进入客户端C库

#autoreconf -if

#ACLOCAL="aclocal -I /usr/local/share/aclocal" autoreconf -if

#./configure --prefix=/opt/zookeeper-lib  #如果第一步Cppunit 没有安装 加 --without-cppunit 跳过测试 可以看README文档

#make && make install

 

三,下载zookeeper扩展包:

https://pecl.php.net/package/zookeeper

下载地址:https://pecl.php.net/get/zookeeper-0.6.4.tgz

# tar -zxvf zookeeper-0.6.4.tgz 

# cd zookeeper-0.6.4

#/usr/local/php/bin/phpize

#./configure –with-php-config=/usr/local/php/bin/php-config –with-libzookeeper-dir=/opt/zookeeper-lib #这里是上一步安装的c客户端目录

#make && make install

至此就安装完成!!!

 

简单测试:

1, 在zookeeper 创建一个节点

# bin/zkCli.sh -server
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[cluster, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 2] create /phpDemo 1
Created /phpDemo
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /
[cluster, zookeeper, phpDemo]
[zk: 127.0.0.1:2181(CONNECTED) 4]

 

2, 然后新建phpZookeeperDemo.php文件,写入代码如下:

<?php
class ZookeeperDemo extends Zookeeper{
    public function __construct($host,$watcher_cb = null, $recv_timeout = 1000){
        parent::__construct($host,$watcher_cb,$recv_timeout);
    }
    public function watcher($i,$type,$key){
        echo "Insider Watcher\n";
        //监听 /phpDemo 节点
        $this->get('/phpDemo',array($this,'watcher'));
    }
}
$zk_php = new ZookeeperDemo('192.168.0.106:2181');
$zk_php->get('/phpDemo',array($zk_php,'watcher'));
while(true){
    echo '.';
    sleep(2);
}

 

3, 运行phpZookeeperDemo.php ,  修改 /phpDemo 节点

输出"Insider Watcher"。。 

成功!

 

资料阅读:

http://zookeeper.apache.org/doc/r3.5.5/index.html

https://blog.51cto.com/snowtiger/2073908

https://github.com/andreiz/php-zookeeper

https://blog.csdn.net/u012152619/article/list/2?

https://github.com/apache/zookeeper

http://mirror.bit.edu.cn/apache/zookeeper/

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐