【Azure微服務 Service Fabric 】Service Fabric中應用開啟外部訪問端口及微服務之間通過反向代理端口訪問問題

問題描述

1) 當成功的在Service Fabric集群中部署了應用後,如何來訪問呢?如果是一個Web服務,它的URL又是什麼呢?

2) 當Service Fabric集群中,服務之間如需要相互訪問?如何來設置反向代理的端口呢?如果在創建Service Fabric時沒有設定方向代理端口,如何來添加呢?

問題解釋

問題一:默認情況下,訪問應用的URL通過Service Fabric的集群endpoint + 在發佈應用時候,所設定的端口號。 如//sfdnstest01.chinanorth2.cloudapp.chinacloudapi.cn:8080

Service Fabric集群的Endpoint可以從Azure門戶中獲取:

應用的端口號:則由發佈文件ServiceManifest.xml中設定。如果已經發佈後,可以在Service Fabric Explorer中查看Application的Minifest文件中發現

  <Resources>
    <Endpoints>
      <!-- This endpoint is used by the communication listener to obtain the port on which to 
           listen. Please note that if your service is partitioned, this port is shared with 
           replicas of different partitions that are placed in your code. -->
      <Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8080" />
    </Endpoints>
  </Resources>

還有非常重要的一步:在SF的Load Balance中開啟訪問此端口的規則。否則,通過瀏覽器訪問時,就會得到「This site can』t be reached」的錯誤。在LB中配置規則非常簡單。

進入Load Balance的Azure 門戶頁面,導航到「Load balancing rules」 一欄,添加對應的端口規則即可,如下圖:

以上,及可以設定完成SF集群中應用的外部訪問端口。

 Azure Service Fabric的參考文檔部分://docs.azure.cn/zh-cn/service-fabric/service-fabric-tutorial-create-dotnet-app#configure-the-listening-port

創建 VotingWeb 前端服務後,Visual Studio 會隨機選擇服務偵聽的端口。 VotingWeb 服務充當此應用程序的前端並接受外部流量,因此讓我們將此服務綁定到已知的固定端口。 服務清單聲明服務終結點。

在解決方案資源管理器中,打開「VotingWeb/PackageRoot/ServiceManifest.xml」。 在「Resources」部分中查找「Endpoint」元素,並將「Port」值更改為 8080。 若要在本地部署和運行應用程序,應用程序偵聽端口必須為打開狀態且在你的計算機上可用。

<Resources>
    <Endpoints>
      <!-- This endpoint is used by the communication listener to obtain the port on which to 
           listen. Please note that if your service is partitioned, this port is shared with 
           replicas of different partitions that are placed in your code. -->
      <Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8080" />
    </Endpoints>
  </Resources>

此外,更新投票項目中的應用程序 URL 屬性值,使 Web 瀏覽器在調試應用程序時打開到正確的端口。 在解決方案資源管理器中,選擇「Voting」項目並將「應用程序 URL」屬性更新為 8080 。

 

 

問題二:如何查看反向代理端口是否已經配置,使用Service Fabric Explorer,查看Cluster節點中的Minifest。如截圖中的紅色框中是否有HttpApplicationGatewayEndpoint節點。如圖中配置的反向代理端口為19081

如果查看到當前的Service Fabric集群中沒有配置反向代理端口,那如何來開啟呢?

 

Service Fabric反向代理端口開啟步驟

1) 找到部署Service Fabric的模板。如沒有,則可以到Azure 門戶中改Service Fabric的資源組中,查看部署歷史記錄,選擇正確的部署模板(此模板中包含SF集群所有配置和參數)

  • 導航路徑 (Azure Home -> Service Fabric Clusters -> Resource Group -> Deployment -> Template)

 

2) 在模板文件中查找nodeTypes節點並在其內添加 “reverseProxyEndpointPort”:”19081″ (可以根據自己需要調整端口號)

 

 3) 重新部署,可以使用第一步中下載模板是自帶的Powershell命令,也可以使用以下命令

az login
az account set --subscription "your subscription id"

az deployment group create --name deployment  --resource-group "your resource group " --template-file .\exsitsf.json

 

4)部署完成後,通過Service Fabric Explorer工具查看結果。

Service Fabric中反向代理端口設置參考文檔://docs.azure.cn/zh-cn/service-fabric/service-fabric-tutorial-create-dotnet-app#connect-the-services

下一步是連接這兩個服務,使前端 Web 應用程序獲取並設置來自後端服務的投票信息。

在如何與 Reliable Services 通信方面,Service Fabric 是十分靈活的。 在單個應用程序中,可能會有通過 TCP 訪問的服務。 其他服務也許可以通過 HTTP REST API 訪問,以及可通過 Web 套接字訪問。 有關可用選項和相關權衡取捨的背景信息,請參閱與服務通信

本教程使用 ASP.NET Core Web API 和 Service Fabric 反向代理,以便 VotingWeb 前端 Web 服務能夠與後端 VotingData 服務通信。 反向代理默認配置為使用端口 19081,應適用於本教程。 反向代理端口是在用於設置群集的 Azure 資源管理器模板中設置的。 若要確定使用哪個端口,請在 Microsoft.ServiceFabric/clusters 資源中搜索群集模板:

"nodeTypes": [
          {
            ...
            "httpGatewayEndpointPort": "[variables('nt0fabricHttpGatewayPort')]",
            "isPrimary": true,
            "vmInstanceCount": "[parameters('nt0InstanceCount')]",
            "reverseProxyEndpointPort": "[parameters('SFReverseProxyPort')]"
          }
        ],

若要查找在本地開發群集中使用的反向代理端口,請查看本地 Service Fabric 群集清單中的 HttpApplicationGatewayEndpoint 元素:

  1. 打開一個瀏覽器窗口,並導航到 //localhost:19080 以打開 Service Fabric Explorer 工具。
  2. 選擇「群集」->「清單」。
  3. 記下 HttpApplicationGatewayEndpoint 元素端口。 默認情況下,此端口應是 19081。 如果不是 19081,則需要更改以下 VotesController.cs 代碼的 GetProxyAddress 方法中的端口。

 

 

參考資料

使用 ASP.NET Core Web API 前端服務和有狀態後端服務創建並部署應用程序: //docs.azure.cn/zh-cn/service-fabric/service-fabric-tutorial-create-dotnet-app#connect-the-services