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来知道去哪个队列去取。
- 一对一
- 一对多:这里不是广播,而是多个消费者消费一个队列,一条消息只能被一个消费者消费一次
使用exchange
在exchange下支持三种方式:fanout,direct,topic
Fanout才是真正意义上的广播,即一条消息可以被所有的消费者消费,消费者越多,意味着队列越多,我的理解是一个队列只可以绑定一个消费者。也就是如果消费者很多,也意味着队列很多。所以要了解,mq可以创建多少队列,这个可能取决于内存,也取决与一个队列的生命周期。为什么呢?队列说到底还是buffer,内存中的buffer,内存足够大,也就意味着
Direct:不接收所有的消息,只接受指定的一种消息
Topic:不接收所有消息,只接受一类消息
消息的可靠性和持久性
Mq可以实现一定的可靠性(ack参数)和持久性(persist参数),相应的配置就可以
官方文档已经写的很清楚了。