RabbitMQ試用筆記

Hub and spoke pattern, Message bus pattern, WorkQueue, Publish/Subscribe, RPC.

引言

為何要用MQ(Message Queue)? 若要處理同時大量的 Request 就必需要排隊,就像現實生活中一樣客人一多就要排隊。人數多一點就直接排隊;人數很多就領號碼排隊。

RabbitMQ幾年前就注意到了沒想到可以在激烈的竸爭下活到現在。選擇 RabbitMQ 的原因之一是可以部署到 Azuer Cloud,方法一是用 Azure Docker 部署成單體服務,方法二是透過 Azure Kubernetes Service (AKS) 可以部署成叢集。

RabbitMQ on Kubernetes for beginners

Message Queue 的應用: 實作微服務之間的事件通訊

就是實作 Message bus patternHub and spoke pattern,更俱體一點就是:

  • WorkQueue

    • 工作(task,job)派遣。

  • Publish/Subscribe

    • 訊息訂閱。

  • RPC

    • 有Queue緩存的遠程呼叫。

RabbitMQ 安裝與入門

RabbitMQ的官方文件寫得很好就不再重複,請參考下面文章。

Downloading and Installing RabbitMQ
RabbitMQ Tutorials
RabbitMQ 基本介紹、安裝教學

以規格角度整理

MessageMQ專有名詞:

  • Producer

    • A producer is a program that sends messages.

  • Queue

    • A queue is a buffer that stores messages that bound by the host's memory & disk.

  • Consumer

    • A consumer is a program that mostly waits to receive messages.

  • eXchanges

    • RabbitMQ 不直接操作訊訊息給 Queue,而是再透過 eXchage 操作。

    • Instead, the producer can only send messages to an exchange. On one side it receives messages from producers and the other side it pushes them to queues. The exchange must know exactly what to do with a message it receives. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded.

    • the exchange types: direct, topic, headersand fanout.

  • Message acknowledgment

    • Consumer 表示已收到並已處理好訊息。

    • 有自動回覆autoAck與手動回覆channel.BasicAck()

  • Bindings

    • 指定繫結 Queue 與 eXchange 。

  • Routing

    • 過濾 eXchange 可以送到 Queue 的訊息。

RabbitMQ 元素

RabbitMQ元素
  • eXchange與Queue是繫結在一起的要一起看。

  • Producer 送訊息到 eXchange。

  • eXchange 依指定的 type 與 routing 參數決定送到那個 Queue 緩存。

  • Consumer 聽到有 message 就取出來處理,處理完成回覆 ack 。

組合 RabbitMQ 元素-數量關係

組合 RabbitMQ 元素
  • eXchange 可以有多個來源 Producer。

  • eXchange 可以把 message 送到多個 Queue。

  • Queue 可以有多個 Consumer。

    • 不過在 Queue 裡面一個 message 只能送給一個 Consumer。

    • 也就是說,若一個 message 要送到多個 cousumer 就要在 eXchange 階段就繫結(bind)多個 Queue。

RPC (remote procedure call)

RabbitMQ - RPC

RabbitMQ RPC 機制就是把要回傳的訊息再用另一個 Queue 回授回傳。詳情請參考 Remote procedure call (RPC)

Retry (重試)

RabbitMQ - Retry

RabbitMQ - Retry 也是訊息回授的另一種組合。有興趣可參考進階文章。

RabbitMQ Retries — The Full Story
如何優雅地在RabbitMQ實現失敗重試

EOF

Last updated