Linux安装zookeeper

  1. 作者QQ:67065435 QQ群:821635552

  2. 安装JAVA

  3. 安装前的准备

    yum install \
    vim \
    gcc \
    gcc-c++ \
    wget \
    make \
    automake \
    autoconf \
    -y
    
  4. zk-3.4.13

  5. 下载安装zookeeper

    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.13.tar.gz
    mv ./zookeeper-3.4.13 /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
    
  6. 修改配置

    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
    
  7. 修改环境变量脚本

    vim /usr/local/zookeeper/bin/zkEnv.sh
    
    ZOO_LOG_DIR="${ZOOBINDIR}/../logs"
    
    ESC
    :wq
    
  8. automake-1.15

  9. 安装automake-1.15

    cd ~
    wget http://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz
    tar -zxvf automake-1.15.tar.gz
    cd automake-1.15
    ./configure
    make
    make install
    
  10. 安装zookeeper-c

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

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

    cd /etc/systemd/system
    
    vim zookeeper.service
    
    [Unit]
    Description=开机启动zookeeper.
    After=default.target network.target
    
    [Service]
    User=root
    Group=root
    Type=forking
    ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
    PrivateTmp=false
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    ESC
    :wq
    
  13. 安装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
    
  14. 安装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
    
  15. 修改php.ini

    vim /usr/local/php/lib/php.ini
    
    extension=libzookeeper.so
    extension=zookeeper.so
    
    ESC
    :wq
    
  16. 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;
        }
    }
    
  17. 参考链接

  18. 参考链接

Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2012-12-31 12:00:00

results matching ""

    No results matching ""