前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >go: nsq启动与部署

go: nsq启动与部署

作者头像
超级大猪
发布2021-06-17 17:28:00
5460
发布2021-06-17 17:28:00
举报
文章被收录于专栏:大猪的笔记大猪的笔记

单实例

启动服务

代码语言: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": ""
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-06-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 单实例
    • 启动服务
      • 启动admin
        • producer
          • consumer
          • api相关操作
            • 查看某个topic的相关数据
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档