单实例
启动服务
代码语言:javascript复制./nsqd -mem-queue-size 5000
启动admin
代码语言:javascript复制./nsqadmin --nsqd-http-address 127.0.0.1:4151
producer
代码语言:javascript复制package main
import (
"fmt"
"github.com/nsqio/go-nsq"
"github.com/sirupsen/logrus"
)
func main() {
url := "127.0.0.1:4150"
producer, err := nsq.NewProducer(url, nsq.NewConfig())
if err != nil {
logrus.Fatal(err)
}
i := 0
for {
i
msg := []byte(fmt.Sprintf("hello:%v", i))
err := producer.Publish("test", msg)
if err != nil {
logrus.Errorf("publish error:%v", err)
}
logrus.Infof("write:%s", msg)
// time.Sleep(time.Millisecond * 1)
if i == 11000 {
producer.Stop()
break
}
}
}
consumer
代码语言:javascript复制package main
import (
"log"
"time"
"github.com/nsqio/go-nsq"
)
func main() {
cfg := nsq.NewConfig()
cfg.LookupdPollInterval = time.Second //设置重连时间
c, err := nsq.NewConsumer("test", "test-channel", cfg) // 新建一个消费者
if err != nil {
log.Fatal(err)
}
c.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
log.Printf("consume:%s %s", message.ID, message.Body)
return nil
}))
if err := c.ConnectToNSQDs([]string{"127.0.0.1:4150"}); err != nil {
log.Printf("ConnectToNSQDs error:%v", err)
}
<-c.StopChan
}
api相关操作
查看某个topic的相关数据
代码语言:javascript复制http://127.0.0.1:4171/api/topics/test
回包
代码语言:javascript复制{
"node": "*",
"hostname": "",
"topic_name": "test",
"depth": 0,
"memory_depth": 0,
"backend_depth": 0,
"message_count": 0,
"nodes": [
{
"node": "honoryin-LC3:4151",
"hostname": "honoryin-LC3",
"topic_name": "test",
"depth": 0,
"memory_depth": 0,
"backend_depth": 0,
"message_count": 0,
"nodes": null,
"channels": [
{
"node": "honoryin-LC3:4151",
"hostname": "honoryin-LC3",
"topic_name": "test",
"channel_name": "test-channel",
"depth": 0,
"memory_depth": 0,
"backend_depth": 0,
"in_flight_count": 0,
"deferred_count": 0,
"requeue_count": 0,
"timeout_count": 0,
"message_count": 0,
"client_count": 0,
"nodes": null,
"clients": null,
"paused": false,
"e2e_processing_latency": {
"count": 0,
"percentiles": null,
"topic": "",
"channel": "",
"host": ""
}
}
],
"paused": false,
"e2e_processing_latency": {
"count": 0,
"percentiles": null,
"topic": "",
"channel": "",
"host": ""
}
}
],
"channels": [
{
"node": "honoryin-LC3:4151",
"hostname": "honoryin-LC3",
"topic_name": "test",
"channel_name": "test-channel",
"depth": 0,
"memory_depth": 0,
"backend_depth": 0,
"in_flight_count": 0,
"deferred_count": 0,
"requeue_count": 0,
"timeout_count": 0,
"message_count": 0,
"client_count": 0,
"nodes": null,
"clients": null,
"paused": false,
"e2e_processing_latency": {
"count": 0,
"percentiles": null,
"topic": "",
"channel": "",
"host": ""
}
}
],
"paused": false,
"e2e_processing_latency": {
"count": 0,
"percentiles": null,
"topic": "test",
"channel": "",
"host": "*"
},
"message": ""
}