zookeeper
-
检查是否安装JDK
rpm -qa|grep -E '^open[jre|jdk]|j[re|dk]'
-
卸载已安装JDK
rpm -qa|grep Java|xargs rpm -e --nodeps
-
yum安装jdk
yum search java|grep jdk yum install java-1.8.0-openjdk
-
检查安装是否成功
java -version java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
-
下载并安装
cd /root wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz tar -xvf zookeeper-3.4.11.tar.gz mv ./zookeeper-3.4.11 /usr/local/zookeeper mkdir /usr/local/zookeeper/var mkdir /usr/local/zookeeper/var/log echo 1 > /usr/local/zookeeper/var/log/myid cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
-
修改配置
vim /usr/local/zookeeper/conf/zoo.cfg tickTime=2000 initLimit=5 syncLimit=2 dataDir=/usr/local/zookeeper/var/log dataLogDir=/usr/local/zookeeper/var/log clientPort=2181 # 多台server在下面配置即可,如果单台服务器构建多个server,则每个server用过的端口不能重复使用 # 格式: server.[n]=[server_ip]:[server与leader交互端口]:[server选举leader端口] # server.1=172.31.9.59:2182:2183 # server.2=172.31.9.60:2182:2183 # server.3=172.31.9.61:2182:2183 maxClientCnxns=60 minSessionTimeout=60 maxSessionTimeout=120 # purgeInterval含义: 0-禁用自动清除 1-使用自动清除 autopurge.purgeInterval=1 ESC :wq
-
安装zookeeper-c
cd /usr/local/zookeeper/src/c ./configure make make install
-
防火墙开启
systemctl enable firewalld systemctl start firewalld firewall-cmd --zone=public --permanent --add-port=2181/tcp firewall-cmd --zone=public --permanent --add-port=2182/tcp firewall-cmd --zone=public --permanent --add-port=2183/tcp firewall-cmd --reload
-
单元文件
# 进入单元文件目录 cd /etc/systemd/system # 创建redis单元文件,格式为: [单元文件名].[单元文件类型] vim zookeeper.service [Unit] Description=开机启动zookeeper. After=default.target network.target [Service] User=root Group=root Type=forking PIDFile=/usr/local/zookeeper/var/log/zookeeper_server.pid ExecStart=/usr/local/zookeeper/bin/zkServer.sh start ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop PrivateTmp=false Restart=always [Install] WantedBy=multi-user.target ESC :wq
-
安装php扩展:zookeeper参考链接
cd /root wget https://pecl.php.net/get/zookeeper-0.4.0.tgz tar -zxvf zookeeper-0.4.0.tgz cd zookeeper-0.4.0/ phpize ./configure make make install
-
安装php扩展libzookeeper参考链接
cd /root wget https://github.com/Timandes/libzookeeper/archive/v0.7.2.tar.gz tar -xvf v0.7.2.tar.gz cd libzookeeper-0.7.2 phpize ./configure make make install # 用来调起zookeeper-admin,仓库地址: https://github.com/Timandes/zookeeper-admin.git
-
修改php.ini
vim /usr/local/php/lib/php.ini extension=libzookeeper.so extension=zookeeper.so ESC :wq
-
PHP使用进程公共锁
# 出了$zc的作用域之后,锁将不存在 $zc = new \Zookeeper('127.0.0.1:2181'); //或者 //$zc = new \Zookeeper(); //$zc->connect('127.0.0.1:2181'); $zookeeper_key = '/xxx'; if ($zc->exists($zookeeper_key)) { //如果锁存在,则程序正在运行,不运行新的程序 return false; }else{ //如果锁文件不存在,则创建进程锁文件,运行程序 $acl = [ [ 'perms' => \Zookeeper::PERM_ALL,//共享锁(用来跨进程执行某个程序) 'scheme' => 'world', 'id' => 'anyone', ] ]; //尝试创建锁 $zookeeper_key_res = $zc->create($zookeeper_key, null, $acl, \Zookeeper::EPHEMERAL);//临时锁(可共享的) if ($zookeeper_key_res == $zookeeper_key) { //创建锁成功运行程序 //做些什么,比如等待10秒 sleep(10); $zc->delete($zookeeper_key);//其实不执行也会删除,因为这是一个临时锁,且return之后不再能取到$zc return true; } else { //创建锁失败不运行程序 return false; } }