本文档介绍使用go语言调用火山引擎RocketMQ SDK。
在控制台创建RocketMQ实例,并配置Topic、Group、以及秘钥,详见RocketMQ创建文档
/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package main implements a producer with user custom interceptor. package main import ( "context" "fmt" "os" "github.com/apache/rocketmq-client-go/v2" "github.com/apache/rocketmq-client-go/v2/primitive" "github.com/apache/rocketmq-client-go/v2/producer" ) func main() { p, err := rocketmq.NewProducer( producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"Your TCP intranet access point"})),//此处填写控制台RocketMQ实例概览中的TCP内网接入点,目前不支持公网接入,示例http://MQ_INST_50392uo8m9em_xxxxx.rocketmq.ivolces.com:9876 producer.WithRetry(2), producer.WithCredentials(primitive.Credentials{ AccessKey: "your AccessKey", SecretKey: "your SecretKey", }), ) if err != nil { fmt.Println("init producer error: " + err.Error()) os.Exit(0) } err = p.Start() if err != nil { fmt.Printf("start producer error: %s", err.Error()) os.Exit(1) } for i := 0; i < 100000; i++ {MQ_INST_50392uo8m9em_8f96x res, err := p.SendSync(context.Background(), primitive.NewMessage("your topic", //示例:MQ_INST_50392uo8m9em_xxxxx%test test为创建的topic名称 MQ_INST_50392uo8m9em_xxxxx为实例ID []byte("Hello RocketMQ Go Client!"))) if err != nil { fmt.Printf("send message error: %s\n", err) } else { fmt.Printf("send message success: result=%s\n", res.String()) } } err = p.Shutdown() if err != nil { fmt.Printf("shutdown producer error: %s", err.Error()) } }
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
上传完成后,执行代码,显示结果如下。
send message success: result=SendResult [sendStatus=0, msgIds=C0A8005815C900000000226ee5480532, offsetMsgId=AC1C3961000078BF000000000029EE54, queueOffset=1268, messageQueue=MessageQueue [topic=MQ_INST_50392uo8m9em_8f96x%test, brokerName=brk-2c6afc0c-21e683427f49-223046fbe3aa, queueId=2]] send message success: result=SendResult [sendStatus=0, msgIds=C0A8005815C900000000226ee5480533, offsetMsgId=AC1C3961000078BF000000000029EF63, queueOffset=1267, messageQueue=MessageQueue [topic=MQ_INST_50392uo8m9em_8f96x%test, brokerName=brk-2c6afc0c-21e683427f49-223046fbe3aa, queueId=3]] send message success: result=SendResult [sendStatus=0, msgIds=C0A8005815C900000000226ee5480534, offsetMsgId=AC1C3961000078BF000000000029F072, queueOffset=1267, messageQueue=MessageQueue [topic=MQ_INST_50392uo8m9em_8f96x%test, brokerName=brk-2c6afc0c-21e683427f49-223046fbe3aa, queueId=4]]