Python operation of RabbitMQ (1)

Source: Internet
Author: User

Because I want to use Python to implement a distributed system to manage and monitor the content and running status of CDN, I accidentally met RabbitMQ and many people are recommended, for example, why do I choose RabbitMQ.

It took me several hours to read what MQ is, that is, Message Queue ). As the name implies, a message queue is a data structure filled with message queues, queues, and data structures. A message queue is nothing more than a message, so what is its significance, where is its application scenario, what features it has, what is its unique advantage, and why it should be used, these are a series of problems that come to my mind.

After a large string of searches, I finally had a superficial understanding that message queue is the communication method that the application transmits information to the application. For example, to analyze the logs of multiple servers, each server can use a process to write data to the table of a Mysql database, that is, the required information, and then write several processes, reading data in the table is not very good for data analysis, but soon the ugliness of this design is shown ...... Do you want multiple programs to fetch data from a queue for processing? No problem. We have a good number of hard-coded programs ...... What? How can I dynamically allocate pressure when programs increase or decrease dynamically? This is an example of RabbitMQ + Python getting started with classic rabbit and rabbit nest. Think about it too. When my CDN delivers a large amount of data, data distribution, processing, and everything will be a problem. But I still don't understand how Rabbit implements these things.

In terms of concept, RabbitMQ is the standard implementation of AMPQ (Advanced Message Protocol Queue). It is said that AMQP is not familiar with the document of RabbitMQ. However, we can only build a big understanding of the key concepts. The entire implementation principle model of RabbitMQ is actually a producer and consumer model with a routing Task Distribution queue ., That is, the producer generates the corresponding information and sends it to the router. The router distributes the information to different message queues based on the Key information in the information, then, the consumer reads data from different message queues.

Rabbitmq-server-.-. noarch. rpm

The installation is successful.

In order to set up RBMQ to start upon startup, use the Administrator account to execute

chkconfig rabbitmq-server on

Enable and disable server commands

/sbin/service rabbitmq-server stop/start

The following error is reported when the result is Enabled:
Starting rabbitmq-server (via systemctl): Job for rabbitmq-server.service failed. See 'systemctl status rabbitmq-server.service 'and 'journalctl-xn' for details. [FAILED]

Run the journalctl-xn command to open the log, check that a file that looks like Erlang is denied access, and then put forward a lot of suggestions.

Try it

 beam.smp /var/log/audit/audit.log | audit2allow --/sbin/service rabbitmq-server start

Miraculous success. Okay. I will be back later to study what I just did.

Pika

Start with the simplest message sending and receiving method. That is, one end sends messages and one end receives messages.

= Connection. channel ()

The server connects to the local localhost. You can also specify the ip address or host name.

Second, the sender needs to declare a queue, for example, name it sayhello.

channel.queue_declare(queue=)

Now we can send messages. Because the first small case is relatively simple and does not pass through the router, you can specify a blank route when sending a message.

channel.basic_publish(exchange=== 


Close the connection.

connection.close()

The receiver is the consumer and needs to obtain data from the queue. Therefore, you also need to bind a queue

channel.queue_declare(queue=)

At the same time, since the receiver is working on a queue-based message to execute a callback function, Pika will execute the corresponding callback function when receiving the message, so we need to define this function.

  % (body,)

Next we need to initialize this consumer and start the consumer.

== 

OK is successfully executed.

Next, we will gradually experience the unique charm of RabbitMQ.

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.