-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive.go
50 lines (41 loc) · 1.19 KB
/
receive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package nsq
import (
"github.com/hopeio/cherry/utils/log"
"github.com/nsqio/go-nsq"
)
// 消费者
type Consumer struct{}
// 主函数
// 处理消息
func (*Consumer) HandleMessage(msg *nsq.Message) error {
log.Info("receive", msg.NSQDAddress, "message:", string(msg.Body))
return nil
}
// 初始化消费者
func NewConsumer(topic string, channel string, handle nsq.HandlerFunc) {
cfg := nsq.NewConfig()
//cfg.LookupdPollInterval = time.Second //设置重连时间
c, err := nsq.NewConsumer(topic, channel, cfg) // 新建一个消费者
if err != nil {
panic(err)
}
//c.WithLogger(nil, 0) //屏蔽系统日志
c.AddHandler(handle) // 添加消费者接口
//建立NSQLookupd连接
if err := c.ConnectToNSQLookupd(Addr4161); err != nil {
log.Info("consumer 新建失败")
}
//建立多个nsqd连接
// if err := c.ConnectToNSQDs([]string{"127.0.0.1:4150", "127.0.0.1:4152"}); err != nil {
// panic(err)
// }
// 建立一个nsqd连接
/* if err := c.ConnectToNSQD(address); err != nil {
panic(err)
}
<-c.StopChan*/
}
func handleStringMessage(message *nsq.Message) error {
log.Info("handleStringMessage get a message %v\n\n", string(message.Body))
return nil
}