RabbitMQ message persistence
I. Preface
If we want to avoid Message loss even when the RabbitMQ service is restarted, we can set both Queue and Message as durable ), this ensures that our RabbitMQ messages will not be lost in most cases. Of course there will still be some small probability events that will lead to message loss.
Ii. Queue persistence 2.1 check the number of existing queues and messages
In windows, view the information in the rabbitmq installation directory/sbin through rabbitmqctl. bat list_queues
Two producers are started here to generate two queues hello and hello1 respectively, and both of them have a message
Restart rabbitmq to simulate a fault
We can see that the two queues disappear after the restart.
2.2 persistent queue
We will keep the hello queue persistent.
When the queue name is declared, both the producer and consumer must be in the persistent queue.
channel.queue_declare(queue
=
'hello'
, durable
=
True
)
We repeat the above operation, but do persistence for the hello queue, while hello1 does not, and restart rabbitmq
We can see that after the restart, the hello queue is still there, and the hello1 queue disappears, but a message in the original hello is not saved. So here we only achieve message queue persistence, and we have not made the message persistence.
Iii. Message persistence
We just realized that when rabbitmq crashes, the queue itself is saved and the queue is still running after the restart. Next, we will save the message, that is, message persistence.
channel.basic_publish(exchange
=
'',
routing_key
=
'hello'
,
body
=
'hello'
,
properties
=
pika.BasicProperties(
delivery_mode
=
2
,
# make message persistent
))
# Add properties. This properties is the properties in the consumer callback function.
# Delivery_mode = 2 persistent messages
The production end generates a message and restarts rabbitmq.
We can see that the hello message after the queue and message persistence does not disappear when it is restarted.
After the consumer end restarts, it can receive messages normally.
Iv. Summary
- The queue persistence parameter durable = True must be added when the queue is declared, so that the queue can be saved when rabbitmq crashes.
- Only durable = True can be used, and messages cannot be persisted only in the queue.
- Message persistence requires you to add the parameter properties = pika. BasicProperties (delivery_mode = 2) when the message is generated)
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