CentOS7.X安装zookeeper-3.4.11

  1. 云栖社区>
  2. 博客>
  3. 正文

CentOS7.X安装zookeeper-3.4.11

qq2233466866 2018-06-11 16:44:33 浏览2404
展开阅读全文

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;
        }
    }

网友评论

登录后评论
0/500
评论
qq2233466866
+ 关注