什么是 Azure 服务总线?What is Azure Service Bus?

Azure 服务总线是一个完全托管的企业消息代理,其中包含消息队列和发布订阅主题。Azure Service Bus is a fully managed enterprise message broker with message queues and publish-subscribe topics. 服务总线用于分离应用程序和服务,提供以下优势:Service Bus is used to decouple applications and services from each other,providing the following benefits:

  • 跨争用工作节点实现工作负载均衡Load-balancing work across competing workers
  • 跨服务和应用程序边界安全路由和传输数据和控制Safely routing and transferring data and control across service and application boundaries
  • 协调需要高度可靠性的事务性工作Coordinating transactional work that requires a high-degree of reliability

概述Overview

数据通过消息在不同的应用程序和服务之间传输。Data is transferred between different applications and services using messages. 消息是用元数据修饰的容器,它包含数据。A message is a container decorated with metadata, and contains data. 数据可以是任何类型的信息,包括以常用格式编码的结构化数据,例如:JSON、XML、Apache Avro 和纯文本。The data can be any kind of information, including structured data encoded with the common formats such as the following ones: JSON, XML, Apache Avro, Plain Text.

一些常见的消息传送方案包括:Some common messaging scenarios are:

  • 消息Messaging. 传输业务数据,例如销售或采购订单、日志或库存变动。Transfer business data, such as sales or purchase orders, journals, or inventory movements.

  • 分离应用程序Decouple applications. 提高应用程序和服务的可靠性和可伸缩性。Improve reliability and scalability of applications and services. 制作者和使用者不必同时在线或可用,且负载经过平衡,这样一来,流量高峰不会使服务负担过重。Producer and consumer don't have to be online or readily available at the same time, and load is leveled such that traffic spikes don't overtax a service.

  • 负载均衡Load Balancing. 允许多个竞争性使用者同间从队列读取内容,每个使用者都安全地获取对特定消息的独占所有权。Allow for multiple competing consumers to read from a queue at the same time, each safely obtaining exclusive ownership to specific messages.

  • 主题和订阅Topics and subscriptions. 发布服务器和订阅服务器之间启用 1:n 关系,使订阅服务器可以从已发布的消息流中选择特定消息。Enable 1:n relationships between publishers and subscribers, allowing subscribers to select particular messages from a published message stream.

  • TransactionsTransactions. 允许你执行多个操作,所有操作都在原子事务的作用域中执行。Allows you to do several operations, all in the scope of an atomic transaction. 例如,可以在事务的作用域中执行以下操作。For example, the following operations can be done in the scope of a transaction.

    1. 从一个队列获取消息。Obtain a message from one queue.
    2. 将处理结果发布到一个或多个不同的队列。Post results of processing to one or more different queues.
    3. 从原始队列移动输入消息。Move the input message from the original queue.

    仅在成功时才对下游使用者显示结果,包括成功处置输入消息,允许使用一次性处理语义。The results become visible to downstream consumers only upon success, including the successful settlement of input message, allowing for once-only processing semantics. 对于更大的解决方案上下文中的补偿事务模式,此事务模型是一个可靠的基础。This transaction model is a robust foundation for the compensating transactions pattern in the greater solution context.

  • 消息会话Message sessions. 对于需要严格消息排序或消息延迟的工作流和多路复用传输,实现大规模协调。Implement high-scale coordination of workflows and multiplexed transfers that require strict message ordering or message deferral.

如果熟悉 Apache ActiveMQ 等其他消息代理,服务总线的概念与你已知的概念相似。If you're familiar with other message brokers like Apache ActiveMQ, Service Bus concepts are similar to what you know. 服务总线是一个平台即服务 (PaaS) 产品,一个关键区别在于你不用担心以下操作。As Service Bus is a platform-as-a-service (PaaS) offering, a key difference is that you don't need to worry about the following actions. Azure 会为你完成这些琐事。Azure takes care of those chores for you.

  • 存放日志和管理磁盘空间Placing logs and managing disk space
  • 处理备份Handling backups
  • 持续修补操作系统或产品Keeping the operating systems or the products patched
  • 担心硬件失败Worrying about hardware failures
  • 故障转移到保留计算机Failing over to a reserve machine

符合标准和协议Compliance with standards and protocols

