在 AMQP 模型中,消息的 producer 将 Message 发送给 Exchange,Exchange 负责交换 / 路由,将消息正确地转发给相应的 Queue。消息的 Consumer 从 Queue 中读取消息。
这个过程是异步的,Producer 和 Consumer 没有直接联系甚至可以不知道彼此的存在。
Exchange 如何进行路由的呢?
这便依靠 Routing Key,每个消息都有一个 routing Key,而每个 Queue 都可以通过一个 Binding 将自己所感兴趣的 Routing Key 告诉 Exchange,这样 Exchange 便可以将消息正确地转发给相应的 Queue。下表列出了这几个关键概念的定义。 表 1. AMQP 的几个概念
概念描述ProducerA program that writes messages to an Exchange. To do this, the program creates a message, fills the message with content, gives the message a Routing Key, and sends the message to an Exchange.Routing KeyA string that the Exchangecan use to determine to which Queuesthe message should be delivered.ExchangeAccepts messages from Producersand routes them to Queuesif the message meets the criteria expressed in a binding.BindingDefines the>Exchangeand a Queue, specifying which messages should be routed to a given QueueQueueHolds messages and delivers them to the Consumersthat subscribe to the Queue.ConsumerA program that reads messages from a Queue. A Consumercan create, subscribe to, share, use, or destroy Queueand theirBindings(as long as it has have permission to do so). 为了支持各种常见的通信模型,AMQP 定义了不同的 Exchange 类型,如下表所示 : 表 2. AMQP 定义的 Exchange 类型
Exchange 类型路由行为Fan-OutMessages are routed to every Queue bound to the Exchange, ignoring the Routing KeyDirectA message is routed only if a Queue'sBinding Keyis the same as the message's Routing KeyTopicSimilar to a Direct Exchange, but it supports multipart keys that contain multiple words separated by the "." delimiter; for instance, a message Producer can create messages with Routing Keys like usa.news, usa.weather, europe.news, and europe.weather. AMQP 目前还是一个草案,最新版本是 0.10。
QPID 是 Apache Foundation 的一个开源项目,是一个 AMQP 实现。它提供了 C++ 和 Java 两个版本的 broker,并支持多种语言的客户端,它还包括一个配置工具集。
除了完全实现了 AMQP 的基本功能之外,Qpid 还提供了一些额外的特性: