I. Introduction of RABBITMQ
Explaining RABBITMQ, you have to mention the AMQP (Advanced Message Queuing Protocol) protocol. The AMQP protocol is a network-based message Transfer protocol that provides reliable message transmission between an application or an organization. RABBITMQ is an implementation of the AMQP protocol that enables the transmission of messages from sender to receiver safely and reliably. Simply put, it is the message sender that uses RABBITMQ to pass information securely to the receiving party.
RABBITMQ is a complete, reusable enterprise messaging system based on AMQP. He follows the Mozilla Public License open source agreement.
MQ is all called the message queue, and Message Queuing (MQ) is an application-to-application communication method. Applications communicate by reading and writing messages to and from the queue (data for the application), without requiring a dedicated connection to link them. Message passing refers to the process of communicating between programs by sending data in a message, rather than by directly invoking each other, and directly invoking techniques such as remote procedure calls. Queuing refers to an application communicating through a queue. The use of queues removes the requirement that both the receiving and sending applications execute concurrently.
Second, RABBITMQ installation
Yum-y Install Epel-release
Yum-y Install Rabbitmq-server
Systemctl Start Rabbitmq-server.service #启动服务
Systemctl Enable Rabbitmq-server.service #将服务加入开机启动
Install API, use API to operate RABBITMQ
Pip Install Pika
Or
Easy_install Pika
Or
SOURCE Https://pypi.python.org/pypi/pika
A city Simple RABBITMQ example
########################## Publisher #########################Importpikaconnection= Pika. Blockingconnection (Pika. Connectionparameters ('192.168.31.11')) Channel=Connection.channel ()#declaring a queueChannel.queue_declare (queue='Hello_chen')#n RabbitMQ A message can never is sent directly to the queue, it always needs to go through an exchange.#Exchange is like a switch, and then the switch decides to put the message in that queue. Null here indicates that the switch does not work. #The data in the body is placed in a queue named Hello_chen. Channel.basic_publish (exchange="', Routing_key='Hello_chen', Body='Hello world!')Print("[x] Sent ' Hello world! '") Connection.close ()published by
########################### subscriber ##########################ImportPikaImporttimeconnection= Pika. Blockingconnection (Pika. Connectionparameters ('192.168.31.11')) Channel=Connection.channel ()#May ask why we declare the queue again? We had already declared it in our previous code.#we could avoid so if We were sure that the queue already exists. For example if send.py program#was run before. But we ' re not yet sure which program to run first. In such cases it ' s a good#practice to repeat declaring the queue in both programs.#Why the consumer program also needs to create a queue because it is not known to producers and consumers who first started. Otherwise you will get an error. Channel.queue_declare (queue='Hello_chen')defCallback (ch, method, properties, body):Print(' -', ch, method, properties) Time.sleep (10)#10S for simulation tasks Print("[x] Received%r"%body) ch.basic_ack (Delivery_tag=method.delivery_tag)#sends an ACK after the queue message is processed and needs to be used with the following No_ack#take the data from the body inside the queue Hello_chen and assign it as a parameter to the body in the callback function. Channel.basic_consume (callback, queue='Hello_chen' #No_ack=true #此参数虽然可以增加消息的ack, but it has an impact on efficiency. )Print('[*] waiting for messages. To exit Press CTRL + C') channel.start_consuming ()subscribed by
Python's Path to Growth "12th": Getting Started with RABBITMQ