了解关键的消息模型及它们之间的不同
Understanding the key models for messaging systems and the differences between them.
随着收集或处理数据的应用程序和应用程序组件的数量和种类的激增,构建连接用于将数据从创建数据的地方传输到所有不同处理数据的地方,这是应用程序开发的基本部分。在这种情形下消息系统是一项关键的技术。
With the explosion in the number and variety of applications and application components that collect or process data, building the connections that move data from where it is created to all of the different places that it is processed is a fundamental part of application development. Messaging is a critical technology for connecting data in this environment.
消息传递是通过中间系统将数据记录从一个系统传输到另一个系统,与直接连接不同,发送者知道向哪些接收者发送数据并直接连接到每个接受者,消息的解决方案是将数据的发送同数据的消费进行解耦,发送者不需要知道哪些接收者会看到数据或者何时会看到。
Messaging is the transmission of data records from one system to another via an intermediate system. In contrast to direct connections, where senders know the receivers to which data needs to be sent and connect directly to each receiver, messaging solutions decouple the sending of data from data consumption. Senders do not know which receivers will see that data or when they will see it.
消息传递解决方案通过为架构师和开发人员提供标准化的、可重用的组件来健壮地处理数据流,从而简化了应用程序的开发,这样他们就可以专注于应用程序中的核心逻辑的开发,除了路由数据,消息系统也可以提供一些重要的特性来提高可靠性及易处理性比如容错、日志记录及分布式的处理
Messaging solutions simplify development of application by giving architects and developers a standardized, reusable component for robustly handling the flow of data so that they can focus on the core logic in their applications. In addition to routing data, messaging systems can also provide important features such as fault tolerance, logging, and distributed processing to improve manageability and reliability.
消息是一种广泛的概念包含几种模型 这些模型是基于数据是怎样从发送者发送给接收者的,两种主要的消息模型是队列及发布-订阅(pub-sub)消息
Messaging is a broad term that covers several models that differ based on how data is moved from senders to recipients. The two primary messaging models are message queuing and publish-subscribe (often referred to as pub-sub) messaging.
消息队列
Message Queuing
消息队列针对于特定的场景设计的比如任务列表或者是工作队列,一个消息队列接收入队的消息并确保针对给定Topic或者Channel的每条消息都被发送且仅被一个消费者处理 比如一个消息队列的场景: 可以开工资支票——每个工资都开一次,而且只能开一次,这很重要,但是哪台机器处理每张支票并不重要。
Message queuing is designed for scenarios such as a tasklist or workqueue. A message queue receives incoming messages and ensures that each message for a given topic or channel is delivered to and processed by exactly one consumer. For example, a message queuing scenario could be issuing paychecks–it is critical that every paycheck is issued once and only once, but it does not matter which exact machine does the processing of each check.
通过为每个主题添加多个消费者,消息队列可以支持高消费率 但是只有一个消费者将接收关于该主题的每个消息。哪个消费者接收哪个消息由消息队列的实现决定,为了确保消息只被一个消费者处理,一旦消息被接收并且被消费者处理后将会从队列中被删除(一旦一个消息已经被确认从消息系统中被消费)
Message queues can support high rates of consumption by adding multiple consumers for each topic, but only one consumer will receive each message on the topic. Which consumer receives which message is determined by the implementation of the message queue. To ensure that a message is only processed by one consumer, each message is deleted from the queue once it has been received and processed by a consumer (i.e. once a consumer has acknowledged consumption of the message to the messaging system).
消息队列支持使用一些重要的场景比如每条消息只允许被处理一次,但是没有保证处理消息必要性,在网络或者消费失败的场景下,消息队列将会在之后重新发送这条消息(针对于相同的消费者不是必要的)因此,该消息可能会被处理得不正常。
Message queues support use cases where it is important that each message is processed exactly once, but it is not necessary to process messages in order. In the case of network or consumer failures, the message queue will try resending the message at a later time (not necessarily to the same consumer) and as a result that message may be processed out of order.
发布-订阅消息
Publish-Subscribe Messaging
像消息队列,发布-订阅消息将信息从发送者传递给消费者,但是与消息队列不同的是,Pub-Sub允许Topic中的每一个消息可以被多个消费者消费,此外,Pub-Sub 消息模型确保每个消费者以消息传递系统接收消息的确切顺序接收主题中的消息。
Like message queuing, publish-subscribe messaging moves information from senders to consumers. However, in contrast to message queuing, publish-subscribe messaging allows multiple consumers to receive each message in a topic. Further, pub-sub messaging ensures that each consumer receives messages in a topic in the exact order in which they were received by the messaging system.
Pub-Sub消息系统支持这些场景:一个消息需要被多个消费者消费,每个消费者接收的消息是有序的,比如 许多人和应用程序都可以使用股票行情服务,他们都希望接收关于所选主题的所有消息,对于这些用户来说,他们在订单中接收消息是很重要的——看到一个股票的高价之后是一个低价,这与看到一个低价之后是一个高价有着非常不同的含义。
Publish-subscribe messaging systems support use cases that require that multiple consumers receive each message and/or that messages are received in order by each consumer. For example, a stock ticker service can be used by a large number of people and applications, all of whom want to receive all messages on their topics of choice. It is important for these users that they receive messages in order–seeing a high price followed by a low price for a stock means something very different from seeing a low price followed by a high price.
Pub-Sub的使用场景是与有状态的系统紧密相关的,有状态的系统会关注消息的顺序因为消息的顺序决定了这个系统的状态,并将因此影响应用程序需要应用的任何处理逻辑的正确性。
Publish-subscribe use cases are frequently associated with stateful applications. Stateful applications care about the order of messages received because the ordering of messages determines the state of the application and will as a result impact the correctness of whatever processing logic the application needs to apply.
总结一下:
Queue适用于一条消息只能被消费一次,消费完成之后会被从Queue中删除掉,Pub-Sub适用的场景是一条消息可以被发送给不同的消费者,可以被消费多次,而且每个消费者消费的顺序是得到保证的,但是Queue就不同,然Queue支持可伸缩性。
主流的消息框架
Messaging Products
Both message queuing and pub-sub messaging models are necessary in modern applications. There are a number of technologies that support message queuing, publish-subscribe messaging, or both. Technologies such as Apache ActiveMQ, Amazon SQS, IBM Websphere MQ, RabbitMQ, and RocketMQ were developed primarily for message queuing use cases, while systems such as Amazon SNS, Apache Kafka and Google Cloud Pub/Sub were designed primarily for publish-subscribe use cases. There are also solutions such as Apache Pulsar that provide support for both message queuing and pub-sub messaging.
网友评论