如何通过API管理动态发现服务结构中托管的服务? [英] How can I dynamically discover services hosted in service fabric from API management?

查看:51
本文介绍了如何通过API管理动态发现服务结构中托管的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 可以说我在服务结构群集中托管了服务A和B.他们分别在端口7001和7002上(在群集内部)侦听.
  2. 让我们说我配置了服务结构负载平衡器,以侦听端口8001并将请求转发到服务A的端口7001(在群集内),并侦听端口8002并将请求转发到端口7002(在群集内)服务B.
  3. 让我们说我为服务A和B配置了API管理,并将请求路由到负载均衡器上的适当端口.
  4. 这一切都可以.
  5. 现在,我不是要为每个服务手动映射url路由,而是要动态发现服务结构中托管的服务(来自API管理),并在运行时动态路由请求.
  6. 要做到这一点,我知道我必须写一个策略(最有可能用C#语言)从某个地方查找此信息.
  7. 但是我不确定要查询什么来查找服务矩阵群集中托管的负载平衡端口和服务.
  8. 我想在同一服务结构集群中创建另一个服务C,并使用它(来自API管理)来提供集群的内部信息.
  9. 但是我找不到查找本地服务端口信息或负载均衡服务端口信息的方法.

我该怎么办?

推荐答案

这是发现群集中运行的服务和端点的一种方法. (请记住,您也需要监视更改.)

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

您可以使用PowerShell与负载均衡器进行通信:

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.

这篇关于如何通过API管理动态发现服务结构中托管的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