如何找到Service Fabric应用服务与节点的关联?

时间:2018-05-22 21:55:00

标签: c# azure-service-fabric

使用System.Fabric.FabricClient.QueryClient方法从远程服务结构集群中提取信息,如何将应用程序服务与托管这些服务的节点相关联?

我利用ListEndPoints答案的答案来获取有关我的服务和分区的更多详细信息,但我没有看到将服务映射到节点所需的属性。

var fabricClient = new FabricClient(credentials, connectionString);
var nodes = fabricClient.QueryManager.GetNodeListAsync().Result;
var apps = fabricClient.QueryManager.GetApplicationListAsync().Result;
var services = fabricClient.QueryManager.GetServiceListAsync(app.ApplicationName).Result;
var partitions = fabricClient.QueryManager.GetPartitionListAsync(service.ServiceName).Result;

e.g。

  • APPA
    • ServiceA_A
      • NodeFe0
      • NodeFe1
    • ServiceA_B
      • NodeBe0
      • NodeBe1
      • NodeBe2
  • APPB
    • ServiceB_A
      • NodeFe0
      • NodeFe1

1 个答案:

答案 0 :(得分:1)

此代码可帮助您概览在哪些节点上运行的服务分区

var fabricClient = new FabricClient();
var nodes = await fabricClient.QueryManager.GetNodeListAsync("");
var apps = fabricClient.QueryManager.GetApplicationListAsync().Result;
foreach (var app in apps)
{
    Console.WriteLine($"Discovered application:'{app.ApplicationName}");
    var deployedPartitions = new Dictionary<Guid, List<string>>();
    foreach (var node in nodes)
    {
        //get deployed partitions per node
        var deployed = await fabricClient.QueryManager.GetDeployedReplicaListAsync(node.NodeName, app.ApplicationName);

        foreach (var dep in deployed)
        {
             List<string> list;
             if (!deployedPartitions.TryGetValue(dep.Partitionid, out list))
             {
                  list = new List<string>();
                  deployedPartitions.Add(dep.Partitionid, list);
             }
             list.Add(node.NodeName);
         }
     }

     var services = await fabricClient.QueryManager.GetServiceListAsync(app.ApplicationName);
     foreach (var service in services)
     {
         Console.WriteLine($"Discovered Service:'{service.ServiceName}");
         var partitions = await fabricClient.QueryManager.GetPartitionListAsync(service.ServiceName);
         foreach (var partition in partitions)
         {
              var partitionId = partition.PartitionInformation.Id;
              if (deployedPartitions.TryGetValue(partitionId, out var nodeNames))
              {
                  Console.WriteLine($"Discovered {service.ServiceKind} Service:'{service.ServiceName} PartitionId: '{partitionId}' running on nodes {string.Join(", ", nodeNames)}");
              }
         }
    }
 }