RabbitMQ
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. Some of the more mature MQ products are IBM WEBSPHERE MQ and so on.
Unlike the following, rabbitMq can span processes.
Thread queue: The interaction between different threads under the same process.
Process queue: The parent process interacts with a child process, or multiple sub-processes under the same parent process. There is no interaction between the different processes.
Basic operations
RABBITMQ can maintain many queues at the same time, and producers can send messages to different queues for sending to consumers.
Send End
Import pika# is equivalent to declaring a socketconn = Pika. Blockingconnection (Pika. Connectionparameters (' localhost ')) #声明一个管道channel = Conn.channel () #声明queuechannel. Queue_declare (queue= ' Hello ') Channel.basic_publish (exchange= ", routing_key= ' hello ', body= ' Hello world! ') # Routing_key message The contents of the key body message Print (' Sent ' Hello World ') conn.close ()
Run results
Sent "Hello World"
Receive End
Import pika# is equivalent to declaring a socketconn = Pika. Blockingconnection (Pika. Connectionparameters (' localhost ')) #声明一个管道channel = Conn.channel () #声明queue can not be declared here, but if the consumer runs first and does not want to go wrong, The consumer must first run 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 ) #消费消息 Call the callback function to process print if the message is received (' [*] Waiting For message. To exit Press CTRL + C ') channel.start_consuming () #开始收消息
Run results
[*] Waiting for message. To exit press Ctrl+c[x] Received B ' Hello world! '
A producer that corresponds to multiple consumers
Code above, start three consumers and a producer, the first consumer will receive the first message from the producer, the second consumer will receive a second message from the producer, which in turn rotation.
If a consumer loses power or is in the process of processing a message, the message status needs to be confirmed. After the consumer processing is completed, the state is sent to the producer and the producer deletes the message from the queue.
no_ack=True (consumer-side parameter)
Indicates that the status of the message is not acknowledged and the producer does not care about the consumer. If this parameter is removed, then the producer needs to get a response from the consumer, such as I started 3 consumers and a producer, and the producer needs to get a response from the consumer. If the first consumer gets the message and interrupts the connection, the message is sent to the second consumer, and so on, until the consumer gives the producer a state, and the producer removes the message from the queue.
This state is to be sent manually to the producer:
Ch.basic_ack (Delivery_tag=method.delivery_tag)
When the producer receives the status, it deletes the message from the queue.
Message persistence
From the above we know that the consumer in the event of a machine or other situation, as long as the status is not returned to the producer, then the message has been. What if the producer is on the machine? The news doesn't exist yet?
If the producer is on the machine, the previously saved messages will be lost. To avoid this situation, you need to persist data.
Queue Persistence
durable=True
Declares queue persistence at the same time that the queue is declared.
Channel.queue_declare (queue= ' Hello ', durable=true)
This is the queue persistence, but the message is not yet.
Data persistence
Add the above parameters at the end of the producer sending message.
Properties=pika. Basicproperties ( delivery_mode=2, )
View the HELLO2 message.
Python RABBITMQ Message Queuing