Rabbitmq BASICS (1)

Source: Internet
Author: User

Chapter 1: rabbitmq Introduction

Introduction:

Rabbitmq is a message-oriented middleware. It is mainly used for message forwarding and receiving. Compare rabbitmq to a post office: as long as you deliver the mail to your mailbox, you can be sure that the postman will be able to deliver your mail to your destination. The rabbitmq function is equivalent to the Function Represented by mailbox, post office, and postman.

The Post Office delivers paper letters. The difference between rabbitmq is that it can receive, store, and forward binary big object data. That is, messages.

Rabbitmq related terms:

(1) producing: production is equivalent to sending a message. We make the program that sends the message into a producer ). In the form of an image:


(2) Queue (Queue): the queue of rabbitmq is equivalent to the mailbox mentioned above. It is located inside rabbitmq. Although messages are transmitted between applications and rabbitmq, they can only be stored in the queue. If no conditions are set for the queue, the queue can save any information you want to save. In fact, a queue is essentially a buffer with no capacity limit. After some producers send messages to the queue, the consumers can obtain messages from the queue for processing. The queue can be represented as follows:


(3) Consuming: consumption is equivalent to receiving messages ). Generally, a message consumer is an application waiting to receive messages in a message queue. Message consumer example table:


Generally, message producers, consumers, and middleware are not on the same machine.

 

A simple rabbitmqhelloworld Program

The helloword program simply sends messages, receives them, and prints them to the screen. To implement this function, you need to write two simple programs: one for sending messages and one for receiving messages and printing messages to the screen.

As shown in:


A message producer sends a message to a message queue named "hello". The message consumer receives the message from the queue.

TIPS:

Rabbitmq Library

Rabitmq implements the amqp protocol. When using rabbit, you need to import some libraries, which also follow the amqp protocol like rabbit. Multiple libraries are available for each language. Python implementation libraries include:

-> Py-amqplib

-> Txamqp

-> Pika

This document mainly uses the pika library. Install the pythonpika package: $ sudo Pip install pika = 0.9.8

Install pika on Pip and git-core packages. Install these dependency packages before installing pika.

Ubuntu installation: sudo apt-Get install Python-pip Git-core

Debian installation: APT-Get install Python-setuptools Git-core

Sudo easy-install Pip

Windows installation: easy_install Pip

PIP install pika = 0.9.8

 

Message sending:


This document uses send. py to send messages. First, you need to establish a connection to the rabbitmq service.


A connection to the local middleware has been established above. If the middleware service runs on a remote machine, you only need to modify the connection parameter to the IP address of the remote host.

After the connection is established, make sure that the message receiving queue already exists. If a message is sent to a non-existent location, rabbitmq recycles the sent message. For example, to create a message queue named "hello" to receive messages, the creation process is as follows:


After creation, you can start sending messages. In this example, a message containing a "hello" string is sent to the Message Queue named "hello.

In rabbitmq, messages cannot be directly sent to the message queue. Always send messages to the Message Queue through exchange. A detailed introduction to exchange is provided later. Here, you only need to know how to use a default exchange with a Null String. This specific exchange can specify the message queue in which our messages should be sent. The message queue name must be specified in the routing_key parameter:


Before exiting the program, we need to ensure that the Network cache is flushed and the message is sent to rabbitmq. Then we close the connection.


TIPS: The consumption sending process cannot be completed

If you use rabbitmq for the first time, when you see that the message cannot be sent and you do not know why the error occurs. You may need to check whether your message-oriented middleware has sufficient disk space when it is started (at least 1 GB by default ). Too little space may cause message-oriented middleware to reject messages. When detecting intermediate logs, the disk space limit can be reduced. The configuration file details how to set disk_free_limit to reduce disk space restrictions.

 

Message receiving:

This document uses receive. py to receive and display messages in the message queue. Similarly, you must first connect to the rabbitmq server. The connection method is as follows. Then make sure that the Message Queue already exists. You can use the queue_declare method to create a message queue. They can execute this method multiple times. However, it is created only when it is executed for the first time.

Maybe you will ask why we have previously executed this method, and why do we need to call this method again? If you can determine and execute this method, you do not need to execute it again. However, if we are not sure whether this method has been executed. It is also a good solution to repeatedly call this method in multiple programs to ensure that the message queue already exists.

TIPS: You want to view which rabbitmq already exists and how many messages are in the message queue. You can use the rabbitmqctl tool to view the information:


Receiving messages from a message queue is a little more complex. It is done by declaring a callback function callback in the message queue in advance. When we receive a message, the callback function will be called by the pika library. The callback function body displays the corresponding content on the screen:


Then, we need to notify the specific callback function of rabbitmq to receive messages from the "hello" Message Queue:


Before notifying rabbitmq, we also need to ensure that the message queue already exists. This completes the confirmation after we call the queue_declare function. The no_ack parameter will be detailed later.

Finally, we will enter a loop to wait for receiving data and run the callback function (after receiving the message) as needed ):


 

Rabbitmq BASICS (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.