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

The problem of RabbitMQ message loss and the consumer side of ensuring message reliability do not lose messages and the example analysis of HA

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

The problem of RabbitMQ message loss and the example analysis of the consumer side of message loss and HA, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

The above two operations ensure that the message will not be lost to the server, but it is not in a highly available state. If the node is hung up and the service is temporarily unavailable, the message will be restored after restart, and the message will not be lost because there is disk storage.

This article starts with the consumer side:

How not to lose information from RabbitMQ Server to consumers?

As mentioned in the previous article, after consumers got the message, they didn't have time to process it, so they went down directly, because the messenger defaults to automatic ack. At this time, RabbitMQ's automatic ack mechanism will notify MQ Server that the message has been processed, and the message will be lost, which is not expected.

Then we use manual ack mechanism to solve this problem, and the consumer notifies MQ Server after processing the logic, so that consumers will not send ack until the message is processed. If the consumer gets the message and doesn't have time to process it, the MQ cluster will automatically perceive it, and it will automatically resend the message to other consumer service instances.

According to the above ideas, you need to complete the following two steps:

First: consumer monitoring settings manual ack

This.channel = channelManager.getListenerChannel (namespace); this.queue = queue; this.channel.basicConsume (queue, false, consumerTag, this); this.disconnectedCallback.setChannel (channel)

Core code: this.channel.basicConsume (queue, false, consumerTag, this); the second parameter sets false for not automatic ack

Second: manual ack after the completion of business execution

Public static void ack (MessageContext context) {long deliveryTag = context.getEnvelope () .getDeliveryTag (); try {context.getChannel () .basicAck (deliveryTag, false);} catch (IOException e) {throw new MqAckException ("message ack error: connection exception or remote shutdown", context, e);}}

Core code: context.getChannel () .basicAck (deliveryTag, false)

Encapsulated here, the business needs to call the ack method of the object channel to notify MQServer after executing its own business code, saying that my execution is finished and you can delete it.

Note that there is a problem here: if you forget to call this context.getChannel () .basicAck (deliveryTag, false)

Or what happens if the code is not executed because of an exception? Write another article about this problem sometime later.

Messages stored in RabbitMQ Server are highly available

When we solve the problems of production and consumption, we basically guarantee that messages will not be lost, but there is another problem of high availability of messages. Single node and ordinary nodes will affect the temporary unavailability of messages. In this case, we need to use our HA image cluster mode to ensure.

In the previous article, solving the problem of RabbitMQ message loss and ensuring message reliability (1) have already mentioned the difference between the three modes of server-side message deployment. Today we will focus on the introduction of the mirror mode.

The mirroring mode is guaranteed by at least 3 nodes, 2 disk nodes and 1 memory node, architecture diagram:

There are also some strategies for setting up images:

Synchronize to all, generally do not do so, performance will be greatly affected

Synchronize up to N machines

Synchronize only to nodes that matches the specified name

> Command processing HA policy template: rabbitmqctl set_policy [- p Vhost] Name Pattern Definition [Priority]

Set mirrors for all nodes for each queue that starts with "rock.wechat" and set to automatic synchronization mode

Rabbitmqctl set_policy ha-all "^ rock.wechat"'{"ha-mode": "all", "ha-sync-mode": "automatic"} 'rabbitmqctl set_policy-p rock ha-all "^ rock.wechat"' {"ha-mode": "all", "ha-sync-mode": "automatic"}'

For each with "rock.wechat." The first queue sets the mirroring of two nodes and sets it to automatic synchronization mode

Rabbitmqctl set_policy-p rock ha-exacly "^ rock.wechat"\'{"ha-mode": "exactly", "ha-params": 2, "ha-sync-mode": "automatic"}'

For each with "node." The beginning of the queue assigns the specified node to mirror

Rabbitmqctl set_policy ha-nodes "^ nodes\."\'{"ha-mode": "nodes", "ha-params": ["rabbit@nodeA", "rabbit@nodeB"]}'

> however, the HA image queue has a big disadvantage: the throughput of the system will decrease.

Therefore, if you use the mirror mode and customize the call processing according to the specific business rules, if the business is not so important, it doesn't matter if the message is lost, and when high performance is required, the image can not be set up.

This is the answer to the question about the loss of RabbitMQ messages and the consumer side to ensure the reliability of messages and the sample analysis of HA. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Internet Technology

Wechat

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

12
Report