We know that message queue is a commonly used architectural pattern to solve problems such as asynchronous processing and task distribution, and RabbitMQ is currently one of the most widely used message middleware. In practical applications, we may need to use Golang to implement RabbitMQ monitoring. This article will introduce how to use Golang to implement RabbitMQ monitoring.
Preparation
Before you start, you need to make sure that RabbitMQ has been installed. Since RabbitMQ depends on Erlang, Erlang also needs to be installed.
After the installation is complete, we need to install the Golang third-party package. Among them, the AMQP package is essential, which allows us to easily connect and operate RabbitMQ.
go get github.com/streadway/amqp
Code implementation
First, we need to connect to RabbitMQ. After the connection is successful, we need to declare an exchange named "test" and type "fanout". Exchange is an important part of message routing in RabbitMQ. It is responsible for receiving messages and distributing them to queues. In this case, we will declare an exchange called "test" and set its type to "fanout", which means that it will broadcast messages to all queues subscribed to it.
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn .Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
err = ch.ExchangeDeclare(
"test", // name "fanout", // type true, // durable false, // auto-deleted false, // internal false, // no-wait nil, // arguments
)
failOnError(err, "Failed to declare an exchange")
Next, we need to create a new, non-persistent, with Queue with automatically generated name. Here we will use the names of the queues to bind them to the "test" exchange we just declared.
q, err := ch.QueueDeclare(
"", // name false, // durable false, // delete when unused true, // exclusive false, // no-wait nil, // arguments
)
failOnError(err, "Failed to declare a queue")
err = ch.QueueBind(
q.Name, // queue name "", // routing key "test", // exchange false, nil,
)
failOnError(err, "Failed to bind a queue")
Now, RabbitMQ is ready and we can start listening for its messages. We can use the Consume function to implement message listening, which allows us to continuously receive messages from the queue and process them.
msgs, err := ch.Consume(
q.Name, // queue name "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args
)
failOnError(err, "Failed to register a consumer")
for msg := range msgs {
log.Printf("Received a message: %s", msg.Body)
}
In the above code, we use the ch.Consume() method to listen to the messages in the specified queue and output the message content by printing the log. It should be noted that we use an infinite loop to deploy message listening, which means that we will keep listening to the queue until the program is stopped or an error occurs.
The complete code is as follows:
package main
import (
"log" "github.com/streadway/amqp"
)
func failOnError(err error, msg string) {
if err != nil { log.Fatalf("%s: %s", msg, err) }
}
func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") failOnError(err, "Failed to connect to RabbitMQ") defer conn.Close() ch, err := conn.Channel() failOnError(err, "Failed to open a channel") defer ch.Close() err = ch.ExchangeDeclare( "test", // name "fanout", // type true, // durable false, // auto-deleted false, // internal false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare an exchange") q, err := ch.QueueDeclare( "", // name false, // durable false, // delete when unused true, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") err = ch.QueueBind( q.Name, // queue name "", // routing key "test", // exchange false, nil, ) failOnError(err, "Failed to bind a queue") msgs, err := ch.Consume( q.Name, // queue name "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) failOnError(err, "Failed to register a consumer") for msg := range msgs { log.Printf("Received a message: %s", msg.Body) }
}
Summary
This article introduces how to use Golang to implement rabbitmq To listen, first we need to connect to rabbitmq, declare an exchange, create a queue and bind the queue to the exchange, and finally use a consumer to listen to the messages in the queue. I hope this article can be helpful to developers who are using Golang for rabbitmq development.
The above is the detailed content of Golang implements rabbitmq monitoring. For more information, please follow other related articles on the PHP Chinese website!