Subscribing to Service Fabric cluster level events

China☆狼群 提交于 2019-12-01 02:41:12

问题


I am trying to create a service that will update an external list of Service Endpoints for applications running in my service fabric cluster. (Basically I need to replicate the Azure Load Balancer in my on premises F5 Load Balancer.)

During last month's Service Fabric Q&A, the team pointed me at RegisterServiceNotificationFilterAsync.

I made a stateless service using this method, and deployed it to my development cluster. I then made a new service by running the ASP.NET Core Stateless service template.

I expected that when I deployed the second service, the break point would hit in my first service, indicating that a service had been added. But no breakpoint was hit.

I have found very little in the way of examples for this kind of thing on the internet, so I am asking here hopping that someone else has done this and can tell me where I went wrong.

Here is the code for my service that is trying to catch the application changes:

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");
}

If you have any tips on how to get this going, I would love to see them.


回答1:


You need to set the MatchNamePrefix to true, like this:

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

otherwise it will only match specific services. In my application I can catch cluster wide events when this parameter is set to true.



来源:https://stackoverflow.com/questions/45247218/subscribing-to-service-fabric-cluster-level-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!