CentOS7.X安装zookeeper-3.4.11

qq2233466866 2018-06-11

系统软件 编程语言 系统研发与运维 网络与数据通信 linux php 开发框架与中间件 java LOG yum Server zookeeper vim wget github

zookeeper

  1. 安装前的准备

    yum install \
    vim \
    gcc \
    gcc-c++ \
    wget \
    make \
    automake \
    autoconf \
    -y \
  2. 检查是否安装JDK

    rpm -qa|grep -E '^open[jre|jdk]|j[re|dk]'
  3. 卸载已安装JDK

    rpm -qa|grep java|xargs rpm -e --nodeps
  4. yum安装jdk

    yum search java|grep jdk
    yum install java-1.8.0-openjdk
  5. 检查安装是否成功

    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)
  6. 下载并安装

    cd /root
    wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.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
  7. 修改配置

    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=192.168.0.123:2182:2183
    # server.2=192.168.0.123:2182:2183
    # server.3=192.168.0.123:2182:2183
    maxClientCnxns=60
    minSessionTimeout=60
    maxSessionTimeout=120
    # purgeInterval含义: 0-禁用自动清除 1-使用自动清除
    autopurge.purgeInterval=1
    
    ESC
    :wq
  8. 安装zookeeper-c

    cd /usr/local/zookeeper/src/c
    
    ./configure
    
    make
    make install
  9. 防火墙开启

    systemctl enable firewalld
    systemctl start firewalld
    firewall-cmd --zone=public --permanent --add-port=2181/tcp
    firewall-cmd --reload
  10. 单元文件

    # 进入单元文件目录
    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
  11. 安装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
  12. 安装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
  13. 修改php.ini

    vim /usr/local/php/lib/php.ini
    
    extension=libzookeeper.so
    extension=zookeeper.so
    
    ESC
    :wq
  14. 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',
            ]
        ];
        //尝试创建节点
        //EPHEMERAL: 临时节点
        //EPHEMERAL_SEQUENTIAL: 临时顺序节点
        //PERSISTENT: 持久节点(暂时别用)
        //PERSISTENT_SEQUENTIAL: 持久顺序节点(暂时别用)
        $zookeeper_key_res = $zc->create($zookeeper_key, '这是节点的值', $acl, \Zookeeper::EPHEMERAL);//临时节点(可共享的)
        if ($zookeeper_key_res == $zookeeper_key) {
            //创建节点成功运行程序
            //做些什么,比如等待10秒
            sleep(10);
            //可以尝试取节点的值
            //$zookeeper_val = $zc->get($zookeeper_key);
            //echo $zookeeper_val;
            $zc->delete($zookeeper_key);//其实不执行也会删除,因为这是一个临时节点,且return之后不再能取到$zc
            return true;
        } else {
            //创建节点失败不运行程序
            return false;
        }
    }
登录 后评论
下一篇
冒顿单于
8314人浏览
2019-08-28
相关推荐
文档修改声明
478人浏览
2018-11-14 11:20:10
centos7.x搭建disconf
728人浏览
2017-11-12 00:56:00
35.CentOS7.X系统安装
621人浏览
2017-11-14 22:49:00
zk集群搭建
798人浏览
2018-02-17 15:01:37
CentOS7.X安装FastDHT
1061人浏览
2018-06-11 15:46:10
CentOS7.X更新gcc到5.3.0
1737人浏览
2018-06-11 15:50:01
CentOS7.X安装JDK-1.8.0
1063人浏览
2018-06-11 15:55:53
CentOS7.X安装memcached
749人浏览
2018-06-11 16:01:13
CentOS7.X安装mongodb-3.4.2
1121人浏览
2018-06-11 16:04:08
CentOS7.X安装postgresql-10.3
874人浏览
2018-06-11 16:25:10
zookeeper基本安装文档
666人浏览
2018-08-24 15:10:57
CentOS7.x 安装scala
600人浏览
2018-08-16 14:06:00
CentOS7.x 安装Sqoop
617人浏览
2018-07-29 11:27:00
0
0
0
2255