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

Implementing a simple timer in JavaScript

This article example shares the specific code of ...

Complete steps for vue dynamic binding icons

0 Differences between icons and images Icons are ...

MySQL optimization query_cache_limit parameter description

query_cache_limit query_cache_limit specifies the...

How to modify the root password of mysql under Linux

Preface The service has been deployed on MySQL fo...

A small question about the execution order of SQL in MySQL

I encountered a sql problem at work today, about ...

Example of how to configure cross-domain failure repair in nginx

Nginx cross-domain configuration does not take ef...

Use of kubernetes YAML files

Table of contents 01 Introduction to YAML files Y...

Detailed explanation of vue page state persistence

Table of contents Code: Replenish: Summarize Requ...

Solution to the timeout problem when installing docker-compose with PIP

1: Installation command pip install docker-compos...

How to add links to FLASH in HTML and make it compatible with all major browsers

Look at the code first Copy code The code is as fo...

CSS animation combined with SVG to create energy flow effect

The final effect is as follows: The animation is ...

MySQL 5.7 installation and configuration tutorial under CentOS7 (YUM)

Installation environment: CentOS7 64-bit, MySQL5....

Implementation of Nginx filtering access logs of static resource files

Messy log Nginx in daily use is mostly used as bo...

Summary of how to modify the root password in MySQL 5.7 and MySQL 8.0

MySQL 5.7 version: Method 1: Use the SET PASSWORD...