2017-02-21 53 views
0

如何使用java sdk檢查應用程序網關的運行狀況。 我需要執行類似的操作,如下所示使用java sdk的azure cli命令:如何檢查Azure中應用程序網關的運行狀況

azure網絡應用程序 - 網關後端 - 健康顯示「$ 1」「$ 2」--json \ | jq -r'.backendAddressPools []。backendHttpSettingsCollection []。servers [] |選擇(.health ==「健康」)| 。地址

回答

2

我試圖通過Azure的Java SDK中的類ApplicationGatewayBackendHealthServer的方法health()讓您的管道命令的保健價值,但失敗了,因爲它似乎並沒有被執行,我無法通過獲得ApplicationGatewayBackendHealthServer對象現在從根類Azure開始的實現路徑。

因此,我試圖搜索相關的REST API以獲取命令​​對Azure REST API docs的響應,但也失敗了,因爲它不存在。

我試圖在AzureCLI &其他SDK源代碼的研究,最後我得到了REST API的,你從細節日誌AzureCLIazure.details.log希望。

您需要的REST API如下所示爲第一步。

https://management.azure.com/subscriptions/<subscriptionId>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/applicationGateways/<applicationGateway-name>/backendhealth?api-version=<api-version>

否則用於上述REST API與頭Authorization: Bearer <accessToken>POST請求,則可以通過GET請求與頭Authorization: Bearer <accessToken>得到響應頭location下一動態REST API像的下方。

https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.Network/locations/<region>/operationResults/<objectId, such as f7bfd1fd-e3ea-42f7-9711-44f3229ff877>?api-version=<api-version>

這裏是我的示例代碼。

String AUTHORITY = "https://login.windows.net/<tenantId>"; 
String clientId = "<client-id on management portal(old), or application-id on Azure new portal>"; 
String clientSecret = "<client-secret-key>"; 
String subscriptionId = "<subscriptionId>"; 
String resourceGroupName = "<resource-group-name>"; 
String appGatewayName = "<applicationgateway-name>"; 
String apiVersion = "2016-09-01"; 
// Getting access token 
AuthenticationContext context = null; 
AuthenticationResult result = null; 
ExecutorService service = null; 
service = Executors.newFixedThreadPool(1); 
context = new AuthenticationContext(AUTHORITY, false, service); 
ClientCredential credential = new ClientCredential(clientId, clientSecret); 
Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", credential, null); 
result = future.get(); 
String accessToken = result.getAccessToken(); 
System.out.println(accessToken); 

使用第1步REST API:

// Get the response header `location` from 1st step REST API 
OkHttpClient client = new OkHttpClient(); 
String url = String.format("https://management.azure.com/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/applicationGateways/%s/backendhealth?api-version=%s", subscriptionId, resourceGroupName, appGatewayName, apiVersion); 
MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 
RequestBody body = RequestBody.create(JSON, ""); 
Request request = new Request.Builder().url(url).header("Authorization", "Bearer "+accessToken).post(body).build(); 
Response response = client.newCall(request).execute(); 
String location = response.header("Location"); 
System.out.println(location); 

使用第2步動態REST API:

// Get the response content as the same as the azure-cli command `azure network applicationgateway backend-health show` 
Request request2 = new Request.Builder().url(location).header("Authorization", "Bearer " + accessToken).build(); 
// Notice for the below code, see under the code 
Response response2 = client.newCall(request2).execute(); 
System.out.println(response2.body().string()); 

注意: 我能夠通過POSTMAN獲取內容,但我第二步使用了響應內容null g中的OKHttp/Apache HttpClient/HttpURLConnection。我不知道發生了什麼&爲什麼,即使在Golang/Jquery中實現的代碼工作正常。這似乎是由Java中實現HTTP協議棧引起的。

同時,如果您對上述REST API的權限有一些錯誤信息,請參考https://github.com/JamborYao/ArmManagement來解決。

希望它對你有幫助。如果您可以解決第二步問題,請與我們分享解決方案,我會繼續嘗試解決它。

+0

感謝彼得的完整答案。這非常有幫助。 –

+0

在java中,如果您已經在Azure控制檯中創建了Web應用程序,就會發生這種情況。如果應用程序是Native,那麼一切都很好。 –

+0

@AurelAvramescu太棒了!感謝您的分享。 –

相關問題