tencent cloud

SDK for Go
Terakhir diperbarui:2025-04-29 10:08:24
SDK for Go
Terakhir diperbarui: 2025-04-29 10:08:24

Overview

This document describes how to use open-source SDK to send and receive messages by using the SDK for Go as an example and helps you better understand the message sending and receiving processes.

Prerequisites

Directions

1. Run the following command to install the required package in the client environment:
go get "github.com/rabbitmq/amqp091-go"
2. After the installation is completed, import the package to your Go project file.
import (amqp "github.com/rabbitmq/amqp091-go")
After the import, you can use the client in your project.

Samples

1. Establish the connection and communication channel.
// Required parameters
const (
host = "amqp-xx.rabbitmq.x.tencenttdmq.com" // Service access address
username = "roleName" // Role name in the console
password = "eyJrZX..." // Role key
vhost = "amqp-xx|Vhost" // Full name of the vhost to be used
)
// 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, obtained from the Client Access module on the cluster basic information page.

username
Username, fill in the username created in the console.
password
User password, fill in the password when creating a user in the console.
vhost
Vhost Name, obtain it from the vhost list on the console.
2. Declare the exchange.
// Declare the exchange (the name and type must be the same as those of the existing exchange)
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 messages.
Messages can be directly sent to the exchange or the specified queue (hello world and work).
Publish messages to the exchange:
// Message content
body := "this is new message."
// Publish messages to the exchange
err = ch.Publish(
"logs-exchange", // exchange
"", // Routing key (set whether the routing key is required based on the used exchange type). If exchange is not selected, this parameter will be the message queue name
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")
Publish messages to the specified queue:
// Publish messages 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 messages.
// Create a consumer and consume messages in the specified message queue
msgs, err := ch.Consume(
"message-queue", // message-queue
"", // consumer
false, // Set to manual acknowledgment as needed
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "Failed to register a consumer")

// Get 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 return the acknowledgment
d.Ack(false)
}
}()
log.Printf(" [Consumer] Waiting for messages.")
<-forever
5. The consumer uses the routing key.
// You need 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 samples, see Demo or RabbitMQ Tutorials.

Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
Ya
Tidak

masukan