RabbitMQ Message distribution round robin and Message Acknowledgment
I. Message Distribution
Messages in RabbitMQ can only be stored in the Queue. The producer (P) produces messages and finally delivers them to the Queue. The consumer (C) can obtain and consume messages from the Queue.
Multiple consumers can subscribe to the same Queue. In this case, messages in the Queue will be evenly distributed to multiple consumers for processing, rather than each consumer will receive and process all the messages.
Start three consumers
The producer generates three messages in sequence.
It can be seen that three messages are obtained by three consumers respectively. Therefore, RabbitMQ uses the polling mechanism to send messages in the Queue to different consumers in sequence.
2. Message Acknowledgment)
In actual applications, messages in the Queue may be received by the consumer, but the message goes down (or in case of other exceptions) if the processing is not completed. In this case, messages may be lost. In order to avoid this situation, we can ask the consumer to send a receipt to RabbitMQ after the Message is consumed. After RabbitMQ receives the Message receipt (Message acknowledgment), it will remove the Message from the Queue; if RabbitMQ does not receive a receipt and detects that the consumer's RabbitMQ connection is disconnected, RabbitMQ sends the message to other consumers (if multiple consumers exist) for processing. The concept of timeout does not exist here. If a consumer processes a message for a long time, the message will not be sent to other consumers unless its RabbitMQ connection is disconnected.
How to implement it? You only need to remove no_ack = True from the consumer.
No_ack means no acknowlegment. This parameter will cause RabbitMQ not to care whether the consumer has completed the processing. It may remove the message from the Queue after the consumer obtains the message. Remove this parameter. If an error occurs during the consumer's execution (downtime), RabbitMQ will send the message receipt to other consumers for execution.
Modify the consumer end
def
callback(ch, method, properties, body):
print
(
'--->>'
, ch,
'\n'
, method,
'\n'
, properties)
time.sleep(
30
)
# Let the consumer process a little longer and can be used to simulate disconnection during running
print
(
" [x] Received %r"
%
body)
ch.basic_ack(delivery_tag
=
method.delivery_tag)
# Confirm that the message has been executed and notify rabbitMQ
# Ch: the declared channel object memory address # channel. basic_consume (callback, # Call the callback function if a message is received to process the Message queue = 'hello', # no_ack = True)
Run three consumers to receive the data from the producer, disable consumer 1 and consumer 2 in sequence, and finally the messages in RabbitMQ will be processed by consumer 3.
Setting up a RabbitMQ cluster in CentOS 7.2
Install and use professional message queue product RabbitMQ in CentOS7 Environment
RabbitMQ getting started
How to install RabbitMQ on CentOS7
How to Use NServiceBus with RabbitMQ
RabbitMQ cluster installation and configuration in CentOS 7
RabbitMQ practice: deploy distributed message queue in Chinese PDF
Detailed installation of RabbitMQ on CentOS7
RabbitMQ distributed cluster architecture and high availability (HA)
RabbitMQ details: click here
RabbitMQ: click here