linux shell快速搭建NSQ叢集

mengdj發表於2020-12-17

自己研究的一個golang專案,裡面用到了訊息佇列,因為NSQ 易於部署,並帶有統計,管理和生產者的內建HTTP介面 的優點,而且我本身對訊息順序就不關注。但在開發過程中頻繁的敲shell命令麻煩,索性自己寫個shell快速搭建NSQ叢集,shell具體程式碼如下

#!/usr/bin/env bash
###########################################################
#Author:mengdj@outlook.com
#Created Time:2020.12.16 11:56
#Description:execute nsq server
#Version:0.0.1
#File:nsqh
###########################################################

NSQ_VERSION=`./nsqd --version`
NSQ_ADDRESS="127.0.0.1"
NSQ_CURRENT_DIR=$(pwd)
NSQ_LOG_DIR=$NSQ_CURRENT_DIR/log

function start(){
    #create nsqlookupd
    for p in {1..2};
    do
            $NSQ_CURRENT_DIR/nsqlookupd \
            -broadcast-address="$NSQ_ADDRESS" \
            -tcp-address="$NSQ_ADDRESS:900$p" \
            -http-address="$NSQ_ADDRESS:901$p" >> "$NSQ_LOG_DIR/nsqlookupd.log" 2>&1 &
    done

    #create nsqd
    for p in {1..4};
    do
        if [ ! -d $NSQ_CURRENT_DIR"/data/"$p ];then
            mkdir -p $NSQ_CURRENT_DIR"/data/"$p
            if [ ! $? -eq 0 ];then
                echo 'create data directory failture.'
                stop
                exit
            fi
        fi
            $NSQ_CURRENT_DIR/nsqd \
            -data-path "data/$p" \
            -broadcast-address="$NSQ_ADDRESS" \
            -tcp-address="$NSQ_ADDRESS:1001$p" \
            -http-address="$NSQ_ADDRESS:1002$p" \
            -lookupd-tcp-address="$NSQ_ADDRESS:9001" \
            -lookupd-tcp-address="$NSQ_ADDRESS:9002" >> "$NSQ_LOG_DIR/nsqd.log" 2>&1 &
    done

    #nsqadmin
    $NSQ_CURRENT_DIR/nsqadmin \
        -http-address="0.0.0.0:9000" \
        -lookupd-http-address="$NSQ_ADDRESS:9011" \
        -lookupd-http-address="$NSQ_ADDRESS:9012" >> "$NSQ_LOG_DIR/nsqadmin.log" 2>&1 &
}

function restart(){
    stop
    start
}

#stop process
function stop(){
    for process in nsqlookupd nsqd nsqadmin;
    do
        pkill "$process"
    done
    echo 'ok'
}

#help info
function help(){
    echo $NSQ_VERSION
    echo 'nsqh start|stop|restart|help'
}

#status
function status(){
    local count=0
    for process in nsqlookupd nsqd nsqadmin;
    do
        local result=`ps -ef|grep "$NSQ_CURRENT_DIR/$process"|grep -v grep|awk '$1="";$3="";$4="";$5="";$6="";$7="";{print $0}'`
        if [ -n "$result" ];then
            echo $result
            let 'count=count+1'
        fi
    done
    if [ $count -ne 0 ];then
        echo -e '\033[32m running \033[0m'
    else
        echo 'stoped.'
    fi
}

if [ $# -eq 0 ];then
    echo 'param can not null.'
else
    if [ ! -d $NSQ_LOG_DIR ];then
        mkdir -p $NSQ_LOG_DIR
        if [ ! $? -eq 0 ];then
            exit
        fi
    fi
    case $1 in
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            restart
            ;;
        status)
            status
            ;;
        help)
            help
            ;;
    esac
fi

啟動 ./nsqh start

重啟 ./nsqh restart

停止 ./nsqh stop

另nsq不提供32位的linux版本,所有我自行編譯了一份32位的版本,下載地址見:download.csdn.net/download/mak0000...

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章