golang rocketmq消费示例

Posted _雪辉_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang rocketmq消费示例相关的知识,希望对你有一定的参考价值。

package main

import (
	"context"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/consumer"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"os"
	"time"
	"log"
)

func main() 
	c, err := rocketmq.NewPushConsumer(
		consumer.WithGroupName("testGroup"),
	if err != nil 
		log.Printf(err.Error())
		return
	
	consumer.WithNsResolver(primitive.NewPassthroughResolver([]string"127.0.0.1:9876")),
	)
	topic := "trans"

	err := c.Subscribe(topic, consumer.MessageSelector, func(ctx context.Context,
		msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) 
		for i := range msgs 
			log.Printf("body=%v, tag =%v \\n", string(msgs[i].Body),msgs[i].GetTags())
		
		return consumer.ConsumeSuccess, nil
	)
	if err != nil 
		log.Printf(err.Error())
	
	err = c.Start()
	defer c.Shutdown()
	if err != nil 
		log.Printf(err.Error())
		os.Exit(-1)
	
	time.Sleep(2*time.Second)

以上是关于golang rocketmq消费示例的主要内容,如果未能解决你的问题,请参考以下文章

golang rocketmq消费示例

golang rocketmq消费示例

golang生产者消费者模型示例代码

golang生产者消费者模型示例代码

golang RabbitMQ重新连接功能的示例。包括恢复已注册的消费者。

[Golang] kafka集群搭建和golang版生产者和消费者