RabbitMQ隨手記

In RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. But let’s not get dragged down by the details ‒ you can read more about exchanges in the third part of this tutorial. All we need to know now is how to use a default exchange identified by an empty string. This exchange is special ‒ it allows us to specify exactly to which queue the message should go. The queue name needs to be specified in the routing_key parameter
批註:
More works
消息的持久化
Note on message persistence
Marking messages as persistent doesn’t fully guarantee that a message won’t be lost. Although it tells RabbitMQ to save the message to disk, there is still a short time window when RabbitMQ has accepted a message and hasn’t saved it yet. Also, RabbitMQ doesn’t do fsync(2) for every message – it may be just saved to cache and not really written to the disk. The persistence guarantees aren’t strong, but it’s more than enough for our simple task queue. If you need a stronger guarantee then you can use publisher confirms.
消息可以持久化
In order to defeat that we can use the basic.qos method with the prefetch_count=1 setting. This tells RabbitMQ not to give more than one message to a worker at a time. Or, in other words, don’t dispatch a new message to a worker until it has processed and acknowledged the previous one. Instead, it will dispatch it to the next worker that is not still busy.
在使用工作隊列模式會有坑,比如mq默認不會去通過ack判斷work的閒忙,只是簡單的輪詢,通過加入配置可以在一定程度上避免。當然所有的措施都不是絕對的。我們能做的只是儘可能避免。
By default, RabbitMQ will send each message to the next consumer, in sequence. On average every consumer will get the same number of messages. This way of distributing messages is called round-robin. Try this out with three or more workers.
這句話的意思是,每次只發給一個wroker,因爲consumer是單數。
這給我的感覺只需要一個隊列名就可以,沒有很複雜的概念
e full messaging model in Rabbit.

有了exchange就不需要對queue進行硬編碼

進入exchange模式:發現exchange模式也是作者推薦的,
Faout:mq分配一個固定的queue給consumer,message會通過exchange進行廣播到各個通道

Direct:

可以通過routing key綁定queue,consumer也指定routing key消費指定的消息(原文中稱其而subset)
對於topic:it must be a list of words, delimited by dots,必須是點號分開的
總結:
常用的五種使用方法
分爲兩大類:
使用exchange和不使用exchange
不使用exchange
代碼中不配置exchange不代表MQ不使用。不使用exchange就需要使用隊列名來告訴producer將消息發送到哪個隊列,consumer通過routing key來知道去哪個隊列去取。

  1. 一對一
  2. 一對多:這裏不是廣播,而是多個消費者消費一個隊列,一條消息只能被一個消費者消費一次
    使用exchange
    在exchange下支持三種方式:fanout,direct,topic
    Fanout纔是真正意義上的廣播,即一條消息可以被所有的消費者消費,消費者越多,意味着隊列越多,我的理解是一個隊列只可以綁定一個消費者。也就是如果消費者很多,也意味着隊列很多。所以要了解,mq可以創建多少隊列,這個可能取決於內存,也取決與一個隊列的生命週期。爲什麼呢?隊列說到底還是buffer,內存中的buffer,內存足夠大,也就意味着
    Direct:不接收所有的消息,只接受指定的一種消息
    Topic:不接收所有消息,只接受一類消息
    消息的可靠性和持久性
    Mq可以實現一定的可靠性(ack參數)和持久性(persist參數),相應的配置就可以
    官方文檔已經寫的很清楚了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章