Advanced scenarios of rabbitmq

This article will answer the following questions:

1. How to ensure that the producer will not lose the message

2. How to ensure that mq messages are persisted to the hard disk

3. Consumer message confirmation

4. Consumer consumption failure retry mechanism

1. How to ensure that the producer will not lose the message

Because the message needs to be sent to the consumer through multiple network transmissions, producer->switch->queue->consumer. After three data transmissions in the middle, it will inevitably be lost.

rabbitmq provides a publisher confirm mechanism to avoid message loss during the process of sending it to mq. After the message is sent to mq, a result will be returned, indicating that the processing is successful. There are two types of results returned.

publisher-confirm sender confirmation

        The message is successfully sent to the switch and ack is returned.

The message is not successfully sent to the switch and nack is returned.

publisher-return sender receipt

The message is sent to the switch, but is not routed to the queue. An ack is returned, and the reason for the routing failure is returned.

solution:

If the corresponding success indicator is not returned, you can record the log, resend it again, or process it manually.

2. How to ensure that mq messages are persisted to the hard disk

        Because mq’s messages are stored in memory, if mq goes down or the service hangs, the messages will be gone after restarting. So what to do?

        It’s actually very simple: just set the switch and queue parameters durability to durable. So how can your message be durable?

        ​ ​ ​ In fact, it is also very simple: the officially provided MessageBuilder, set DeliveryMode, the specific code:

Message msg = MessageBuilder
.withBody(message.getBytes(StandardCharsets.UTF_8))//消息体
.setDeliveryMode(MessageDeliveryMode.PERSISTENT)//设置持久化
// .setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)//设置不持久化
.build();

In fact, our springamqp has helped us with the persistence of switches, queues, and messages. However, some functions do not need to be persisted. Here, based on business analysis, whether to make persistence settings

3. Consumer message confirmation

        There are three modes:

                Manual: To send ack manually, you need to write your own code and try-catch to process it (not recommended)

                2) Auto: Automatic ACK, Spring helped us handle the code of this part. Spring was tested through AOP. There was no abnormality. It helped us send ACK and there were abnormalities.

                ​ ​ ​ ​ 3) none: turn off ack,

Solution: Code configuration:

spring:
    rabbitmq:
        listener:
          simple:
            acknowledge-mode: auto #none 关闭 , manual 手动 , auto 自动

4. Consumer consumption failure retry mechanism

        The message retry is set and nack is returned. If the consumer receives the message, but an exception occurs at this time, spring will deliver the message to the consumer again. If there is still a problem with the code, it will deliver it again and keep looping. This will cause problems to the system. It brings a lot of pressure, so how to solve it?

        Solution: Code configuration

spring:
  rabbitmq:
    listener:
      simple:
        prefetch: 1
        retry:
          enabled: true #开启消费者失败重试
          initial-interval: 1000 #初始的失败等待时间为1秒
          multiplier: 1 #下次失败的等待时长倍数
          max-attempts: 5 #最多重试次数
          stateless: true #true无状态, false有状态 ,如果业务中包含事务,这里成false

Of course, you can also record logs, resend it, or process it manually. The method is flexible.

ok, that’s it for today’s sharing, see you next time

Guess you like

Origin blog.csdn.net/Japhet_jiu/article/details/125299332