How can I dynamically discover services hosted in service fabric from API management?

前端 未结 1 1075
既然无缘
既然无缘 2020-11-28 13:53
  1. Lets say that I have service A and B hosted in service fabric cluster. They listen (inside the cluster) on port 7001 and 7002 respectively.
  2. Lets say that I co
相关标签:
1条回答
  • 2020-11-28 14:40

    Here's a way to discover services and endpoints running in the cluster. (Keep in mind that you'll need to monitor changes too.)

    private static void ListEndpoints()
    {
        var resolver = ServicePartitionResolver.GetDefault();
        var fabricClient = new FabricClient();
        var apps = fabricClient.QueryManager.GetApplicationListAsync().Result;
        foreach (var app in apps)
        {
            Console.WriteLine($"Discovered application:'{app.ApplicationName}");
    
            var services = fabricClient.QueryManager.GetServiceListAsync(app.ApplicationName).Result;
            foreach (var service in services)
            {
                Console.WriteLine($"Discovered Service:'{service.ServiceName}");
    
                var partitions = fabricClient.QueryManager.GetPartitionListAsync(service.ServiceName).Result;
                foreach (var partition in partitions)
                {
                    Console.WriteLine($"Discovered Service Partition:'{partition.PartitionInformation.Kind} {partition.PartitionInformation.Id}");
    
    
                    ServicePartitionKey key;
                    switch (partition.PartitionInformation.Kind)
                    {
                        case ServicePartitionKind.Singleton:
                            key = ServicePartitionKey.Singleton;
                            break;
                        case ServicePartitionKind.Int64Range:
                            var longKey = (Int64RangePartitionInformation)partition.PartitionInformation;
                            key = new ServicePartitionKey(longKey.LowKey);
                            break;
                        case ServicePartitionKind.Named:
                            var namedKey = (NamedPartitionInformation)partition.PartitionInformation;
                            key = new ServicePartitionKey(namedKey.Name);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("partition.PartitionInformation.Kind");
                    }
                    var resolved = resolver.ResolveAsync(service.ServiceName, key, CancellationToken.None).Result;
                    foreach (var endpoint in resolved.Endpoints)
                    {
                        Console.WriteLine($"Discovered Service Endpoint:'{endpoint.Address}");
                    }
                }
            }
        }
    }
    

    You can communicate with the loadbalancer using PowerShell:

    Get-AzureRmLoadBalancer
    

    Finally you'll need to come up with a way to match loadbalancer backend ports to service endpoints yourself.

    0 讨论(0)
提交回复
热议问题