它是一个为分布式运用供应同等性做事的软件, 供应的功能包括:配置掩护、域名做事、分布式同步、组做事等。

ZooKeeper的目标便是封装好繁芜易出错的关键做事, 将大略易用的接口和性能高效、功能稳定的系统供应给用户。

要在php中利用zookeeper, 先要安装php zookeeper扩展, 要安装php zookeeper扩展, 先安装zookeeper

phpzookeeper扩展Linux zookeeper安排 GraphQL

初始化操作系统须要安装的依赖

> # yum -y install gcc gcc-c++ automake autoconf libtool make libaio-devel

做事端:

1 安装java环境

查看yum库中都有哪些jdk版本, 运行:

> # yum search java | grep jdk

选择版本进行安装:

> # yum install java-1.8.0-openjdk

查看是否安装成功:

> # java -version

openjdk version "1.8.0_242"

2 安装zookeeper

在这里面下载最新版的稳定版

https://downloads.apache.org/zookeeper/stable/

下载zookeeper包

> # wget https://downloads.apache.org/zookeeper/stable/apache-zookeeper-3.5.8-bin.tar.gz

解压

> # tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz

进入根目录

> # mv apache-zookeeper-3.5.8-bin /usr/local/zookeeper

> # cd /usr/local/zookeeper/conf

> # cp zoo_sample.cfg zoo.cfg

> # cd /usr/local/zookeeper/bin

> # ./zkServer.sh start

/usr/bin/java

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Starting zookeeper ... STARTED

> # ./zkServer.sh status

/usr/bin/java

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Client port found: 2181. Client address: localhost.

Mode: standalone

开放端口号

firewall-cmd --zone=public --permanent --add-port=2181/tcp

firewall-cmd --reload

3、设置开机自启动

> # vim /usr/lib/systemd/system/zookeeper.service

[Unit]

Description=zookeeper.service

After=network.target

ConditionPathExists=/usr/local/zookeeper/conf/zoo.cfg

[Service]

Type=forking

User=root

Group=root

ExecStart=/usr/local/zookeeper/bin/zkServer.sh start

ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop

[Install]

WantedBy=multi-user.target

> # systemctl daemon-reload

> # systemctl enable zookeeper

> # systemctl start zookeeper

> # reboot

> # ./zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Mode: standalone

4 客户端操作方法

./zkCli.sh -timeout 0 -r -server ip:port

./zkCli.sh -timeout 5000 -server localhost:2181

-r :纵然ZooKeeper做事器集群一样平常以上的做事器当掉,也给客户端供应读做事

查看帮助

[zk: localhost:2181(CONNECTED) 0] help

ZooKeeper -server host:port cmd args

stat path [watch]

set path data [version]

ls path [watch]

delquota [-n|-b] path

ls2 path [watch]

setAcl path acl

setquota -n|-b val path

history

redo cmdno

printwatches on|off

delete path [version]

sync path

listquota path

rmr path

get path [watch]

create [-s] [-e] path data acl

addauth scheme auth

quit

getAcl path

close

connect host:port

查看根节点

[zk: localhost:2181(CONNECTED) 0] ls /

[zookeeper]

create [-s] [-e] path data acl

-s : 创建顺序节点

-e : 创建临时节点

path : 路径

data : 数据

acl : 权限

create默认创建的是持久节点

ACL的格式由<schema>:<id>:<acl>三段组成。

schema:可以取下列值:world, auth, digest, host/ip

id: 标识身份,值依赖于schema做解析。

acl:便是权限:cdwra分别表示create, delete,write,read, admin

acl来源: https://www.jianshu.com/p/392248ab27f4

创建一个新节点mykey1

[zk: localhost:2181(CONNECTED) 1] create /mykey1 myvalue1

Created /mykey1

创建一个新节点mykey2

[zk: localhost:2181(CONNECTED) 2] create /mykey2 myvalue2

Created /mykey2

查看根节点

[zk: localhost:2181(CONNECTED) 3] ls /

[zookeeper, mykey2, mykey1]

获取mykey1节点

[zk: localhost:2181(CONNECTED) 4] get /mykey1

myvalue1

cZxid = 0x6

ctime = Tue Jun 30 22:48:23 CST 2020

mZxid = 0x6

mtime = Tue Jun 30 22:48:23 CST 2020

pZxid = 0x6

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 8

numChildren = 0

获取mykey2节点

[zk: localhost:2181(CONNECTED) 5] get /mykey2

myvalue2

cZxid = 0x7

ctime = Tue Jun 30 22:48:59 CST 2020

mZxid = 0x7

mtime = Tue Jun 30 22:48:59 CST 2020

pZxid = 0x7

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 8

numChildren = 0

修正mykey1节点的值

[zk: localhost:2181(CONNECTED) 6] set /mykey1 myvalue11

cZxid = 0x6

ctime = Tue Jun 30 22:48:23 CST 2020

mZxid = 0x8

mtime = Tue Jun 30 22:50:54 CST 2020

pZxid = 0x6

cversion = 0

dataVersion = 1

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 9

numChildren = 0

删除节点

[zk: localhost:2181(CONNECTED) 11] delete /mykey1

开放端口号:

> # firewall-cmd --permanent --zone=public --add-port=2181/tcp

> # firewall-cmd --reload

排查缘故原由:

./bin/zkServer.sh status

/usr/bin/java

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Client port found: 2181. Client address: localhost.

Error contacting service. It is probably not running.

> # /usr/local/zookeeper/logs/

> # ls -al

zookeeper-root-server-localhost.localdomain.out

> # vim /usr/local/zookeeper/logs/zookeeper-root-server-localhost.localdomain.out

缺点: 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain

缘故原由: 从目前的最新版本3.5.5开始, 带有bin名称的包才是我们想要的下载可以直策应用的里面有编译后的二进制的包, 而之前的普通的tar.gz的包里面是只是源码的包无法直策应用。

客户端

PHP安装zookeeper扩展

1 安装依赖

yum install libtool ant

安装以上依赖时会自动安装升级java-1.8.0

2 安装Cppunit测试库

下载安装包

> # wget https://nchc.dl.sourceforge.net/project/cppunit/cppunit/1.12.1/cppunit-1.12.1.tar.gz

解压安装包

> # tar -zxvf cppunit-1.12.1.tar.gz

进入目录

> # cd cppunit-1.12.1

configure

./configure

安装

> # make && make install

3、安装zookeeper

在这里面下载最新版的稳定版

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

下载zookeeper包

> # wget http://mirror.bit.edu.cn/apache/zookeeper/stable/apache-zookeeper-3.5.8.tar.gz

解压

> # tar -zxvf apache-zookeeper-3.5.8.tar.gz

进入根目录

> # cd apache-zookeeper-3.5.8/

编译compile_jute

> # ant compile_jute

进入client目录

> # cd zookeeper-client/zookeeper-client-c

autoreconf

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

配置安装目录

> # ./configure --prefix=/usr/local/zookeeper

安装

> # make && make install

4 安装PHP zookeeper扩展

> # wget http://pecl.php.net/get/zookeeper-0.6.4.tgz

> # tar -zxvf zookeeper-0.6.4.tgz

> # cd zookeeper-0.6.4

> # phpize

> # ./configure -with-libzookeeper-dir=/usr/local/zookeeper

> # make && make install

5 开启扩展配置

末了往php.ini里面添加

extension=zookeeper.so

> # php -m | grep zookeeper

zookeeper

安装测试

操作系统: CentOS7.6

PHP环境: PHP7.2.31

测试韶光: 20200630

更新韶光: 20200630

紧张问题: 做事端(bin)和客户端分属不同的安装包