服务总线的主要网络协议是高级消息队列协议 (AMQP) 1.0,它是一项开放式 ISO/IEC 标准。The primary wire protocol for Service Bus is Advanced Messaging Queueing Protocol (AMQP) 1.0, an open ISO/IEC standard. 它允许客户编写针对服务总线和本地代理(例如 ActiveMQ 或 RabbitMQ)的应用程序。It allows customers to write applications that work against Service Bus and on-premises brokers such as ActiveMQ or RabbitMQ. 如果你想生成这样的抽象,AMQP 协议指南提供了详细信息。The AMQP protocol guide provides detailed information in case you want to build such an abstraction.

服务总线高级版完全兼容 Java/Jakarta EE Java 消息服务 (JMS) 2.0 API。Service Bus Premium is fully compliant with the Java/Jakarta EE Java Message Service (JMS) 2.0 API. 而服务总线标准版支持专注于队列的 JMS 1.1 子网。And, Service Bus Standard supports the JMS 1.1 subset focused on queues. JMS 是消息代理的一般抽象,可与许多应用程序和框架(包括热门的 Spring 框架)集成。JMS is a common abstraction for message brokers and integrates with many applications and frameworks, including the popular Spring framework. 若要从其他代理切换到 Azure 服务总线,重新创建队列和主题的拓扑,并更改客户端提供程序依赖关系和配置即可。To switch from other brokers to Azure Service Bus, you just need to recreate the topology of queues and topics, and change the client provider dependencies and configuration. 有关示例,请参阅 ActiveMQ 迁移指南For an example, see the ActiveMQ migration guide.

概念和术语Concepts and terminology

本部分讨论了服务总线的概念和术语。This section discusses concepts and terminology of Service Bus.

命名空间Namespaces

命名空间是一个适用于所有消息传送组件的容器。A namespace is a container for all messaging components. 多个队列和主题可以位于一个命名空间中,命名空间通常用作应用程序容器。Multiple queues and topics can be in a single namespace, and namespaces often serve as application containers.

队列Queues

消息可以发送到队列,也可以从其接收。Messages are sent to and received from queues. 在能够使用接收应用程序接收并处理消息之前,可以通过队列来存储消息。Queues store messages until the receiving application is available to receive and process them.

队列

队列中的消息会排队,并在到达时加盖时间戳。Messages in queues are ordered and timestamped on arrival. 代理接受消息后,该消息将长期保留在三冗余存储中;如果命名空间启用了区域,则这些冗余存储会分布在多个可用性区域中。Once accepted by the broker, the message is always held durably in triple-redundant storage, spread across availability zones if the namespace is zone-enabled. 将消息报告给客户端后,服务总线不会将消息保留在内存或易失存储中。Service Bus never leaves messages in memory or volatile storage after they've been reported to the client as accepted.

消息以拉取模式传送,即仅按请求传送消息。Messages are delivered in pull mode, only delivering messages when requested. 不同于其他某些云队列的繁忙轮询模式,拉取操作可能会长期存续,消息可用时才会完成。Unlike the busy-polling model of some other cloud queues, the pull operation can be long-lived and only complete once a message is available.

主题Topics

也可通过主题发送和接收消息。You can also use topics to send and receive messages. 队列通常用于点到点通信,而主题则用于发布/订阅方案。While a queue is often used for point-to-point communication, topics are useful in publish/subscribe scenarios.

主题

主题可以有多个独立的订阅,这些订阅附加到主题,其他方面与来自接收方的队列完全一样。Topics can have multiple, independent subscriptions, which attach to the topic and otherwise work exactly like queues from the receiver side. 主题的订阅者可以收到发送到该主题的每个消息的副本。A subscriber to a topic can receive a copy of each message sent to that topic. 订阅是命名实体。Subscriptions are named entities. 订阅默认持久存续,但可为其配置过期时间,并在过期后自动将其删除。Subscriptions are durable by default, but can be configured to expire and then be automatically deleted. 通过 JMS API,服务总线高级版还允许创建在连接期间存在的易失订阅。Via the JMS API, Service Bus Premium also allows you to create volatile subscriptions that exist for the duration of the connection.

你可以定义订阅的规则。You can define rules on a subscription. 订阅规则有一个筛选器,用于定义要复制到订阅中的消息应满足的条件,以及可以修改消息元数据的可选操作。 A subscription rule has a filter to define a condition for the message to be copied into the subscription and an optional action that can modify message metadata. 有关详细信息,请参阅主题筛选器和操作For more information, see Topic filters and actions. 此功能在以下情况下很有用:This feature is useful in the following scenarios:

  • 不要让订阅接收发送到某个主题的所有消息。You don't want a subscription to receive all messages sent to a topic.
  • 最好在消息通过订阅时使用额外的元数据来标记消息。You want to mark up messages with extra metadata when they pass through a subscription.

