Because I wanted to use Python to implement a distributed system to manage and monitor the content and operating status of the CDN, I got to know RabbitMQ by accident. Many people recommended it, such as Yu Feng's "Why should I choose RabbitMQ" and so on .
When the word MQ came into view, I spent several hours reading about what MQ is, that is, Message Queue (Message Queue). As the name suggests, message queue, a queue filled with messages, queue, data structure noun, is a data structure with first-in first-out characteristics. The message queue is nothing more than used to deliver messages, so what is its significance, what are its application scenarios, what features it has, what are its unique advantages, and why it should be used. These are a series of questions that come to my mind.
After a long search, I finally came to a superficial understanding that message queue is a communication method for application-to-application information transmission. For example, if you need to analyze the logs of multiple servers, you can use one process on each server to write data, that is, the required information, into a Mysql database table, and then write several processes to read the data in the table and process the data. The analysis is not very good, but soon the ugliness of this design became apparent... Do you want multiple programs to fetch data from a queue for processing? No problem, we hardcoded the number of programs...what? Also allow the program to dynamically distribute pressure as it increases and decreases dynamically? This is an example from the very popular "RabbitMQ+Python Introduction to Classic Rabbit and Rabbit Nest". Think about it, when my CDN transmits a lot of data, data distribution, processing, everything will be a problem. But I still don’t understand how Rabbit implements these things.
Conceptually speaking, RabbitMQ is the standard implementation of AMPQ (Advanced Message Protocol Queuing). It is said that if you are not familiar with AMQP, you will not be able to understand the RabbitMQ documentation. But we can only build a large understanding of the key concepts. The entire RabbitMQ implementation principle model is shown in the figure below. It is actually a producer and consumer model with a routing task distribution queue. As shown in the figure, the producer produces 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, and then the consumer reads the data in different message queues. the process of.
#Broker: Simply put, it is the message queue server entity.
Exchange: Message exchange, which specifies the rules according to which messages are routed to which queue.
Queue: Message queue carrier, each message will be put into one or more queues.
Binding: Binding, its function is to bind exchange and queue according to routing rules.
Routing Key: Routing keyword, exchange delivers messages based on this keyword.
Vhost: virtual host. Multiple vhosts can be opened in a broker to separate permissions for different users.
Producer: Message producer is the program that delivers messages.
Consumer: Message consumer is the program that accepts messages.
Channel: Message channel. In each connection of the client, multiple channels can be established, and each channel represents a session task.
The process of using message queue is roughly as follows:
(1) The client connects to the message queue server and opens a channel.
(2) The client declares an exchange and sets related attributes.
(3) The client declares a queue and sets related attributes.
(4) The client uses routing key to establish a binding relationship between exchange and queue.
(5) The client delivers the message to exchange.
After understanding the general process and advantages of RabbitMQ, I started to trial RabbitMQ on my own Fedora.
According to the RabbitMQ official website, the installation needs to be downloaded from here
http://www.rabbitmq.com/download.htmlClick to download each version of the binary package. I downloaded rabbitmq-server-3.3.0-1.noarch.rpm
Enter the download path, /home/ sun5495/Downloads/
[sun5495@localhost Downloads]# sudo chmod 777 rabbitmq-server-3.3.0-1.noarch.rpm
Change the permissions of the executable file and increase the execution permissions.
Then execute ./rabbitmq-server-3.3.0-1.noarch.rpm, and an error will be reported when running, unable to be installed.
It turns out that you need to install Erlang now. Try this command yum install erlang to get it done.
Then use the root user to execute
rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc yum install rabbitmq-server-3.3.0-1.noarch.rpm
The installation is successful.
In order to set up RBMQ to start up, use the administrator account to execute the
chkconfig rabbitmq-server on
command to open and close the server
/sbin/service rabbitmq-server stop/start
The result is the following error when opening it
Starting rabbitmq-server (via systemctl): Job for rabbitmq-server.service failed. See 'systemctl status rabbitmq-server.service' and 'journalctl -xn' for details. [FAILED]
使用journalctl -xn命令打开日志,查看了下貌似是Erlang的某个文件拒绝访问,然后还提出了一大堆建议。
尝试一下
grep beam.smp /var/log/audit/audit.log | audit2allow -M mypolsemodule -i mypol.pp/sbin/service rabbitmq-server start
既然RabbitMQ安装也运行成功了,那么我就来尝尝RabbitMQ的鲜吧。就从官网上的例子一步一笔来做好了。
由于我使用的是Python,那么就需要安装一些支持RabbitMQ的库来进行操作,其中包括
py-amqplib,txAMQP,pika这几种,按照官网的tutorial,我也安装了pika。
pip install pika
从最简单的收发消息开始。即一端发送消息,一端接收消息。
发送方即生产者,首先要创建与RabbitMQ服务器的连接,
#!/usr/bin/env pythonimport pikaconnection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost'))channel = connection.channel()
此处服务器连接本地localhost,也可以指定ip或者主机名。
其次发送方需要声明一个队列,比如命名为sayhello
channel.queue_declare(queue='sayhello')
此时我们就可以发送消息了。由于第一个小案例比较简单,没有经过路由器,因此发送消息时,指定路由为空即可。
channel.basic_publish(exchange='', routing_key='hello', body='hello world')print "Sent ‘hello world'"
最后关闭连接即可。
connection.close()
接收方即消费者,需要从队列上获取数据,因此也需要绑定一个队列
channel.queue_declare(queue='hello')
同时,由于接收方的工作方式是基于队列的消息执行一个回调函数,当收到消息时,Pika就会执行相应的回调函数,因此我们需要定义此函数。
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()
OK执行成功。
接下来就逐步的深入体验RabbitMQ的独特魅力。
以上就是Python操作RabbitMQ初体验(一)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!