订阅 Service Fabric 群集级别事件

Subscribing to Service Fabric cluster level events

我正在尝试创建一个服务,该服务将更新服务结构集群中应用程序 运行 的外部服务端点列表。 (基本上我需要在我的本地 F5 负载均衡器中复制 Azure 负载均衡器。)

在上个月的 Service Fabric 问答中,团队向我指出 RegisterServiceNotificationFilterAsync

我用这个方法做了一个无状态服务,部署到我的开发集群。然后我通过 运行 ASP.NET 核心无状态服务模板创建了一个新服务。

我预计当我部署第二个服务时,断点会在我的第一个服务中命中,表明已经添加了一个服务。但是没有断点。

我在 Internet 上发现的这类事情的例子很少,所以我想在这里问一下,希望有其他人这样做过,并且可以告诉我哪里出错了。

这是我的服务的代码,它试图捕获应用程序更改:

protected override async Task RunAsync(CancellationToken cancellationToken)
{

    var fabricClient = new FabricClient();

    long? filterId = null;


    try
    {
        var filterDescription = new ServiceNotificationFilterDescription
        {
            Name = new Uri("fabric:")
        };
        fabricClient.ServiceManager.ServiceNotificationFilterMatched += ServiceManager_ServiceNotificationFilterMatched;
        filterId = await fabricClient.ServiceManager.RegisterServiceNotificationFilterAsync(filterDescription);


        long iterations = 0;

        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);

            await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
        }
    }
    finally
    {
        if (filterId != null)
            await fabricClient.ServiceManager.UnregisterServiceNotificationFilterAsync(filterId.Value);
    }



}

private void ServiceManager_ServiceNotificationFilterMatched(object sender, EventArgs e)
{
    Debug.WriteLine("Change Occured");
}

如果您有任何关于如何进行此操作的提示,我很乐意看到它们。

您需要将 MatchNamePrefix 设置为 true,如下所示:

    var filterDescription = new ServiceNotificationFilterDescription
    {
        Name = new Uri("fabric:"),
        MatchNamePrefix = true
    };

否则只会匹配特定的服务。在我的应用程序中,当此参数设置为 true.

时,我可以捕获集群范围的事件