RabbitMQ(一)publish消息确认

Confirms (aka Publisher Acknowledgements)


Using standard AMQP, the only way to guarantee that a message isn't lost is by using transactions -- make the channel transactional, publish the message, commit. In this case, transactions are unnecessarily heavyweight and decrease throughput by a factor of 250. To remedy this, a confirmation mechanism was introduced.

如果采用标准的 AMQP 协议,则唯一能够保证消息不会丢失的方式是利用事务机制 -- 令 channel 处于 transactional 模式、向其 publish 消息、执行 commit 动作。在这种方式下,事务机制会带来大量的多余开销,并会导致吞吐量下降 250% 。为了补救事务带来的问题,引入了 confirmation 机制(即 Publisher Confirm)。


To enable confirms, a client sends the confirm.select method. Depending on whether no-wait was set or not, the broker may respond with a confirm.select-ok. Once the confirm.select method is used on a channel, it is said to be in confirm mode. A transactional channel cannot be put into confirm mode and once a channel is in confirm mode, it cannot be made transactional.

为了使能 confirm 机制,client 首先要发送 confirm.select 方法帧。取决于是否设置了 no-wait 属性,broker 会相应的判定是否以 confirm.select-ok 进行应答。一旦在 channel 上使用 confirm.select方法,channel 就将处于 confirm 模式处于 transactional 模式的 channel 不能再被设置成 confirm 模式,反之亦然。


Once a channel is in confirm mode, both the broker and the client count messages (counting starts at 1 on the first confirm.select). The broker then confirms messages as it handles them by sending a basic.ack on the same channel. The delivery-tag field contains the sequence number of the confirmed message. The broker may also set the multiple field in basic.ack to indicate that all messages up to and including the one with the sequence number have been handled.

一旦 channel 处于 confirm 模式,broker 和 client 都将启动消息计数(以 confirm.select 为基础从 1 开始计数)。broker 会在处理完消息后,在当前 channel 上通过发送 basic.ack 的方式对其进行 confirm 。delivery-tag 域的值标识了被 confirm 消息的序列号。broker 也可以通过设置 basic.ack 中的 multiple 域来表明到指定序列号为止的所有消息都已被 broker 正确的处理了。


In exceptional cases when the broker is unable to handle messages successfully, instead of a basic.ack, the broker will send a basic.nack. In this context, fields of the basic.nack have the same meaning as the corresponding ones in basic.ack and the requeue field should be ignored. By nack'ing one or more messages, the broker indicates that it was unable to process the messages and refuses responsibility for them; at that point, the client may choose to re-publish the messages.

在异常情况中,broker 将无法成功处理相应的消息,此时 broker 将发送 basic.nack 来代替 basic.ack 。在这个情形下,basic.nack 中各域值的含义与 basic.ack 中相应各域含义是相同的,同时 requeue 域的值应该被忽略。通过 nack 一或多条消息broker 表明自身无法对相应消息完成处理,并拒绝为这些消息的处理负责。在这种情况下,client 可以选择将消息 re-publish 。


After a channel is put into confirm mode, all subsequently published messages will be confirmed or nack'd once. No guarantees are made as to how soon a message is confirmed. No message will be both confirmed and nack'd.

在 channel 被设置成 confirm 模式之后,所有被 publish 的后续消息都将被 confirm(即 ack) 或者被 nack 一次。但是没有对消息被 confirm 的快慢做任何保证,并且同一条消息不会既被 confirm 又被 nack 。


An example in Java that publishes a large number of messages to a channel in confirm mode and waits for the acknowledgements can be found 
here.
一个 Java 示例展现了 publish 大量消息到一个处于 confirm 模式的 channel 并等待获取 acknowledgement 的情况,示例在这里


When will messages be confirmed?
消息会在何时被 confirm?


The broker will confirm messages once:

