如何在 Azure 中检查应用程序网关的健康状况

How to check the health of application gateway in Azure

如何使用java sdk 检查应用程序网关的健康状况。 我需要使用 java sdk:

执行类似下面的 azure cli 命令的类似操作

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

我试图通过 Azure Java SDK class ApplicationGatewayBackendHealthServer 的方法 health() 获取您的管道命令的健康值,但失败了,因为它似乎无法实现,我现在无法通过根 class Azure 的实现路径获得 ApplicationGatewayBackendHealthServer Object。

所以我尝试搜索相关的 REST API 以获取 Azure REST API docs 上命令 azure network application-gateway backend-health show <resource-group-name> <applicationgateway-name> 的响应,但也失败了,因为没有现有。

我试图研究 AzureCLI 和其他 SDK 的源代码,最后我从 [=18= 的详细日志 azure.details.log 中得到了你想要的 REST APIs ].

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

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

使用headerAuthorization: Bearer <accessToken>对上述RESTAPI进行POST请求,您可以从响应中获取下一个动态RESTAPIheader location 像下面通过 GET 请求 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>

这是我的示例代码。

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

使用第一步 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);

使用第二步动态 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 获取内容,但我使用 OKHttp/[=31= 获取了第二步的响应内容 null ]/HttpURLConnection 在 Java。我不知道发生了什么以及为什么,即使 Golang/Jquery 中实现的代码也能正常工作。好像是Java.

中HTTP协议栈的实现导致的

同时,如果您在上述RESTAPI中得到一些关于权限的错误信息,请参考https://github.com/JamborYao/ArmManagement解决。

希望对您有所帮助。如果您能解决第2步的问题,请与我们分享解决方案,我会继续努力解决。