Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Rabbitmq study notes

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I. message confirmation mechanism

Rabbitmq deletes the message from memory immediately after sending the message, so if the consumer takes a long time to process the message and the consumer is kill during processing, the message in process and other messages sent to that consumer will be lost.

To ensure that the message is not lost, rabbitmq supports the message confirmation mechanism, and the consumer can send ack to tell rabbitm that the message has been received and processed, so rabbitmq can delete the message.

If the consumer dies (channel shuts down, connection shuts down, or TCP connection is lost), the rabbitmq does not receive the ack,rabbitmq and the message is requeued.

There is no message timeout, which means that processing a message for a very long time is also ok.

The message acknowledgement mechanism is turned on by default and is turned off by setting no_ack=True in channel.basic_consume.

Note that after the consumer processes the message, don't forget to call channel.basic_ack for message confirmation, otherwise rabbitmq will continue to consume memory to requeue the message.

II. Queue / message persistence

To prevent queues and messages from being lost due to rabbitmq service termination, queues and messages need to be marked as persistent:

Ensure that rabbitmq never loses queues, which need to be declared persistent:

Channel.queue_declare (queue='task_queue', durable=True)

Declare the message as persistent:

Channel.basic_publish (exchange='', routing_key= "task_queue", body=message, properties=pika.BasicProperties (delivery_mode = 2, # make message persistent))

Note: although you are already robust, there is no guarantee that messages will not be lost, such as when rabbitmq receives messages but has not yet saved them to your hard disk.

III. Exchange

To put it simply, one end of the exchange receives the message, and the other end puts the message into the queue.

In rabbitmq, the producer does not send the request directly to the consumer, the production value only sends the message to the exchange,exchange and needs to know what to do after receiving the message: add to a specific queue, add to multiple queues, or discard it.

The types of exchange include direct,topic,headers,fanout

IV. Binding

The link between exchange and queue is called binding, and you can simply look at it: the queue is interested in messages on a particular exchange

Channel.queue_bind (exchange='logs', queue=result.method.queue)

At this point, 'logs' exchange will add a message to the specified queue

Bindings can use an additional routing_key parameter, such as:

Channel.queue_bind (exchange=exchange_name, queue=queue_name, routing_key='black')

For exchange of type fanout, the routing_key parameter is ignored

5. Topic exchange

Messages destined for topic exchange cannot carry any routing_key, but must be a string of characters separated by dots, up to a maximum of 255bytes

Binding key must also be in the same form, and note that binding key has two important special cases:

* can replace a word

# can replace zero or more words

For example, if binding key is * .orange. *, you can match all .oranges. , but if the key is not in the form of *. *, such as orange, or quick.orange.male.rabbit, the message will be discarded.

If binding key is lazy.#, messages similar to key with lazy.orange.male.rabbit can match.

Topic exchange is very powerful, and by matching routing_key, it can appear as if there are multiple exchange

VI. RPC

In order to receive the response, the client needs to attach the send callback queue address when sending the request:

Result = channel.queue_declare (exclusive=True) callback_queue = result.method.queuechannel.basic_publish (exchange='', routing_key='rpc_queue', properties=pika.BasicProperties (reply_to = callback_queue,), body=request) #... And some code to read a response message from the callback_queue...

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report