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

  • Suppose I have service A and B hosted in a work fabric cluster. They listen (inside the cluster) on ports 7001 and 7002, respectively.
  • Suppose I configure a load balancer for listening services on port 8001 and redirect the request to port 7001 (inside the cluster) for service A and listen on port 8002 and redirect the request to port 7002 (inside the cluster) for service B.
  • Suppose I configure API control for services A and B and forward requests to the appropriate ports in the load balancer.
  • It all works.
  • Now, instead of manually displaying the URL for each service, I would like to dynamically discover the services hosted in the service structure (from the API control) and route the request dynamically at runtime.
  • To do this, I know that I need to write a policy (most likely in C #) to find this information from somewhere.
  • But I don’t know exactly what to request to find balanced ports and services hosted in a cluster of working tissue.
  • I thought about creating another C service in the same cluster of service clusters and use it (from the API control) to provide internal cluster information.
  • But I could not find a way to find information about the port of the local service port or information about balancing the service port.

How should I do it?

+3
1

, . ( , .)

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

loadbalancer PowerShell:

Get-AzureRmLoadBalancer

, backbalancer backend .

+15

Source: https://habr.com/ru/post/1666321/


All Articles