你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ChangeFeedEventHost 类

定义

注意

Switch to the ChangeFeedProcessorBuilder class and use the BuildAsync method for building the change feed processor host or the BuildEstimatorAsync method for building the remaining work estimator.

用于跨观察程序分发更改源事件的简单主机,从而允许这些观察程序缩放。 它在其实例之间分配负载,并允许动态缩放:

  • 分区集合中的分区分布在实例/观察器之间。
  • 新实例从现有实例获取租约,使分布相等。
  • 如果实例死亡,租约将分布在剩余实例上。 当分区计数较高时,一个主机/VM 无法处理那么多的更改源事件时,此功能非常有用。 客户端应用程序需要通过 ChangeFeedEventHost 实现 IChangeFeedObserver 和注册处理器实现。
[System.Obsolete("Switch to the ChangeFeedProcessorBuilder class and use the BuildAsync method for building the change feed processor host or the BuildEstimatorAsync method for building the remaining work estimator.")]
public class ChangeFeedEventHost
[<System.Obsolete("Switch to the ChangeFeedProcessorBuilder class and use the BuildAsync method for building the change feed processor host or the BuildEstimatorAsync method for building the remaining work estimator.")>]
type ChangeFeedEventHost = class
Public Class ChangeFeedEventHost
继承
ChangeFeedEventHost
属性

示例

class DocumentFeedObserver : IChangeFeedObserver
{
    private static int s_totalDocs = 0;
    public Task OpenAsync(ChangeFeedObserverContext context)
    {
        Console.WriteLine("Worker opened, {0}", context.PartitionKeyRangeId);
        return Task.CompletedTask;  // Requires targeting .NET 4.6+.
    }
    public Task CloseAsync(ChangeFeedObserverContext context, ChangeFeedObserverCloseReason reason)
    {
        Console.WriteLine("Worker closed, {0}", context.PartitionKeyRangeId);
        return Task.CompletedTask;
    }
    public Task ProcessChangesAsync(ChangeFeedObserverContext context, IReadOnlyList<Document> docs)
    {
        Console.WriteLine("Change feed: total {0} doc(s)", Interlocked.Add(ref s_totalDocs, docs.Count));
        return Task.CompletedTask;
    }
}
static async Task StartChangeFeedHost()
{
    string hostName = Guid.NewGuid().ToString();
    DocumentCollectionInfo documentCollectionLocation = new DocumentCollectionInfo
    {
        Uri = new Uri("https://YOUR_SERVICE.documents.azure.com:443/"),
        MasterKey = "YOUR_SECRET_KEY==",
        DatabaseName = "db1",
        CollectionName = "documents"
    };
    DocumentCollectionInfo leaseCollectionLocation = new DocumentCollectionInfo
    {
        Uri = new Uri("https://YOUR_SERVICE.documents.azure.com:443/"),
        MasterKey = "YOUR_SECRET_KEY==",
        DatabaseName = "db1",
        CollectionName = "leases"
    };
    Console.WriteLine("Main program: Creating ChangeFeedEventHost...");
    ChangeFeedEventHost host = new ChangeFeedEventHost(hostName, documentCollectionLocation, leaseCollectionLocation);
    await host.RegisterObserverAsync<DocumentFeedObserver>();
    Console.WriteLine("Main program: press Enter to stop...");
    Console.ReadLine();
    await host.UnregisterObserversAsync();
}

注解

它使用辅助文档集合来管理分区的租约。 每个 EventProcessorHost 实例执行以下两个任务:1) 续订租约:它跟踪主机当前拥有的租约,并持续续订租约。 2) 获取租约:每个实例持续轮询所有租约,以检查如果系统应获取任何租约才能进入均衡状态。

构造函数

ChangeFeedEventHost(String, DocumentCollectionInfo, DocumentCollectionInfo)
已过时.

初始化 ChangeFeedEventHost 类的新实例。

ChangeFeedEventHost(String, DocumentCollectionInfo, DocumentCollectionInfo, ChangeFeedHostOptions)
已过时.

初始化 ChangeFeedEventHost 类的新实例。

ChangeFeedEventHost(String, DocumentCollectionInfo, DocumentCollectionInfo, ChangeFeedOptions, ChangeFeedHostOptions)
已过时.

初始化 ChangeFeedEventHost 类的新实例。

属性

HostName
已过时.

获取主机名,该主机名是实例的唯一名称。

方法

GetEstimatedRemainingWork()
已过时.

异步检查当前现有租约,并计算每个租用分区的剩余工时估计值。

RegisterObserverAsync<T>()
已过时.

向主机异步注册观察程序接口实现。 此方法还会启动主机,并使主机能够开始参与分区分发过程。

RegisterObserverFactoryAsync(IChangeFeedObserverFactory)
已过时.

向主机异步注册观察程序工厂实现。 此方法还会启动主机,并使主机能够开始参与分区分发过程。

UnregisterObserversAsync()
已过时.

异步关闭主机实例。 此方法维护当前保留的所有分区的租约,并通过使用 对象调用 方法使每个主机实例能够干净地关闭。

适用于