RabbitMQ Tutorial (1) -From Installation to Hello World (Official Site Translation)

I am learning RabbitMQ, which is a representative of open source MQ. For the time being, I tried Tutorial on the official website, so I translated it for my own understanding. I hope it helps you.

Preparation

This chapter is not in the tutorial on the official website, but I will write the installation procedure first.

Install RabbitMQ

Check Documents on the official website. The following is the procedure for Ubuntu.

--Add the following entry to /etc/apt/sources.list:

deb http://www.rabbitmq.com/debian/ testing main

--Register public key

wget https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
sudo apt-key add rabbitmq-signing-key-public.asc

--Update the package list with apt-get update

sudo apt-get update

--Installation

sudo apt-get install rabbitmq-server

Install the Python library'pika'

You can install it with pip.

sudo pip install pika

When you're ready, let's get into the tutorial.

Tutorial 1: Hello World

introduction

Rabbit MQ is a message broker. The main idea is very thimble, "receive and forward messages". You can think of it as a post office: when you post a letter to a mailbox, you are confident that a post office courier will eventually deliver the letter to its destination. Applying the role of RabbitMQ to this analogy, it plays the role of post, post office, and delivery person.

The difference between RabbitMQ and the post office is that it deals with chunks of data, or messages, rather than paper. RabbitMQ receives it, stores it, and transfers it.

RabbitMQ and common messaging systems use some jargon.

--Producing means send. The program that sends is called Producer (hereinafter referred to as "producer" in katakana). We will use the abbreviation "P" in the illustrations of the programs that will appear in future tutorials. producer.png

--queue is like a mailbox that stores letters (hereinafter referred to as "queue" in katakana). It works inside Rabbit MQ. When your application sends a message via Rabbit MQ, it's just queued. You can store as many messages as you like in a queue-essentially an infinite buffer. Multiple producers can send messages to one queue, and multiple consumers can receive messages from one queue. In the illustrations in future tutorials, we will use the following illustration with the queue name added above. queue.png

--Consuming means close to receiving. A program that waits for a message to be received is called Consumer (hereinafter referred to as "consumer" in katakana). In the illustrations in the tutorial, we will use the abbreviation "C". consumer.png

Keep in mind that producers, consumers and brokers (RabbitMQ servers) do not have to live together on the same machine. In fact, many applications configure them to be placed separately.

Hello World!

This Hello World program ray isn't too complicated. Let's send a message and print it on the screen. To achieve this, you need two programs: one that sends the message and one that receives and outputs the message.

The overall design looks like this: python-one-overall.png

Producer (P) sends a message to the "Hello" queue. The consumer (C) receives the message from the queue.

__ About the RabbitMQ library: __ RabbitMQ uses AMQP 0.9.1 which is an open and general-purpose messaging protocol for communication. Clients that support RabbitMQ are available in a variety of languages. The pika used in this tutorial is the Python client recommended by the RabbitMQ team.

Send

sending.png Create the send program send.py. (full code) The first thing to do is to establish a connection with the RabbitMQ server:

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
               'localhost'))
channel = connection.channel()

In this example, we are connecting to the local host broker (RabbitMQ server). If you want to connect to the broker on an external machine, simply specify the IP address or host name in the localhost part.

The destination queue must already exist when sending a message. If you send a message to a queue that doesn't exist, RabbitMQ will drop the message. This time, let's create this queue by sending it to a queue named Hello:

channel.queue_declare(queue='hello')

Now you are ready to send a message. I would like to send a message containing only the word Hello World! To the Hello queue.

In RabbitMQ, messages are always sent via a component called exchange, rather than being sent directly to the queue, but I won't go into the details right now (I'll cover it in Tutorial 3). For now, let's learn how to use the default exchange by specifying an empty string (''). This exchange is special and allows you to directly specify which queue to send the message to. The name of the destination queue is specified in the routing_key parameter:

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")

Before exiting the program, you need to make sure that the network buffer is flushed and the message is properly sent to RabbitMQ. You can do those things and close the connection with the code below.

connection.close()

__ Sending does not work __ If this is your first time using RabbitMQ and you're having trouble sending messages, you'll be wondering what's wrong. Perhaps the broker (RabbitMQ server) is starting up without enough disk space (minimum 1GB of space is required). In this case RabbitMQ will not accept the message. Check the log file and lower the minimum required disk space limit setting (disk_free_limit) if necessary. The configuration file documentation (http://www.rabbitmq.com/configure.html#config-items) describes how to configure it.

Receive

receiving.png The second program, receive.py, receives the message from the queue and outputs it to the screen. (full code)

First, connect to the RabbitMQ server. This code is similar to send.py.

Then call queue_declare to make sure there is a queue to use. This method guarantees idempotency and does nothing if the specified queue already exists. Therefore, you can call as many times as you like. (Only one queue with the same name will be created)

channel.queue_declare(queue='hello')

You may be wondering why you want to declare the queue again in the send.py code. The reason is to make sure that the queue doesn't exist. If you always launch send.py first, there is no problem (if you declare the queue only with send.py), but we have decided which one to launch first, send.py or receive.py. not. In these cases, it is a good practice to write a queue declaration (queue_declare) in both programs.

__ Queue Listing __ You may want to see how many queues your RabbitMQ has and how many messages you have. In that case, run the rabbitmqctl tool as an authorized user:

$ sudo rabbitmqctl list_queues Listing queues ... hello 0 ...done.

Receiving messages from the queue is a bit complicated. You can move the process by registering the callback function in the queue. Whenever a message is received, the callback function is called from the Pika library. This time, this function prints the contents of the message to the screen.

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

Then tell RabbitMQ to call this callback when it receives a message from hello:

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

This command will not succeed if there is no queue to register. Fortunately, we mentioned queue_declare before this, so we can be confident that the queue exists. The no_ack parameter is discussed in Tutorial 2. Finally, we enter an infinite loop that listens for data and drives callbacks:

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

Whole code

send.py


#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

receive.py


#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

From the terminal, try the program. First, run send.py to send a message:

 $ python send.py
 [x] Sent 'Hello World!'

The producer program send.py will exit as soon as you send it. Now let's receive the message:

 $ python receive.py
 [*] Waiting for messages. To exit press CTRL+C
 [x] Received 'Hello World!'

Hurray! You have successfully sent and received your first message using RabbitMQ. As you may have noticed, reveive.py does not exit when it receives a message and waits for the next message. Press Ctrl-C to exit.

In this tutorial, we learned how to send and receive messages using named queues. Tutorial 2 creates a single Work queue.

Recommended Posts

RabbitMQ Tutorial (1) -From Installation to Hello World (Official Site Translation)
Flask tutorial (from installation to hello world)
RabbitMQ Tutorial 1 ("Hello World!")
RaspberryPi3 (STRETCH) setup from OS installation to Hello World
From Kivy environment construction to displaying Hello World
From setting up a Rust environment to running Hello World
From building a Python environment for inexperienced people to Hello world
From Elasticsearch installation to data entry
Introduction to TensorFlow --Hello World Edition
OpenMPI installation from download to pass-through
Try Django's official tutorial from scratch
Introduction to Ansible Part 1'Hello World !!'
C language to see and remember Part 1 Call C language from Python (hello world)
[Python] Web application from 0! Hands-on (2) -Hello World-
[Note] [PyTorch] From installation to easy usage
How to build Hello, World on #Nix
How to display Hello world in python
Introduction to Scapy ① (From installation to execution of Scapy)