NSQ 安装指南

NSQ 安装指南

假设我们的服务器按照下面要求编排。

nsqlookup 集群列表

192.168.234.77
192.168.234.36
192.168.234.39

nsq 节点

192.168.234.117
192.168.234.118

nsqadmin 节点

192.168.234.119

安装前准备

下载最新的二进制包,命令行操作如下:

tar -xvf nsq-0.3.8.linux-amd64.go1.6.2.tar.gz 
mv nsq-0.3.8.linux-amd64.go1.6.2 nsq

构建环境变量

设置nsq的环境变量,假设nsq的bin路径在/usr/local/software/nsq下。运行命令如下:

$ vi /etc/profile
$ export NSQ_HOME=/usr/local/software/nsq
$ export PATH=$PATH:$NSQ_HOME/bin
$. /etc/profile

构建运行脚本

创建执行脚本,分别创建start-nsqlookup.sh, start-nsq.sh和 start-nsqadmin.

start-nsqlookup.sh脚本内容:

#nsqlookup
eval nsqlookupd --verbose > nsqlookup.log 2>&1 "&"

start-nsq.sh 脚本内容:

#nsq 
eval nsqd –data-path=/usr/local/software/nsq/data –lookupd-tcp-address=192.168.234.77:4160,192.168.234.36:4160,192.168.234.39:4160 > nsqd.log 2>&1 “&”

start-nsqadmin.sh 脚本内容

#nsqadmin 
eval nsqadmin –lookupd-http-address=192.168.234.77:4161,192.168.234.36:4161,192.168.234.39:4161 > nsqadmin.log 2>&1 “&”

运行脚本

首先需要安装次序运行上面的脚本,必选先运行nsqlookup集群,在运行start-nsqadmin.sh或者start-nsq.sh.

在对应服务器上执行对应的脚本。

nsqlookup节点

#192.168.234.77
#192.168.234.36
#192.168.234.39
sh start-nsqlookup.sh

nsq节点上 

#192.168.234.117 
#192.168.234.118 
sh start-nsq.sh

nsqadmin节点上 

#192.168.234.119 
sh start-nsqadmin.sh

查看运行效果,可以直接访问nsqadmin节点http://192.168.234.119:4171/

创建Producer

package main

import (
    "log"
    "github.com/bitly/go-nsq"
    "io/ioutil"
    "strconv"
)
var nullLogger = log.New(ioutil.Discard, "", log.LstdFlags)

func sendMsg(message string){
    //init default config
    config := nsq.NewConfig()

    w, _ := nsq.NewProducer("192.168.2.117:4150", config)
    err := w.Ping()
    if err != nil {
        //192.168.2.117:4150,192.168.2.68:4150
        log.Fatalln("error ping 10.50.115.16:4150", err)
        // switch the second nsq. You can use nginx or HAProxy for HA.
        w, _ = nsq.NewProducer("192.168.2.68:4150", config)
    }
    w.SetLogger(nullLogger, nsq.LogLevelInfo)

    err2 := w.Publish("a-test", []byte(message))
    if err2 != nil {
        log.Panic("Could not connect nsq")
    }

    w.Stop()
}
func main() {
    for i := 0; i < 2; i ++ {
        sendMsg("msg index "+ strconv.Itoa(i + 10000))
    }

}

恭喜,你已经成功发送两个消息到nsq节点。

创建Consumer

package main

import (
    "log"
    "github.com/bitly/go-nsq"
    "fmt"
)


func doSimpleConsumerTask(){

    config := nsq.NewConfig()
    q, _ := nsq.NewConsumer("a-test", "ch", config)
    q.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {

        log.Printf("message: %v", string(message.Body))
        message.Finish()
        return nil
    }))
    lookupAddr := []string {
        "192.168.234.77:4161",
        "192.168.234.36:4161",
        "192.168.234.39:4161",
    }
    err := q.ConnectToNSQLookupds(lookupAddr)
    if err != nil {
        log.Panic("Could not connect")
    }
    <-q.StopChan

    stats := q.Stats()
    fmt.Sprintf("message received %d, finished %d", stats.MessagesReceived, stats.MessagesFinished)
}
func main(){
    doSimpleConsumerTask()
}

程序运行输出:

2017/02/23 09:07:50 message: msg index 10000
2017/02/23 09:07:50 message: msg index 10001
  • 消费者持续接受发送来的信息,运行程序后端。

尊重别人的劳动成果,原文网址:https://blog.csdn.net/john_f_lau/article/details/56666196

展开阅读全文

没有更多推荐了,返回首页