How to check the health of application gateway in Azure

我的未来我决定 提交于 2019-12-23 04:47:12

问题


How to check the health of application gateway using java sdk. I need to perform a similar operation like below azure cli command using java sdk:

azure network application-gateway backend-health show "$1" "$2" --json \ | jq -r '.backendAddressPools[].backendHttpSettingsCollection[].servers[] | select(.health == "Healthy") | .address'


回答1:


I tried to get the health value of your pipeline command via the method health() of the class ApplicationGatewayBackendHealthServer of Azure Java SDK, but failed because it seems not to be implemented that I could not get the ApplicationGatewayBackendHealthServer Object via a implementing path from the root class Azure now.

So I tried to search a related REST API to get the response of command azure network application-gateway backend-health show <resource-group-name> <applicationgateway-name> on Azure REST API docs, but also failed that there is not existing.

I tried to research on source code of AzureCLI & other SDK, finally I got the REST APIs as you want from the detail logs azure.details.log of AzureCLI.

The REST API for your needs is as below for 1st step.

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

Doing the POST request for the above REST API with header Authorization: Bearer <accessToken>, you can get the next dynamic REST API from the response header location like below via GET request with header Authorization: Bearer <accessToken>.

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

Here is my sample code.

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

Using 1st step 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);

Using 2nd step dynamic 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());

Notice: I was able to get the content via POSTMAN, but I got the response content null for the 2nd step using OKHttp/Apache HttpClient/HttpURLConnection in Java. I don't know what happened & why, even the code implemented in Golang/Jquery works fine. It seems to be caused by the implementation of HTTP protocol stack in Java.

Meanwhile, if you got some error information about permission for the above REST APIs, please refer to https://github.com/JamborYao/ArmManagement to solve it.

Hope it helps for you. If you can resolve the 2nd step issue, please share the solution with us, and I will continue to try for solving it.



来源:https://stackoverflow.com/questions/42367621/how-to-check-the-health-of-application-gateway-in-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!