高级功能Advanced features

服务总线包括用于解决更复杂消息传送问题的高级功能。Service Bus includes advanced features that enable you to solve more complex messaging problems. 以下部分说明了这几个功能。The following sections describe several of these features.

消息会话Message sessions

若要在服务总线中创建先进先出 (FIFO) 保证,请使用会话。To create a first-in, first-out (FIFO) guarantee in Service Bus, use sessions. 使用消息会话,可以排他、有序的方式处理一系列无限多的相关消息。Message sessions enable exclusive, ordered handling of unbounded sequences of related messages. 为了允许在高度缩放的高可用性系统中处理会话,会话功能还允许存储会话状态,以便在处理程序间安全地移动会话。To allow for handling sessions in high-scale, high-availability systems, the session feature also allows for storing session state, which allows sessions to safely move between handlers. 有关详细信息,请参阅消息会话:先进先出 (FIFO)For more information, see Message sessions: first in, first out (FIFO).

自动转发Autoforwarding

自动转发功能将序列或订阅链接到相同命名空间中的主题。The autoforwarding feature chains a queue or subscription to another queue or topic inside the same namespace. 使用此功能时,服务总线会自动将消息从队列或订阅移动到目标队列或主题。When you use this feature, Service Bus automatically moves messages from a queue or subscription to a target queue or topic. 这样的移动都通过事务完成。All such moves are done transactionally. 有关详细信息,请参阅使用自动转发链接服务总线实体For more information, see Chaining Service Bus entities with autoforwarding.

死信队列Dead-letter queue

所有服务总线队列和主题订阅都有关联的死信队列 (DLQ)。All Service Bus queues and topic subscriptions have an associated dead-letter queue (DLQ). DLQ 包含满足以下条件的消息:A DLQ holds messages that meet the following criteria:

  • 无法成功传递到任何接收方。They can't be delivered successfully to any receiver.
  • 已超时。They timed out.
  • 由接收方应用程序显式排除。They're explicitly sidelined by the receiving application.

死信队列中的消息将带有关于其进入队列原因的批注。Messages in the dead-letter queue are annotated with the reason why they've been placed there. 死信队列具有特殊的终结点,其他方面与任何常规队列类似。The dead-letter queue has a special endpoint, but otherwise acts like any regular queue. 应用程序或工具可以浏览 DLQ 或取消排队。An application or tool can browse a DLQ or dequeue from it. 你也可以自动转发出死信队列。You can also autoforward out of a dead-letter queue. 有关详细信息,请参阅服务总线死信队列概述For more information, see Overview of Service Bus dead-letter queues.

计划的传递Scheduled delivery

可以将消息提交到队列或主题以进行延迟处理,并设置消息可用的时间。You can submit messages to a queue or topic for delayed processing, setting a time when the message will become available for consumption. 还可以取消计划的消息。Scheduled messages can also be canceled. 有关详细信息,请参阅计划的消息For more information, see Scheduled messages.

消息延迟Message deferral

队列或订阅客户端可以延迟一段时间检索收到的消息。A queue or subscription client can defer retrieval of a received message until a later time. 消息可能是按预期顺序发布的,而客户端希望等到收到另一条消息。The message may have been posted out of an expected order and the client wants to wait until it receives another message. 延迟的消息保留在队列或订阅中,必须使用其服务分配的序列号显式重新激活。Deferred messages remain in the queue or subscription and must be reactivated explicitly using their service-assigned sequence number. 有关详细信息,请参阅消息延迟For more information, see Message deferral.

批处理Batching

通过客户端批处理,队列或主题客户端可以累积一组消息并一起传输它们。Client-side batching enables a queue or topic client to accumulate a set of messages and transfer them together. 这样做通常是为了节约带宽或增加吞吐量。It's often done to either save bandwidth or to increase throughput. 有关详细信息,请参阅客户端批处理For more information, see Client-side batching.

事务Transactions

