1. Get the image #Specify the version that includes the web control page docker pull rabbitmq:management 2. Run the image #Method 1: Default guest user, password is also guest docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq:management #Method 2: Set username and password docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password -p 15672:15672 -p 5672:5672 rabbitmq:management 3. Access the UI page http://localhost:15672/ 4. Golang Case #producer producer code package main import ( "fmt" "log" "github.com/streadway/amqp" ) const ( //AMQP URI uri = "amqp://guest:[email protected]:5672/" // 10.0.0.11 is the host IP //Durable AMQP exchange name exchangeName = "" //Durable AMQP queue name queueName = "test-queues" //Body of message bodyMsg string = "hello angel" ) //If there is an error, then output func failOnError(err error, msg string) { if err != nil { log.Fatalf("%s: %s", msg, err) panic(fmt.Sprintf("%s: %s", msg, err)) } } func main() { //Call the publish message function publish(uri, exchangeName, queueName, bodyMsg) log.Printf("published %dB OK", len(bodyMsg)) } //Publisher method //@amqpURI, amqp address //@exchange, exchange name //@queue, queue name //@body, body content func publish(amqpURI string, exchange string, queue string, body string) { //Establish connection log.Printf("dialing %q", amqpURI) connection, err := amqp.Dial(amqpURI) failOnError(err, "Failed to connect to RabbitMQ") defer connection.Close() //Create a Channel log.Printf("got Connection, getting Channel") channel, err := connection.Channel() failOnError(err, "Failed to open a channel") defer channel.Close() log.Printf("got queue, declaring %q", queue) //Create a queue q, err := channel.QueueDeclare( queueName, // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") log.Printf("declared queue, publishing %dB body (%q)", len(body), body) // Producer can only send to the exchange, it cannot send directly to the queue // Now we use the default exchange (name is empty character) this default exchange allows us to send to the specified queue // routing_key is the specified queue name err = channel.Publish( exchange, // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ Headers: amqp.Table{}, ContentType: "text/plain", ContentEncoding: "", Body: []byte(body), }) failOnError(err, "Failed to publish a message") } 5. Own message confirmation code #producer package main import ( "fmt" "github.com/streadway/amqp" "log" "os" "strings" ) const ( //AMQP URI uri = "amqp://guest:[email protected]:5672/" //Durable AMQP exchange name exchangeName = "" //Durable AMQP queue name queueName = "test-queues-acknowledgments" ) //If there is an error, then output func failOnError(err error, msg string) { if err != nil { log.Fatalf("%s: %s", msg, err) panic(fmt.Sprintf("%s: %s", msg, err)) } } func main() { bodyMsg := bodyFrom(os.Args) //Call the publish message function publish(uri, exchangeName, queueName, bodyMsg) log.Printf("published %dB OK", len(bodyMsg)) } func bodyFrom(args []string) string { var s string if (len(args) < 2) || os.Args[1] == "" { s = "hello angel" } else { s = strings.Join(args[1:], " ") } returns } //Publisher method //@amqpURI, amqp address //@exchange, exchange name //@queue, queue name //@body, body content func publish(amqpURI string, exchange string, queue string, body string) { //Establish connection log.Printf("dialing %q", amqpURI) connection, err := amqp.Dial(amqpURI) failOnError(err, "Failed to connect to RabbitMQ") defer connection.Close() //Create a Channel log.Printf("got Connection, getting Channel") channel, err := connection.Channel() failOnError(err, "Failed to open a channel") defer channel.Close() log.Printf("got queue, declaring %q", queue) //Create a queue q, err := channel.QueueDeclare( queueName, // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") log.Printf("declared queue, publishing %dB body (%q)", len(body), body) // Producer can only send to exchange, it cannot be sent directly to queue. // For now we use the default exchange (with an empty string as its name). This default exchange allows us to send to a specific queue. // routing_key is the specified queue name. err = channel.Publish( exchange, // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ Headers: amqp.Table{}, ContentType: "text/plain", ContentEncoding: "", Body: []byte(body), }) failOnError(err, "Failed to publish a message") } This is the end of this article about the steps to quickly install rabbitmq with docker. For more information about installing rabbitmq with docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A brief discussion on MySql views, triggers and stored procedures
>>: Mac node deletion and reinstallation case study
Preface: Based on a recent medical mobile project...
Use JS timer to make an element to make a method ...
Table of contents Preface 1. Technical Principle ...
To install a virtual machine on a Windows system,...
How Nginx works Nginx consists of a core and modu...
Table of contents 1. Signal List 1.1. Real-time s...
Configuration Preface Project construction: built...
This article summarizes the knowledge points of M...
The same server simulates the master-slave synchr...
Table of contents Overview Object rest attribute ...
Table of contents Million-level data processing s...
background The company code is provided to third ...
In actual Web development, inserting images, incl...
There is a table user, and the fields are id, nic...
Table of contents Scene Setting Game Resources Tu...