RabbitMQ in Docker
Docker
Python
Go
Supporting repo is here. (Includes a Celery example)
If you don’t already have Docker installed, you can find instructions here.
Setup and Run
Pull the RabbitMQ docker container:
docker pull rabbitmq
Startup for RabbitMQ docker container:
sudo docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 rabbitmq:3
The port mapping (5672:5672) is not included in the instructions on Docker Hub, but it’s required for the Python send/receive scripts to work.
Simple Test in Python
You’ll need to install the Pika library before you run the send/receive scripts:
sudo pip3 install pika --upgrade
Python script to send a message:
send.py
#!/usr/bin/env python3
import pika
= pika.BlockingConnection(pika.ConnectionParameters('localhost'))
connection = connection.channel()
channel
='hello')
channel.queue_declare(queue
='', routing_key='hello', body='Hello World!')
channel.basic_publish(exchangeprint(" [x] Sent 'Hello World!'")
connection.close()
Python script to receive messages:
receive.py
#!/usr/bin/env python3
import pika
= pika.BlockingConnection(pika.ConnectionParameters('localhost'))
connection = connection.channel()
channel
='hello')
channel.queue_declare(queue
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
='hello', on_message_callback=callback, auto_ack=True)
channel.basic_consume(queue
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
Simple Test in Go
First, install amqp using go get:
go get github.com/streadway/amqp
Then, use this to send a message:
send.go
package main
import (
"log"
"github.com/streadway/amqp"
)
func failOnError(err error, msg string) {
if err != nil {
.Fatalf("%s: %s", msg, err)
log}
}
func main() {
, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
conn(err, "Failed to connect to RabbitMQ")
failOnErrordefer conn.Close()
, err := conn.Channel()
ch(err, "Failed to open a channel")
failOnErrordefer ch.Close()
, err := ch.QueueDeclare(
q"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
(err, "Failed to declare a queue")
failOnError
:= "Hello World!"
body = ch.Publish(
err "", // exchange
.Name, // routing key
qfalse, // mandatory
false, // immediate
.Publishing{
amqp: "text/plain",
ContentType: []byte(body),
Body})
.Printf(" [x] Sent %s", body)
log(err, "Failed to publish a message")
failOnError}
And use this to receive messages:
receive.go
package main
import (
"log"
"github.com/streadway/amqp"
)
func failOnError(err error, msg string) {
if err != nil {
.Fatalf("%s: %s", msg, err)
log}
}
func main() {
, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
conn(err, "Failed to connect to RabbitMQ")
failOnErrordefer conn.Close()
, err := conn.Channel()
ch(err, "Failed to open a channel")
failOnErrordefer ch.Close()
, err := ch.QueueDeclare(
q"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
(err, "Failed to declare a queue")
failOnError
, err := ch.Consume(
msgs.Name, // queue
q"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
(err, "Failed to register a consumer")
failOnError
:= make(chan bool)
forever
go func() {
for d := range msgs {
.Printf("Received a message: %s", d.Body)
log}
}()
.Printf(" [*] Waiting for messages. To exit press CTRL+C")
log<-forever
}