【Azure 事件中心】从Azure Event Hub中消费数据,如何查看当前消费客户端消费数据的Offset和SequenceNumber呢(消息偏移量和序列号)?

问题描述

当通过Azure Event Hub SDK消费Event Hub中的消息时,必须指定一个Storage Account(存储账号)用于保存 Checkpoint (检查点)。

 

比如在C#代码中,需要指定Storage Account Connection String 和一个Blob Container的名称。其他语言代码也是一样,都需要指定Storage Account。

复制代码
    private const string ehubNamespaceConnectionString = "<EVENT HUBS NAMESPACE - CONNECTION STRING>";
    private const string eventHubName = "<EVENT HUB NAME>";
    private const string blobStorageConnectionString = "<AZURE STORAGE CONNECTION STRING>";
    private const string blobContainerName = "<BLOB CONTAINER NAME>";

...... 

  static async Task Main()
    {
        // Read from the default consumer group: $Default
        string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;

        // Create a blob container client that the event processor will use 
        storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName);

        // Create an event processor client to process events in the event hub
        processor = new EventProcessorClient(storageClient, consumerGroup, ehubNamespaceConnectionString, eventHubName);

        // Register handlers for processing events and handling errors
        processor.ProcessEventAsync += ProcessEventHandler;
        processor.ProcessErrorAsync += ProcessErrorHandler;

        // Start the processing
        await processor.StartProcessingAsync();

        // Wait for 30 seconds for the events to be processed
        await Task.Delay(TimeSpan.FromSeconds(30));

        // Stop the processing
        await processor.StopProcessingAsync();
    }
复制代码

 

那么,在Storage Account中如何查看Offest 和 Sequence Number的值呢?

 

解答如下

 

 如上图,在Azure门户页面中,进入当前使用的Storage Account页面:

  1. 在指定的Container中,SDK会自动创建一个以Event Hub Namespace主机域名为名的Folder
  2. 然后是Event Hub -> 消费组(默认为 $default) --> Checkpint --> 分区号(从0开始,1,2 ...) 
  3. 在以分区号为名称的Blob的元数据(Metadata)记录了sequencenumber 和 offset的值。

 

PS: 当删除或修改这个值后,如果重启Event Hub的消费端,这会导致数据从新的OFFSET开始获取。

 

参考资料

Checkpoint: https://docs.azure.cn/zh-cn/event-hubs/event-processor-balance-partition-load#checkpointing

向 Azure 事件中心发送事件及从 Azure 事件中心接收事件 - .NET (Azure.Messaging.EventHubs): https://docs.azure.cn/zh-cn/event-hubs/event-hubs-dotnet-standard-getstarted-send#receive-events

 

posted @   路边两盏灯  阅读(254)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-08-17 【应用服务 App Service】当使用EntityFrameWorkCore访问Sql Server数据库时,在Azure App Service会出现Cannot create a DbSet for ** because this type is not included in the model for the context的错误
2020-08-17 【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
2020-08-17 【Azure 批处理 Azure Batch】在Azure Batch中如何通过开始任务自动安装第三方依赖的一些软件(Windows环境)
点击右上角即可分享
微信分享提示