nsq之基本用法

nsq的基本用法

安装参考路径

http://nsq.io/deployment/installing.html

nsq安装脚本

#!/bin/sh

`brew install nsq`

`nohup /usr/local/bin/nsqlookupd > /dev/null &`
`nohup nsqd --lookupd-tcp-address=127.0.0.1:4160  > /dev/null &`
`nohup nsq_to_file --topic=test --output-dir=/Users/bikang1/Documents/GoProject/go-go-go/标准库的使用/src/weibo.com/uverd/kproject/nsq-use/tmp --lookupd-http-address=127.0.0.1:4161  > /dev/null &`
`nohup nsqadmin --lookupd-http-address=127.0.0.1:4161   > /dev/null &`

nsq的详细说明

nsq的后台

后台地址

http://127.0.0.1:4171/

nsq的使用

通过命令行发布一条消息

curl -d 'hello world 1' 'http://127.0.0.1:4151/pub?topic=test'

一直报错~~因为nsqlookup没有指定域名。我绑定了本机名称到本地解决了这个问题

通过go-nsq发布消息

消息地址
    https://godoc.org/github.com/nsqio/go-nsq#Producer.SetLogger

发送消息

package main

import (
    "log"

    "github.com/nsqio/go-nsq"
)

func main() {
    //生成一个producer
    config := nsq.NewConfig()
    produce, err := nsq.NewProducer("127.0.0.1:4152", config)
    defer produce.Stop()
    if err != nil {
        panic(err)
    }

    topicName := "test"

    err = produce.Publish(topicName, []byte("a-msg"))
    if err != nil {
        log.Printf("%v\n", err)
    }

}

接收消息

package main

import (
    "log"
    "time"

    "github.com/nsqio/go-nsq"
)

func main() {
    //生成一个producer
    config := nsq.NewConfig()
    config.LookupdPollInterval = time.Second * 3

    topicName := "test"
    consumer, err := nsq.NewConsumer(topicName, "tt", config)
    //defer consumer.Stop()
    if err != nil {
        panic(err)
    }
    consumer.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error {
        log.Println("receive", m.NSQDAddress, "message:", string(m.Body))
        return nil
    }))

    address := "0.0.0.0:4161"
    if err = consumer.ConnectToNSQLookupd(address); err != nil {
        panic(err)
    }
    time.Sleep(time.Second * 10)
}

猜你喜欢

转载自blog.csdn.net/cabing2005/article/details/79640648
nsq