我在项目中使用NSQ作为mq,java生产者将消息发送给NSQ并让消费者使用它。但奇怪的是,消费者总是在几秒钟后收到消息。只有几条消息,我真的不知道如何解释它为什么发生。 这是测试结果,请注意时间。它们都消耗相同的主题。第二次您可以看到,Go比Java慢7s。 Java结果:
INFO | jvm 1 | 2018/07/11 17:22:01 |讯息 接收:{ “做”: “XSQ000200000005”, “MSG”:{ “ID”: “5560”, “类型”:1, “内容”: “ZBINh6CBsLw7k2xjr1wslSjY + 5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB + / SpsxZQnrWR9PlULzpSP / p9l9t8wiAwj8qhznRaT8jeyx1 / EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo =”}} >
INFO | jvm 1 | 2018/07/11 17:22:11 |讯息 接收:{ “做”: “XSQ000200000005”, “MSG”:{ “ID”: “5560”, “类型”:1, “内容”: “ZBINh6CBsLw7k2xjr1wslSjY + 5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB + / SpsxZQnrWR9PlULzpSP / p9l9t8wiAwj8qhznRaT8jeyx1 / EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo =”}} >
INFO | jvm 1 | 2018/07/11 17:23:21 |讯息 接收:{ “做”: “XSQ000200000005”, “MSG”:{ “ID”: “5560”, “类型”:1, “内容”: “ZBINh6CBsLw7k2xjr1wslSjY + 5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB + / SpsxZQnrWR9PlULzpSP / p9l9t8wiAwj8qhznRaT8jeyx1 / EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo =”}} >
INFO | jvm 1 | 2018/07/11 17:25:31 |讯息 接收:{ “做”: “XSQ000200000005”, “MSG”:{ “ID”: “5560”, “类型”:1, “内容”: “ZBINh6CBsLw7k2xjr1wslSjY + 5QavEgYU6AzzLZn0lOgON9ZYHnNP4UJVUGB + / SpsxZQnrWR9PlULzpSP / p9l9t8wiAwj8qhznRaT8jeyx1 / EUrDE0oXJB8GxWaLJUICCbC92j4BMA2HU8vgcfDOp9nSy1KFafi9zgFiCf9Igqo =”}} >
转到结果:
2018-07-11 17:22:03 broker.go调试准备好将类型1的味精5560发送到XSQ000200000005
2018-07-11 17:22:28 broker.go调试准备好将类型1的味精5560发送到XSQ000200000005
2018-07-11 17:23:21 broker.go调试准备好将类型1的味精5560发送到XSQ000200000005
2018-07-11 17:25:38 broker.go调试准备将类型1的味精5560发送到XSQ000200000005
仅出于业务原因,请忽略其他错误。
这是我的常客:
func (b *Broker) createConsumer(topic string, vendor int32) error {
config := nsq.NewConfig()
laddr := "127.0.0.1"
// so that the test can simulate binding consumer to specified address
config.LocalAddr, _ = net.ResolveTCPAddr("tcp", laddr+":0")
// so that the test can simulate reaching max requeues and a call to LogFailedMessage
config.DefaultRequeueDelay = 0
// so that the test wont timeout from backing off
config.MaxBackoffDuration = time.Millisecond * 50
c, err := nsq.NewConsumer(topic, "channel_box_" + util.String(vendor), config)
if err != nil {
return log.Error("Failed to new nsq consumers.")
}
c.AddConcurrentHandlers(nsq.HandlerFunc(func(message *nsq.Message) error {
if err := b.handle(message, vendor); err != nil {
log.Errorf("Handle message %v for vendor %d from mq failed.", message.ID, vendor)
}
return nil
}), 5)
if err = c.ConnectToNSQLookupds(b.Opts.Nsq.Lookup); err != nil {
return log.Error("Failed to connect to nsq lookup server.")
}
b.consumers = append(b.consumers, c)
return nil
}