broker 将在下面的情况中对消息进行 confirm :

  • it decides a message will not be routed to queues
    (if the mandatory flag is set then the basic.return is sent first) or
    broker 发现当前消息无法被路由到指定的 queues 中(如果设置了 mandatory 属性,则 broker 会先发送 basic.return
  • a transient message has reached all its queues (and mirrors) or
    非持久属性的消息到达了其所应该到达的所有 queue 中(和镜像 queue 中)
  • a persistent message has reached all its queues (and mirrors) and been persisted to disk (and fsynced) or
    持久消息到达了其所应该到达的所有 queue 中(和镜像 queue 中),并被持久化到了磁盘(被 fsync)
  • a persistent message has been consumed (and if necessary acknowledged) from all its queues
    持久消息从其所在的所有 queue 中被 consume 了(如果必要则会被 acknowledge)



Notes


The broker loses persistent messages if it crashes before said messages are written to disk. Under certain conditions, this causes the broker to behave in surprising ways.

broker 会丢失持久化消息,如果 broker 在将上述消息写入磁盘前异常。在一定条件下,这种情况会导致 broker 以一种奇怪的方式运行。


For instance, consider this scenario:

例如,考虑下述情景:

  1. a client publishes a persistent message to a durable queue
    一个 client 将持久消息 publish 到持久 queue 中
  2. a client consumes the message from the queue (noting that the message is persistent and the queue durable), but doesn't yet ack it,
    另一个 client 从 queue 中 consume 消息(注意:该消息具有持久属性,并且 queue 是持久化的),当尚未对其进行 ack 
  3. the broker dies and is restarted, and
    broker 异常重启
  4. the client reconnects and starts consuming messages.
    client 重连并开始 consume 消息

At this point, the client could reasonably assume that the message will be delivered again. This is not the case: the restart has caused the broker to lose the message. In order to guarantee persistence, a client should use confirms. If the publisher's channel had been in confirm mode, the publisher would 
  not    have received an ack for the lost message (since the consumer hadn't ack'd it and it hadn't been written to disk).  

在上述情景下,client 有理由认为消息需要被(broker)重新 deliver 。但这并非事实:重启(有可能)会令 broker 丢失消息。为了确保持久性,client 应该使用 confirm 机制。如果 publisher 使用的 channel 被设置为 confirm 模式,publisher 将不会收到已丢失消息的 ack(这是因为 consumer 没有对消息进行 ack ,同时该消息也未被写入磁盘)。


下面是用rabbitmq-c实现publish消息确认的代码:

/*
 *
 * gcc -o amqp_sendstring amqp_sendstring.c utils.c -I/usr/local/rabbitmq-c/include -L/usr/local/rabbitmq-c/lib -lrabbitmq
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdint.h>

#include <amqp_tcp_socket.h>
#include <amqp.h>
#include <amqp_framing.h>

#include "utils.h"

int main(int argc, char const *const *argv)
{
  char const *hostname;
  int port, status;
  char const *exchange;
  char const *routingkey;
  char const *messagebody;
  amqp_socket_t *socket = NULL;
  amqp_connection_state_t conn;

  if (argc < 6) {
    fprintf(stderr, "Usage: amqp_sendstring host port exchange routingkey messagebody\n");
    return 1;
  }

  hostname = argv[1];
  port = atoi(argv[2]);
  exchange = argv[3];
  routingkey = argv[4];
  messagebody = argv[5];

  conn = amqp_new_connection();

  socket = amqp_tcp_socket_new(conn);
  if (!socket) {
    die("creating TCP socket");
  }

  status = amqp_socket_open(socket, hostname, port);
  if (status) {
    die("opening TCP socket");
  }

  die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"),
                    "Logging in");
  amqp_channel_open(conn, 1);
  die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");

  {
    amqp_basic_properties_t props;
    props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
    props.content_type = amqp_cstring_bytes("text/plain");
    props.delivery_mode = 2; /* persistent delivery mode */

    amqp_confirm_select(conn, 1);  //在通道上打开Publish确认
    die_on_error(amqp_basic_publish(conn,
                                    1,
                                    amqp_cstring_bytes(exchange),
                                    amqp_cstring_bytes(routingkey),
                                    0,   //mandatory标志位,消息不能到达队列则返回basic.return
                                    0,   //immediate标志位,消息不能到达消费者返回basic.return
                                    &props,
                                    amqp_cstring_bytes(messagebody)),
                 "Publishing");
  }

  {
    /* Publish消息后需要在当前通道上监听返回的信息,来判断消息是否成功投递
     * 这里要息根据投递消息的方式来过滤判断几个方法
     */
    amqp_frame_t frame;
    amqp_rpc_reply_t ret;

    if (AMQP_STATUS_OK != amqp_simple_wait_frame(conn, &frame)) {
      return;
    }

    if (AMQP_FRAME_METHOD == frame.frame_type) {
      amqp_method_t method = frame.payload.method;
      fprintf(stdout, "method.id=%08X,method.name=%s\n",
        method.id, amqp_method_name(method.id));
      switch (method.id) {
        case AMQP_BASIC_ACK_METHOD:{
          /* if we've turned publisher confirms on, and we've published a message
           * here is a message being confirmed
           */
          {
            amqp_basic_ack_t *s;
            s = (amqp_basic_ack_t *) method.decoded;
            fprintf(stdout, "Ack.delivery_tag=%d\n", s->delivery_tag);
            fprintf(stdout, "Ack.multiple=%d\n", s->multiple);
          }

          break;

        case AMQP_BASIC_NACK_METHOD:
          /* if we've turned publisher confirms on, and we've published a message
           * here is a message not being confirmed
           */
          {
            amqp_basic_nack_t *s;
            s = (amqp_basic_nack_t *) method.decoded;
            fprintf(stdout, "NAck.delivery_tag=%d\n", s->delivery_tag);
            fprintf(stdout, "NAck.multiple=%d\n", s->multiple);
            fprintf(stdout, "NAck.requeue=%d\n", s->requeue);
          }

          break;

        case AMQP_BASIC_RETURN_METHOD:
          /* if a published message couldn't be routed and the mandatory flag was set
           * this is what would be returned. The message then needs to be read.
           */
          {
            amqp_message_t message;
            amqp_basic_return_t *s;
            char str[1024];
            s = (amqp_basic_return_t *) method.decoded;
            fprintf(stdout, "Return.reply_code=%d\n", s->reply_code);
            strncpy(str, s->reply_text.bytes, s->reply_text.len); str[s->reply_text.len] = 0;
            fprintf(stdout, "Return.reply_text=%s\n", str);

            ret = amqp_read_message(conn, frame.channel, &message, 0);
            if (AMQP_RESPONSE_NORMAL != ret.reply_type) {
              return;
            }
            strncpy(str, message.body.bytes, message.body.len); str[message.body.len] = 0;
            fprintf(stdout, "Return.message=%s\n", str);

            amqp_destroy_message(&message);
          }

          break;

        case AMQP_CHANNEL_CLOSE_METHOD:
          /* a channel.close method happens when a channel exception occurs, this
           * can happen by publishing to an exchange that doesn't exist for example
           *
           * In this case you would need to open another channel redeclare any queues
           * that were declared auto-delete, and restart any consumers that were attached
           * to the previous channel
           */
          return;

        case AMQP_CONNECTION_CLOSE_METHOD:
          /* a connection.close method happens when a connection exception occurs,
           * this can happen by trying to use a channel that isn't open for example.
           *
           * In this case the whole connection must be restarted.
           */
          return;

        default:
          fprintf(stderr ,"An unexpected method was received %d\n", frame.payload.method.id);
          return;
      }
    }
  }

  }

  die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel");
  die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection");
  die_on_error(amqp_destroy_connection(conn), "Ending connection");

  return 0;
}


  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
RabbitMQ 中,可以使用确认模式来确保消息被正确地处理。确认模式有两种:基础确认模式和事务模式。其中,基础确认模式是更常用的一种。 在基础确认模式下,生产者通过 `channel.confirm_select()` 方法将 channel 设置为确认模式。然后,每当生产者发送一条消息时,都会设置一个回调函数,在服务器确认收到消息后,该回调函数就会被触发。在回调函数中,可以进行一些处理,比如记录日志或者发送下一条消息。 以下是一个示例代码: ```python import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 将 channel 设置为确认模式 channel.confirm_select() # 定义回调函数 def callback(ch, method, properties, body): print("Message received:", body) ch.basic_ack(delivery_tag=method.delivery_tag) # 确认消息已被处理 # 向队列发送消息,并设置回调函数 channel.basic_publish(exchange='', routing_key='hello', body='Hello, World!') channel.add_callback(callback) # 等待确认消息 if channel.waitForConfirms(): print("Message sent successfully.") else: print("Failed to send message.") connection.close() ``` 在上面的代码中,我们首先将 `channel` 设置为确认模式,然后向队列发送一条消息,并设置一个回调函数 `callback`。在回调函数中,我们打印出收到的消息,并调用 `ch.basic_ack()` 方法来确认消息已被处理。最后,我们调用 `channel.waitForConfirms()` 方法来等待消息确认。如果消息发送成功,该方法将返回 `True`,否则返回 `False`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值