一个事务将两个或更多操作组合成执行作用域。A transaction groups two or more operations together into an execution scope. 服务总线允许对针对单个事务作用域内的多个消息传递实体的操作进行分组。Service Bus allows you to group operations against multiple messaging entities within the scope of a single transaction. 消息实体可以是队列、主题或订阅。A message entity can be a queue, topic, or subscription. 有关详细信息,请参阅服务总线事务处理概述For more information, see Overview of Service Bus transaction processing.

出现空闲队列时自动删除Autodelete on idle

可以使用“出现空闲队列时自动删除”功能指定一个空闲时间间隔,该时间间隔过后系统会自动删除队列或主题订阅。Autodelete on idle enables you to specify an idle interval after which a queue or topic subscription is automatically deleted. 最短持续时间为 5 分钟。The minimum duration is 5 minutes. 有关详细信息,请参阅 QueueDescription.AutoDeleteOnIdle 属性For more information, see the QueueDescription.AutoDeleteOnIdle Property.

重复检测Duplicate detection

重复检测功能使发送方能够再次发送相同的消息,并让代理能够删除可能的重复项。The duplicate detection feature enables the sender to resend the same message again and for the broker to drop a potential duplicate. 重复检测基于跟踪消息的 message-id 属性,这意味着,当重新发送消息时,应用程序需要注意使用相同的值,该值可能是直接从某些特定于应用程序的上下文中派生的。The duplicate detection is based on tracking the message-id property of a message, meaning the application needs to take care to use the same value when resending the message, which might be directly derived from some application-specific context. 有关详细信息,请参阅重复检测For more information, see Duplicate detection.

异地灾难恢复Geo-disaster recovery

在 Azure 区域遭遇停机时,可以使用灾难恢复功能在其他区域或数据中心进行数据处理,以实现连续运行。When an Azure region experiences downtime, the disaster recovery feature enables data processing to continue operating in a different region or data center. 此功能在次要区域中保留可用命名空间的结构化镜像,并允许命名空间标识切换到辅助命名空间。The feature keeps a structural mirror of a namespace available in the secondary region and allows the namespace identity to switch to the secondary namespace. 可用性事件平息后,已发布的消息仍保留在以前的主命名空间中以便恢复。Already posted messages remain in the former primary namespace for recovery once the availability episode subsides. 有关详细信息,请参阅 Azure 服务总线异地灾难恢复For more information, see Azure Service Bus Geo-disaster recovery.

安全性Security

服务总线支持标准 AMQP 1.0HTTP 或 REST 协议及其各自的安全功能,包括传输层安全性 (TLS)。Service Bus supports standard AMQP 1.0 and HTTP or REST protocols and their respective security facilities, including transport level security (TLS). 可以使用共享访问签名Azure Active Directory 基于角色的安全性,向客户端授予访问权限。Clients can be authorized for access using Shared Access Signature or Azure Active Directory role-based security.

为针对意外的流量进行保护,服务总线提供了多种安全功能,例如 IP 防火墙和与虚拟网络的集成。For protection against unwanted traffic, Service Bus provides security features such as IP firewall and integration with virtual networks.

客户端库Client libraries

可通过 Azure SDK 使用完全受支持的服务总线客户端库。Fully supported Service Bus client libraries are available via the Azure SDK.

Azure 服务总线的主要协议是 AMQP 1.0,可从兼容 AMQP 1.0 的任何协议客户端使用它。Azure Service Bus' primary protocol is AMQP 1.0 and it can be used from any AMQP 1.0 compliant protocol client. 若干开源 AMQP 客户端具有显式演示服务总线互操作性的示例。Several open source AMQP clients have samples that explicitly demonstrate Service Bus interoperability. 查看 AMQP 1.0 协议指南,了解如何通过 AMQP 1.0 客户端直接使用服务总线功能。Review the AMQP 1.0 protocol guide to understand how to use Service Bus' features with AMQP 1.0 clients directly.

Language Library
Java Apache Qpid Proton-J
C/C++ Azure uAMQP C, Apache Qpid Proton-C
Python Azure uAMQP for Python, Apache Qpid Proton Python
PHP Azure uAMQP for PHP
Ruby Apache Qpid Proton Ruby
Go Azure Go AMQP, Apache Qpid Proton Go
C#/F#/VB AMQP .NET Lite, Apache NMS AMQP
JavaScript/Node Rhea

集成Integration

服务总线与许多 Azure 服务完全集成,例如:Service Bus fully integrates with many Azure services, for instance:

后续步骤Next steps

有关服务总线消息传送入门的内容,请参阅以下文章:To get started using Service Bus messaging, see the following articles: