几种常见的消息队列比较:

Message Queue

Wiki: Message Queue

Message queues provide an asynchronous communications protocol, meaning that then sender and receiver of the message do not need to interact with the message queue at the same time. Message placed onto the queue are stored until the recipient(接收者) retrieves them.

消息队列使用 异步通信 方式, 即消息发送者和接受者都不必等待彼此就绪就可完成消息发送和接收,也没有必要同时和消息队列进行交互。发送的消息会被临时存储起来,等待接收者来拿走。

Message queuing allows applications to communicate by sending messages to each other. The message queue providers a temporary message storage when the destination program is busy or not connected.

A queue is a line of things waiting to be handled - in sequential order starting at the beginning of the line. A message queue is a queue of messages sent between applications. It includes a sequence of work objects that are waiting to be processed.

一个队列就是一系列等待被处理的事情——会从队头按顺序开始处理。一个消息队列就在应用程序间发送的消息的队列,这个队列就包含了一系列等待被处理的工作对象。

A message is the data transported between the sender and the receiver application; it’s essentially a byte array with some headers on top. An example of a message could be something that tells one system to start processing a task, it could contains information about a finished task or just be a plain message.

消息就是在发送者和接收者(通常指引用程序)传输的数据,数据格式为字节数组,包含头部信息。

最简单的消息队列架构组成:

  • Producer:Client Applications -> Create Message
  • Broker:Message queue -> Stored Msg
  • Consumer: Another Applications -> Get Message

Ref:一个故事告诉你什么是消息队列

什么时候需要 Message Queue

Message Queue 主要用来解决:应用解耦、异步消息、流量削峰等问题,实现高可用,可伸缩和最终一致性架构。

解耦和可扩展性

解耦:

解耦描述的是:一个系统的某一部分有多少是依赖于另一个系统的一部分的。解耦——就是将具有依赖关系的部分进行拆分的过程,让拆分后的部分功能更加独立。

解耦系统:

当两个系统,没有进行连接(connected) 的情况下依然能够进行通讯时,说明实现了 解耦系统。解析系统仍然保持独立和完整,并且彼此之间不会有影响。

当解耦系统中的某个进程无法处理来自队列中的消息时,其他消息依然可以被添加进队列,且在系统恢复运行后会继续处理之前没有完成的任务。

图片地址

可扩展性:

开发应用程序的时候,将其拆分为不同的部分比构建一个大型的应用更好,而拆分的部分可以使用异步消息进行通信。采用这种方式就可以使得应用部分彼此独立,且可以交付给不同的开发团队完成。

消息队列保证了应用程序进程间相互隔离和彼此独立。一个进程从来不会去调用另一个进程或者发送通知给其他进程,或者按其他进程的流程进行处理。一个 Application 就是将消息发送给队列,然后继续处理。其他程序也是独立的处理他们的工作。当可以处理任务的时候他们就会从队列中拿去消息。通过这种处理消息的方式,就可以创建一个易于扩展和维护的系统了。

有哪些 Message Queues 可供选择

图片地址

RabbitMQ

RabbitMQ: Erlang 开发,对高并发、路由、负载均衡、数据持久化有很好的支持。支持的协议:AMQP,XMPP, SMTP, STOMP

ActiveMQ

ActiveMQ:Java开发,支持持久化,性能相对较差。

ZeroMQ

ZeroMQ:C语言开发,引入库即可使用,不需要服务器,速度快,但不支持持久化(宕机数据丢失)。

Redis

Redis:C语言开发,基于内存的 K-V 数据库,速度快,支持持久化,但不可靠。

RocketMQ

RocketMQ:Java开发,性能略低于kafka,但支持严格的顺序,失败重试,更多队列等。

Kafaka

Kafaka:高性能跨语言分布式发布/订阅消息队列系统,支持快速持久化、高吞吐、支持 Hadoop 数据并行加载。

AMQP

AMQP(Advanced Message Queuing Protocol): 高级消息队列协议

See Also

Thanks to the authors 🙂