本文共 3325 字,大约阅读时间需要 11 分钟。
检查是否安装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 jdkyum install java-1.8.0-openjdk
检查安装是否成功
java -versionjava 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 /rootwget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gztar -xvf zookeeper-3.4.11.tar.gzmv ./zookeeper-3.4.11 /usr/local/zookeepermkdir /usr/local/zookeeper/varmkdir /usr/local/zookeeper/var/logecho 1 > /usr/local/zookeeper/var/log/myidcp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
修改配置
vim /usr/local/zookeeper/conf/zoo.cfgtickTime=2000initLimit=5syncLimit=2dataDir=/usr/local/zookeeper/var/logdataLogDir=/usr/local/zookeeper/var/logclientPort=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:2183maxClientCnxns=60minSessionTimeout=60maxSessionTimeout=120# purgeInterval含义: 0-禁用自动清除 1-使用自动清除autopurge.purgeInterval=1ESC:wq
安装zookeeper-c
cd /usr/local/zookeeper/src/c./configuremakemake install
防火墙开启
systemctl enable firewalldsystemctl start firewalldfirewall-cmd --zone=public --permanent --add-port=2181/tcpfirewall-cmd --zone=public --permanent --add-port=2182/tcpfirewall-cmd --zone=public --permanent --add-port=2183/tcpfirewall-cmd --reload
单元文件
# 进入单元文件目录cd /etc/systemd/system# 创建redis单元文件,格式为: [单元文件名].[单元文件类型]vim zookeeper.service[Unit]Description=开机启动zookeeper.After=default.target network.target[Service]User=rootGroup=rootType=forkingPIDFile=/usr/local/zookeeper/var/log/zookeeper_server.pidExecStart=/usr/local/zookeeper/bin/zkServer.sh startExecReload=/usr/local/zookeeper/bin/zkServer.sh restartExecStop=/usr/local/zookeeper/bin/zkServer.sh stopPrivateTmp=falseRestart=always[Install]WantedBy=multi-user.targetESC:wq
安装php扩展:zookeeper
cd /rootwget https://pecl.php.net/get/zookeeper-0.4.0.tgztar -zxvf zookeeper-0.4.0.tgzcd zookeeper-0.4.0/phpize./configuremakemake install
安装php扩展libzookeeper
cd /rootwget https://github.com/Timandes/libzookeeper/archive/v0.7.2.tar.gztar -xvf v0.7.2.tar.gzcd libzookeeper-0.7.2phpize./configuremakemake install# 用来调起zookeeper-admin,仓库地址:https://github.com/Timandes/zookeeper-admin.git
修改php.ini
vim /usr/local/php/lib/php.iniextension=libzookeeper.soextension=zookeeper.soESC: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; }}
转载地址:http://zvpzl.baihongyu.com/