事件中心内的可用性和一致性Availability and consistency in Event Hubs

本文提供了有关 Azure 事件中心支持的可用性和一致性的信息。This article provides information about availability and consistency supported by Azure Event Hubs.

可用性Availability

Azure 事件中心分散了跨数据中心内多个故障域的群集中单个甚至计算机整个机架发生灾难性故障的风险。Azure Event Hubs spreads the risk of catastrophic failures of individual machines or even complete racks across clusters that span multiple failure domains within a datacenter. 它实现了透明的故障检测和故障转移机制,使服务将继续在有保证的服务级别内运行,并且在发生此类故障时通常不会出现明显的中断。It implements transparent failure detection and failover mechanisms such that the service will continue to operate within the assured service-levels and typically without noticeable interruptions when such failures occur.

如果在启用了可用性区域的情况下已创建了事件中心命名空间,则中断风险会进一步分散到三个物理上分离的设备,并且该服务有足够的容量预留,可立即应对整个设施的全部灾难性损失。If an Event Hubs namespace has been created with availability zones enabled, the outage risk is further spread across three physically separated facilities, and the service has enough capacity reserves to instantly cope up with the complete, catastrophic loss of the entire facility.

如果客户端应用程序将事件发送到事件中心,但未指定分区,则事件会自动在事件中心内的各个分区之间分布。When a client application sends events to an event hub without specifying a partition, events are automatically distributed among partitions in your event hub. 如果某个分区由于某种原因而不可用,则会在其余分区之间分布事件。If a partition isn't available for some reason, events are distributed among the remaining partitions. 此行为可实现最大运行时间量。This behavior allows for the greatest amount of up time. 对于需要最大运行时间的用例,此模型是首选模型,而不是将事件发送到特定分区。For use cases that require the maximum up time, this model is preferred instead of sending events to a specific partition.

一致性Consistency

在某些方案中,事件的排序可能十分重要。In some scenarios, the ordering of events can be important. 例如,可能希望后端系统先处理更新命令,再处理删除命令。For example, you may want your back-end system to process an update command before a delete command. 在此方案中,客户端应用程序将事件发送到特定分区,以便保留排序。In this scenario, a client application sends events to a specific partition so that the ordering is preserved. 当使用者应用程序从分区使用这些事件时,将按顺序读取它们。When a consumer application consumes these events from the partition, they are read in order.

使用此配置,请记住,如果发送到的特定分区不可用,则会收到错误响应。With this configuration, keep in mind that if the particular partition to which you are sending is unavailable, you will receive an error response. 作为对比,如果未与单个分区关联,则事件中心服务会将事件发送到下一个可用分区。As a point of comparison, if you don't have an affinity to a single partition, the Event Hubs service sends your event to the next available partition.

因此,如果高可用性最为重要,则不要以特定分区为目标(使用分区 ID/分区键)。Therefore, if high availability is most important, don't target a specific partition (using partition ID/key). 使用分区 ID/键会将事件中心的可用性降级到分区级别。Using partition ID/key downgrades the availability of an event hub to partition-level. 在这种情况下,需在可用性(无分区 ID/键)和一致性(将事件固定到特定分区)之间做出明确选择。In this scenario, you are making an explicit choice between availability (no partition ID/key) and consistency (pinning events to a specific partition). 有关事件中心内分区的详细信息,请参阅分区For detailed information about partitions in Event Hubs, see Partitions.

附录Appendix

发送事件时不指定分区Send events without specifying a partition

我们建议在不设置分区信息的情况下将事件发送到事件中心,以允许事件中心服务在分区之间均衡负载。We recommend sending events to an event hub without setting partition information to allow the Event Hubs service to balance the load across partitions. 请参阅以下快速入门,了解如何以不同编程语言实现此目的。See the following quick starts to learn how to do so in different programming languages.

将事件发送到特定分区Send events to a specific partition

本部分介绍如何使用不同的编程语言将事件发送到特定分区。In this section, you learn how to send events to a specific partition using different programming languages.

若要将事件发送到特定分区,请使用 EventHubProducerClient.CreateBatchAsync 方法通过在 CreateBatchOptions 中指定 PartitionIdPartitionKey 来创建批处理。To send events to a specific partition, create the batch using the EventHubProducerClient.CreateBatchAsync method by specifying either the PartitionId or the PartitionKey in CreateBatchOptions. 以下代码通过指定分区键将一批事件发送到特定分区。The following code sends a batch of events to a specific partition by specifying a partition key. 事件中心确保共享分区键值的所有事件存储在一起,并按到达顺序进行传递。Event Hubs ensures that all events sharing a partition key value are stored together and delivered in order of arrival.

var batchOptions = new CreateBatchOptions { PartitionKey = "cities" };
using var eventBatch = await producer.CreateBatchAsync(batchOptions);

还可以通过在 SendEventOptions 中指定 PartitionId 或 PartitionKey 来使用 EventHubProducerClient.SendAsync 方法 。You can also use the EventHubProducerClient.SendAsync method by specifying either PartitionId or PartitionKey in SendEventOptions.

var sendEventOptions  = new SendEventOptions { PartitionKey = "cities" };
// create the events array
producer.SendAsync(events, sendOptions)

后续步骤Next steps

访问以下链接可以了解有关事件中心的详细信息:You can learn more about Event Hubs by visiting the following links: