簡體   English   中英

RabbitMQ:如何將失敗的消息從一個隊列移動到另一個隊列?

[英]RabbitMQ: How to move failed message from one queue to another queue?

我有兩個隊列:

在此處輸入圖像描述

當我運行rabbitmqadmin list queues vhost name node messages message_stats.publish_details.rate -u admin -p admin時,同樣可見:

我得到:

+-------+-------------------------+-------------------------+----------+------------------------------------+
| vhost |          name           |          node           | messages | message_stats.publish_details.rate |
+-------+-------------------------+-------------------------+----------+------------------------------------+
| /     | high_priority           | rabbit@server-rabbitmq  | 5        | 0.0                                |
| /     | high_priority_secondary | rabbit@server-rabbitmq  | 0        | 0.0                                |
+-------+-------------------------+-------------------------+----------+------------------------------------+

我的交流( rabbitmqadmin -V / list exchanges -u admin -p admin )如下所列:

+-------------------------+---------+
|          name           |  type   |
+-------------------------+---------+
|                         | direct  |
| amq.direct              | direct  |
| amq.fanout              | fanout  |
| amq.headers             | headers |
| amq.match               | headers |
| amq.rabbitmq.trace      | topic   |
| amq.topic               | topic   |
| high_priority           | direct  |
| high_priority_secondary | direct  |
| low_priority            | direct  |
+-------------------------+---------+

隊列和整個相關邏輯在 PHP / Symfony 中實現,但是我想通過在終端中使用rabbitmqadminrabbitmqctl命令來使用本機邏輯(如果可能)。

如果high_priority上的消息失敗,我希望 RabbitMQ 自動將其移動到high_priority_secondary隊列,而無需任何 PHP 參與。 這可能嗎? 我已經開始閱讀有關死信交換的信息,但我不確定如何處理這個問題。

我已經為輔助隊列創建了一個消費者,因此只要將消息移到那里,就會對其進行處理。

是否可以僅在 CLI 中實現這一點?

僅供參考:有一些關於 SO 的建議帖子已經涵蓋了這個問題,但沒有一個解決方案是純粹的 CLI 解決方案。

high_priority_secondary隊列應該綁定到high_priority_secondary交換。 high_priority隊列應該綁定到high_priority交換並且應該用x-dead-letter-exchange = high_priority_secondary聲明。

所以應該用死信交換來聲明隊列。

要對此進行測試,只需在您從high_priority隊列中使用消息時拒絕帶有 requeue 的消息。 在此處輸入圖像描述

好的,雖然我不必修改任何 PHP 代碼,但我確實必須在框架級別更改yaml配置,因為我希望我的解決方案能夠持久化並成為代碼庫的一部分。

在您的app/config/services/rabbitmq.yaml

定義生產者:

high_priority:
    connection: default
    class: Foo\Infrastructure\RabbitMQ\SuppressedProducer
    exchange_options:
        name: 'high_priority'
        type: direct
high_priority_secondary:
    connection: default
    class: Foo\Infrastructure\RabbitMQ\SuppressedProducer
    exchange_options:
        name: 'high_priority_secondary'
        type: direct
message_hospital:
    connection: default
    class: Foo\Infrastructure\RabbitMQ\SuppressedProducer
    exchange_options:
        name: 'message_hospital'
        type: direct

定義消費者:

high_priority:
    connection: default
    exchange_options:
        name: 'high_priority'
        type: direct
    queue_options:
        name: 'high_priority'
        arguments:
            x-dead-letter-exchange: ['S', 'high_priority_secondary']
    qos_options:
        prefetch_size: 0
        prefetch_count: 1
        global: false
    callback: foo.task_bus.consumer
high_priority_secondary:
    connection: default
    exchange_options:
        name: 'high_priority_secondary'
        type: direct
    queue_options:
        name: 'high_priority_secondary'
        arguments:
            x-dead-letter-exchange: ['S', 'message_hospital']
    qos_options:
        prefetch_size: 0
        prefetch_count: 1
        global: false
    callback: foo.task_bus.consumer
message_hospital:
    connection: default
    exchange_options:
        name: 'message_hospital'
        type: direct
    queue_options:
        name: 'message_hospital'
    qos_options:
        prefetch_size: 0
        prefetch_count: 1
        global: false
    callback: foo.task_bus.consumer

現在隊列看起來像:

在此處輸入圖像描述

多虧了 DLX 屬性,消息一旦在之前的消息中失敗,就會進入醫院隊列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM