Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code for golang to operate rocketmq

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要介绍"golang操作rocketmq的代码怎么写",在日常操作中,相信很多人在golang操作rocketmq的代码怎么写问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"golang操作rocketmq的代码怎么写"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

代码

func main() { // 1. 创建主题 //CreateTopic("test-04", 10909) // 2. 生产者向主题中发送消息 //SendSyncMessage("hello world0002") // 3. 消费者订阅主题并消费 SubscribeMessage()}func CreateTopic(topicName string, port int) { // 创建主题 testAdmin, err := admin.NewAdmin(admin.WithResolver(primitive.NewPassthroughResolver([]string{"ip:server_port"}))) if err != nil { fmt.Println(err) } err = testAdmin.CreateTopic( context.Background(), admin.WithTopicCreate(topicName), admin.WithBrokerAddrCreate(fmt.Sprintf("ip:%d", port)), ) fmt.Println(err)func SendSyncMessage(message string) { endPoint := []string{"ip:server_port"} p, err := rocketmq.NewProducer( producer.WithNameServer(endPoint), //producer.WithNsResolver(primitive.NewPassthroughResolver(endPoint)), producer.WithRetry(2), err = p.Start() result, err := p.SendSync(context.Background(), &primitive.Message{ Topic: "test", Body: []byte(message), }) fmt.Println(result.Status, result)func SubscribeMessage() { // 订阅主题、并消费 c, err := rocketmq.NewPushConsumer( consumer.WithNameServer(endPoint), consumer.WithConsumerModel(consumer.Clustering), consumer.WithGroupName("GID_TEST01"), //fmt.Println(err) err = c.Subscribe("test", consumer.MessageSelector{}, func(ctx context.Context, msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) { for i := range msgs { fmt.Printf("subscribe callback: %v \n", msgs[i]) } return consumer.ConsumeSuccess, nil //fmt.Println(err.Error()) // Note: start after subscribe err = c.Start() os.Exit(-1) c.Shutdown()到此,关于"golang操作rocketmq的代码怎么写"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report