使用golang调用RocketMQ SDK

前言

本文档介绍使用go语言调用火山引擎RocketMQ SDK。

关于实验
  • 预计部署时间:30分钟
  • 级别:初级
  • 相关产品:中间件-RocketMQ
  • 受众: 通用
实验说明

第一步、创建RocketMQ实例

在控制台创建RocketMQ实例,并配置Topic、Group、以及秘钥,详见RocketMQ创建文档

第二步 、配置golang代码

/*
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())
	}
}

第三步、交叉编译代码并上传到ECS实例进行测试

linux

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go

Windows

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Mac

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]]
0
0
0
0
关于作者

文章

0

获赞

0

收藏

0

所属团队号:
相关资源
基于 Ray 的大模型离线推理
大模型离线推理,是指在具有数十亿或数万亿参数的大规模模型上进行分布式推理的过程。相较于常规模型推理,在模型切分、数据处理和数据流、提升 GPU 利用率方面面临了很大挑战。本次分享将介绍如何利用 Ray 及云原生优势助力大模型离线推理。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论