RabbitMQ Interview Highlights

Original link: https://chaser520.iteye.com/blog/2428253

640?wx_fmt=png


1. How to ensure that messages are correctly transmitted to RabbitMQ?

RabbitMQ confirmation using the transmission mode, to ensure that the message is correctly transmitted RabbitMQ.

Transmission confirmation mode: the channel setting mode to confirm (transmission confirmation mode), all messages are posted channel is assigned a unique ID. Once the message is delivered to the destination queue, or after the message is written to disk (persistent message may be), the channel sends an acknowledgment to the producer (containing the unique message ID). If an internal error occurred causing RabbitMQ message is lost, it sends a nack (not acknowledged, unacknowledged) messages.

Transmission confirmation pattern is asynchronous, while waiting for the application producer acknowledgment, can continue to send the message. When the confirmation message arrives producer application, the application callback method producers will be triggered to process the confirmation message.

2. How to ensure that the message recipient consume the news?


Mechanism acknowledgment message recipient: the consumer receives each message must be acknowledged (message reception and acknowledgment message are two different operating). Consumers only confirmed the news, RabbitMQ can safely delete the message from the queue.

Here and did not use a timeout mechanism, RabbitMQ only to confirm that the need to re-send the message by connecting the Consumer interrupt. In other words, as long as the connection is not interrupted, RabbitMQ to the Consumer sufficient length of time to process the message.

The following lists some special circumstances:

  • If the consumer receives a message, disconnected or unsubscribe, RabbitMQ think that message was not distributed, then re-distributed to consumers under a subscription before confirming.

    (There may be a message repeated consumption of risks, need to re according to bizId)

  • 如果消费者接收到消息却没有确认消息,连接也未断开,则RabbitMQ认为该消费者繁忙,将不会给该消费者分发更多的消息。


3. 如何避免消息重复投递或重复消费?


在消息生产时,MQ内部针对每条生产者发送的消息生成一个inner-msg-id,作为去重和幂等的依据(消息投递失败并重传),避免重复的消息进入队列;在消息消费时,要求消息体中必须要有一个bizId(对于同一业务全局唯一,如支付ID、订单ID、帖子ID等)作为去重和幂等的依据,避免同一条消息被重复消费。


4. 消息基于什么传输?


由于TCP连接的创建和销毁开销较大,且并发数受系统资源限制,会造成性能瓶颈。RabbitMQ使用信道的方式来传输数据。信道是建立在真实的TCP连接内的虚拟连接,且每条TCP连接上的信道数量没有限制。

5. 消息如何分发?


若该队列至少有一个消费者订阅,消息将以循环(round-robin)的方式发送给消费者。每条消息只会分发给一个订阅的消费者(前提是消费者能够正常处理消息并进行确认)。


6. 消息怎么路由?


从概念上来说,消息路由必须有三部分:交换器、路由、绑定。生产者把消息发布到交换器上;绑定决定了消息如何从路由器路由到特定的队列;消息最终到达队列,并被消费者接收。


  1. 消息发布到交换器时,消息将拥有一个路由键(routing key),在消息创建时设定。

  2. 通过队列路由键,可以把队列绑定到交换器上。

  3. 消息到达交换器后,RabbitMQ会将消息的路由键与队列的路由键进行匹配(针对不同的交换器有不同的路由规则)。

    如果能够匹配到队列,则消息会投递到相应队列中;

    如果不能匹配到任何队列,消息将进入 “黑洞”。


常用的交换器主要分为一下三种:

  • direct:

    如果路由键完全匹配,消息就被投递到相应的队列

  • fanout:

    如果交换器收到消息,将会广播到所有绑定的队列上

  • topic:

    可以使来自不同源头的消息能够到达同一个队列。

    使用topic交换器时,可以使用通配符,比如:

    “*” 匹配特定位置的任意文本, “.” 把路由键分为了几部分,“#” 匹配所有规则等。

    特别注意:

    发往topic交换器的消息不能随意的设置选择键(routing_key),必须是由"."隔开的一系列的标识符组成。


7. 如何确保消息不丢失?


消息持久化的前提是:将交换器/队列的durable属性设置为true,表示交换器/队列是持久交换器/队列,在服务器崩溃或重启之后不需要重新创建交换器/队列(交换器/队列会自动创建)。


如果消息想要从Rabbit崩溃中恢复,那么消息必须:


  • 在消息发布前,通过把它的 “投递模式” 选项设置为2(持久)来把消息标记成持久化

  • 将消息发送到持久交换器

  • 消息到达持久队列


RabbitMQ确保持久性消息能从服务器重启中恢复的方式是,将它们写入磁盘上的一个持久化日志文件,当发布一条持久性消息到持久交换器上时,Rabbit会在消息提交到日志文件后才发送响应(如果消息路由到了非持久队列,它会自动从持久化日志中移除)。一旦消费者从持久队列中消费了一条持久化消息,RabbitMQ会在持久化日志中把这条消息标记为等待垃圾收集。如果持久化消息在被消费之前RabbitMQ重启,那么Rabbit会自动重建交换器和队列(以及绑定),并重播持久化日志文件中的消息到合适的队列或者交换器上。


8. 使用RabbitMQ有什么好处?


  • 应用解耦(系统拆分)

  • 异步处理(预约挂号业务处理成功后,异步发送短信、推送消息、日志记录等)

  • 消息分发

  • 流量削峰

  • 消息缓冲

  • ......


 9. 其他


RabbitMQ是 消息投递服务,在应用程序和服务器之间扮演路由器的角色,而应用程序或服务器可以发送和接收包裹。其通信方式是一种 “发后即忘(fire-and-forget)” 的单向方式。

Wherein the message comprises two parts: a payload (payload) and label (label) .

Payload data is to be transmitted, the content may be arbitrary.

Label describes a payload, the description will RabbitMQ tag, to send a message to a recipient of interest.

Guess you like

Origin blog.csdn.net/xmt1139057136/article/details/98814572
Recommended