tencent cloud

TDMQ for RabbitMQ

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Introduction and Selection of the TDMQ Product Series
What Is TDMQ for RabbitMQ
Strengths
Use Cases
Description of Differences Between Managed Edition and Serverless Edition
Open-Source Version Support Description
Comparison with Open-Source RabbitMQ
High Availability
Use Limits
TDMQ for RabbitMQ-Related Concepts
Regions
Related Cloud Services
Billing
Billing Overview
Pricing
Billing Example
Convert to Monthly Subscription from Hourly Postpaid
Renewal
Viewing Consumption Details
Overdue Payments
Refund
Getting Started
Getting Started Guide
Step 1: Preparations
Step 2: Creating a RabbitMQ Cluster
Step 3: Configuring a Vhost
Step 4: Using the SDK to Send and Receive Messages
Step 5: Querying a Message
Step 6: Deleting Resources
User Guide
Usage Process Guide
Configuring the Account Permission
Creating a Cluster
Configuring a Vhost
Connecting to the Cluster
Managing Messages
Configure Advanced Feature
Managing the Cluster
Viewing Monitoring Data and Configuring Alarm Policy
Use Cases
Use Instructions of Use Cases
RabbitMQ Client Use Cases
RabbitMQ Message Reliability Use Cases
Usage Instructions for MQTT Protocol Supported by RabbitMQ
Migrate Cluster
Migrating RabbitMQ to Cloud
Step 1. Purchasing a TDMQ Instance
Step 2: Migrating Metadata to the Cloud
Step 3: Enabling Dual Read-Write
API Reference (Managed Edition)
API Overview
API Reference (Serverless Edition)
History
Introduction
API Category
Making API Requests
Relevant APIs for RabbitMQ Serverless PAAS Capacity
RabbitMQ Serverless Instance Management APIs
Data Types
Error Codes
SDK Documentation
SDK Overview
Spring Boot Starter Integration
Spring Cloud Stream Integration
Java SDK
Go SDK
Python SDK
PHP SDK
Security and Compliance
Permission Management
Network Security
Deletion Protection
Change Records
CloudAudit
FAQs
Service Level Agreement
Contact Us

Go SDK

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-01-05 11:20:19

Scenarios

This document introduces how to use an open-source SDK (taking the Go SDK as an example) to send and receive messages, so as to help you better understand the complete process of sending and receiving messages.

Prerequisites

You have obtained the related client connection parameters as instructed in SDK Overview.

Operation Steps

1. Run the following command to install the required packages in the client environment.
go get "github.com/rabbitmq/amqp091-go"
2. After the installation is completed, it can be imported into your GO project file.
import (amqp "github.com/rabbitmq/amqp091-go")
3. After import, the client can be used in your project.

Usage Examples

1. Establish a connection and a communication channel.
// Required parameters.
const (
// Host: Select Cluster Details and go to the client access page to copy the access point.
// For example, amqp://1.1.1.1:5672. You only need to enter the IP address.
Host = "1.1.1.1"
// UserName: username. It is required to create the user first in the console, or use the admin account on the cluster details - web console access address page.
UserName = "test"
// Password: user password. It is required to create the password first in the console.
Password = "test"
// Vhost: Enter the custom vhost. It is required to create the vhost first in the console.
Vhost = "test"
)
// Create a connection.
conn, err := amqp.Dial("amqp://" + UserName + ":" + Password + "@" + Host + ":5672/" + Vhost)
failOnError(err, "Failed to connect to RabbitMQ")
defer func(conn *amqp.Connection) {
err := conn.Close()
if err != nil {
}
}(conn)

// Establish a channel.
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer func(ch *amqp.Channel) {
err := ch.Close()
if err != nil {
}
}(ch)
Parameter
Description
host
Cluster access address, which can be obtained from the Client Access module on the basic cluster information page.
username
Username. Enter the username created in the console.
password
User password. Enter the password specified during user creation in the console.
vhost
Vhost name, which can be obtained from the vhost list in the console.
2. Declare an exchange.
// Declare an exchange (the name and type of it should be consistent with those of the existing exchanges).
err = ch.ExchangeDeclare(
"logs-exchange", // Exchange name.
"fanout", // Exchange type.
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare a exchange")
3. Publish a message. Messages can be sent to an exchange or directly to the specified queue (through the Hello World and Work Queues message models).
Publish a message to an exchange:
// Message content.
body := "this is new message."
// Publish a message to an exchange.
err = ch.Publish(
"logs-exchange", // exchange
"", // Routing Key. (select whether the Routing Key is required based on the used exchange type). If no exchange is selected, this parameter is the message queue name.
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")

Publish a message to the specified queue:
// Publish a message to the specified message queue.
err = ch.Publish(
"", // exchange
"queue.Name", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")
4. Subscribe to a message.
// Create a consumer and consume messages in the specified message queue.
msgs, err := ch.Consume(
"message-queue", // message-queue
"", // consumer
false, // Set to non-automatic acknowledgment (it can be selected as needed).
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "Failed to register a consumer")

// Obtain messages in the message queue.
forever := make(chan bool)
go func() {
for d := range msgs {
log.Printf("Received a message: %s", d.Body)
t := time.Duration(1)
time.Sleep(t * time.Second)
// Manually reply with acknowledgment.
d.Ack(false)
}
}()
log.Printf(" [Consumer] Waiting for messages.")
<-forever
5. The consumer uses a Routing Key.
// It is required to specify the exchange and Routing Key in the message queue.
err = ch.QueueBind(
"q.Name", // queue name
"routing_key", // routing key
"topic_demo", // exchange
false,
nil,
)
failOnError(err, "Failed to bind a queue")
Note
For detailed usage examples, see Demo or the RabbitMQ official website.


Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan