How to quickly install RabbitMQ in Docker

How to quickly install RabbitMQ in Docker

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:
  • Detailed operations of building RabbitMq's common cluster and mirror cluster with Docker
  • A detailed introduction to deploying RabbitMQ environment with docker
  • Two problems encountered when deploying rabbitmq with Docker
  • The problem of being unable to enter the management page when installing rabbitmq in docker
  • Docker deployment RabbitMQ container implementation process analysis
  • Docker installs and runs the rabbitmq example code
  • How to deploy rabbitmq cluster with docker
  • How to build a rabbitmq cluster environment with docker
  • Docker installation and configuration steps for RabbitMQ

<<:  A brief discussion on MySql views, triggers and stored procedures

>>:  Mac node deletion and reinstallation case study

Recommend

A brief discussion on the perfect adaptation solution for Vue mobile terminal

Preface: Based on a recent medical mobile project...

Using JS timer to move elements

Use JS timer to make an element to make a method ...

How to generate Vue user interface by dragging and dropping

Table of contents Preface 1. Technical Principle ...

Win10 installation Linux system tutorial diagram

To install a virtual machine on a Windows system,...

Detailed explanation of how Nginx works

How Nginx works Nginx consists of a core and modu...

A brief discussion on Linux signal mechanism

Table of contents 1. Signal List 1.1. Real-time s...

Summary of MySql index, lock, and transaction knowledge points

This article summarizes the knowledge points of M...

Detailed explanation of TS object spread operator and rest operator

Table of contents Overview Object rest attribute ...

MySQL data duplicate checking and deduplication implementation statements

There is a table user, and the fields are id, nic...

Detailed explanation of making shooting games with CocosCreator

Table of contents Scene Setting Game Resources Tu...