nsq

package
v1.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 4 Imported by: 0

README

make by Iori

#nsq git地址:

github.com/nsqio/go-nsq
生产者示例
package main

import (
"github.com/nsqio/go-nsq"
"log"
"math/rand"
"time"
)

func main() {
config := nsq.NewConfig()
w, err := nsq.NewProducer("127.0.0.1:4150", config)

	if err != nil {
		log.Panic(err)
	}

	chars := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

	for {
		buf := make([]byte, 4)
		for i := 0; i < 4; i++ {
			buf[i] = chars[rand.Intn(len(chars))]
		}
		log.Printf("Pub: %s", buf)
		err = w.Publish("test", buf)
		if err != nil {
			log.Panic(err)
		}
		time.Sleep(time.Second * 1)
	}

	w.Stop()
}

##消费者示例

package main

import (
"log"
"sync"

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

func main() {

	wg := &sync.WaitGroup{}
	wg.Add(1000)

	config := nsq.NewConfig()
	q, _ := nsq.NewConsumer("test", "ch", config)
	q.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
		log.Printf("Got a message: %s", message.Body)
		wg.Done()
		return nil
	}))
	err := q.ConnectToNSQD("127.0.0.1:4150")
	if err != nil {
		log.Panic(err)
	}
	wg.Wait()

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConsumer

func NewConsumer(topic, channel, addr string, lookupAddr []string, concurrency int, cb MsgHandler) (*nsq.Consumer, error)

* * @Description: 创建消费者实例 * @param: topic * @param: channel * @param: lookupAddr * @param: addr * @param: cb * @return: *nsq.Consumer * @return: error * @Author: Iori * @Date: 2022-04-25 14:24:32 *

func NewConsumerWithOpts added in v1.2.5

func NewConsumerWithOpts(topic, channel, addr string, lookupAddr []string, concurrency, maxInFlight int, lookupdPollInterval time.Duration, cb MsgHandler) (*nsq.Consumer, error)

NewConsumerWithOpts 创建消费者实例,支持配置 MaxInFlight 和 LookupdPollInterval。 maxInFlight<=0 时用默认值 2;lookupdPollInterval<=0 时用默认值 3s。

func NewProducer

func NewProducer(addr string) (*nsqProducer, error)

* * @Description: 初始化生产者 * @param: addr * @return: *nsqProducer * @return: error * @Author: Iori * @Date: 2022-04-22 14:05:53 *

Types

type ConsumerHandler

type ConsumerHandler struct {
	// contains filtered or unexported fields
}

* nsqHandler * @Description: handler

func (*ConsumerHandler) HandleMessage

func (ch *ConsumerHandler) HandleMessage(msg *nsq.Message) error

* * @Description: 处理消息函数 * @receiver: nh * @param: msg * @return: error * @Author: Iori * @Date: 2022-04-22 12:02:04 *

type MsgHandler

type MsgHandler func(srcBusID uint32, data []byte) error

cb handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL