2016-07-22 37 views
3
  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. 但我無法找到查找本地服務端口信息或負載均衡服務端口信息的方法。

我該怎麼辦?如何從API管理中動態發現託管在服務結構中的服務?

回答

14

以下是發現在集羣中運行的服務和端點的方法。 (請記住,你需要監視變化太大。)

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和負載均衡器通信:

Get-AzureRmLoadBalancer 

最後你需要想出一個辦法將負載均衡後端端口與您自己的服務端點進行匹配。

+0

我已經離開了一段時間。這太棒了。謝謝。 – Raghu

+0

雖然好,但這並不反映因縮放而導致的變化。 (如果您運行此代碼,請打開資源管理器(端口19080)並選擇將服務擴展到更多或更少的實例,然後再次運行此代碼(但不要重新啓動正在運行的服務),它將返回以前的端點。 – Vaccano