一个80后
程序员的笔记

consul和nsq安装使用

下载Consul和nsq:

官网:https://www.consul.io

当前版本:https://releases.hashicorp.com/consul/0.9.0/consul_0.9.0_linux_amd64.zip

官网:http://nsq.io

当前版本:https://s3.amazonaws.com/bitly-downloads/nsq/nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz

安装consul:

unzip consul_0.9.0_linux_amd64.zip
mv consul /usr/local/bin/
#coreos下是/opt/bin

安装nsq: 

tar zxvf nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz
cd nsq-1.0.0-compat.linux-amd64.go1.8/bin
mv nsq-1.2.0.linux-amd64.go1.12.9 /usr/local/nsq-1.2.0
ln -sf /usr/local/nsq/bin/* /usr/local/bin/.


#coreos下是/opt/bin
sudo mv * /opt/bin

新建一个存sh的目录:

sudo mkdir /data/sh
cd  /data/sh
sudo touch consul.sh
sudo touch nsq.sh
sudo touch start.sh

设置运行权限: 

sudo chmod +x consul.sh nsq.sh start.sh

这里注意主控端的consul和其他的脚本是有区别的:

主控consul.sh: 

#! /bin/bash
CONSUL_FILE=/data/disk/consul
CONSUL_LOG=${CONSUL_FILE}/consul.log
ID=`ps -ef | grep "consul" | grep -v "grep" | grep -v "consul.sh" | awk '{print $2}'`

function Run_consul(){
/usr/local/bin/consul agent \
        -data-dir=${CONSUL_FILE} \
        -ui -server \
        --bootstrap-expect 3 \
        -advertise=192.168.140.197 \
        -client 0.0.0.0 >> "${CONSUL_LOG}" 2>&1 &
echo "程序启动完成"
}

if [ -z "$ID" ];then
    echo "没有找到进程"
    Run_consul
else
    echo $ID
        for id in $ID
        do
        kill -9 $id
        echo "killed $id"
    done
    Run_consul
fi

从控:

#! /bin/bash
CONSUL_FILE=/data/disk/consul
CONSUL_LOG=${CONSUL_FILE}/consul.log
ID=`ps -ef | grep "consul" | grep -v "grep" | grep -v "consul.sh" | awk '{print $2}'`
if [ ! -d "${CONSUL_FILE}" ];then
        sudo mkdir -p ${CONSUL_FILE}
fi
function Run_consul(){
/usr/local/bin/consul agent \
                -data-dir=${CONSUL_FILE} \
                -ui -server \
                --bootstrap-expect 3 \
                -advertise=192.168.140.198 -client 0.0.0.0 \
                -join 192.168.140.197 >> "${CONSUL_LOG}" 2>&1 &
echo "程序启动完成"
}

if [ -z "$ID" ];then
    echo "没有找到进程"
    Run_consul
else
    echo $ID
        for id in $ID
        do
        kill -9 $id
        echo "killed $id"
    done
    Run_consul
fi

#新版Coreos
sudo /opt/bin/consul agent -data-dir=/data/docker/data/consul -ui -server --bootstrap-expect 3 -advertise=192.168.40.14 -client 0.0.0.0 -join 192.168.40.13 &

nsq启动脚本nsq.sh这里注意两个--broadcast-address的IP地址,

每个服务器需要改成自己的!!! 

#! /bin/bash
NSQ_FILE=/data/disk/nsq
NSQLOOKUPD_LOG=${NSQ_FILE}/log/nsqlookupd.log
NSQD_LOG=${NSQ_FILE}/log/nsqd.log
NSQADMIN_LOG=${NSQ_FILE}/log/nsqadmin.log

ID=`ps -ef | grep "nsq" | grep -v "grep" | grep -v "nsq.sh" | awk '{print $2}'`

if [ ! -d "${NSQ_FILE}" ];then
        sudo mkdir -p ${NSQ_FILE}
fi
if [ ! -d "${NSQ_FILE}/log" ];then
        sudo mkdir -p ${NSQ_FILE}/log
fi
if [ ! -d "${NSQ_FILE}/data" ];then
        sudo mkdir -p ${NSQ_FILE}/data
fi

function Run_nsq(){
/usr/local/bin/nsqlookupd \
        --broadcast-address=192.168.140.197 >> "${NSQLOOKUPD_LOG}" 2>&1 & \
/usr/local/bin/nsqd \
        --data-path=$NSQ_FILE/data \
        --broadcast-address=192.168.140.197 \
        --lookupd-tcp-address=192.168.140.197:4160 \
        --lookupd-tcp-address=192.168.140.198:4160 \
        --lookupd-tcp-address=192.168.140.199:4160 >> "${NSQD_LOG}" 2>&1 & \
/usr/local/bin/nsqadmin \
        --lookupd-http-address=192.168.140.197:4161 \
        --lookupd-http-address=192.168.140.198:4161 \
        --lookupd-http-address=192.168.140.199:4161 >> "${NSQADMIN_LOG}" 2>&1 &

echo "程序启动完成"
}

if [ -z "$ID" ];then
    echo "没有找到进程"
    Run_nsq
else
    echo $ID
        for id in $ID
        do
        kill -9 $id
        echo "killed $id"
    done
    Run_nsq
fi

启动脚本start.sh: 

#!/bin/bash

/data/sh/consul.sh
/data/sh/nsq.sh

添加自启动: 

chmod +x /etc/rc.d/rc.local
vi /etc/rc.local

ulimit -n 100000下面添加: 

/data/sh/start.sh

遇见问题:1、NSQ启动后,只有2个进程,报错提示

406900 FATAL: --data-path=/data/docker/data/nsq/data in use (possibly by another instance of nsqd)

这是因为这个目录不存在,新建 

mkdir -p /data/docker/data/nsq/data

导出导consul配置

consul snapshot save --http-addr=http://192.168.2.110:8500 consul_state_20201019.snap
vim consul_state_20201019.snap
consul snapshot restore --http-addr=http://192.168.2.9:8500 consul_state_20201019.snap

赞(0) 打赏
未经允许不得转载:FoolTiger笔记本 » consul和nsq安装使用
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