0

如何使用 java sdk 检查应用程序网关的健康状况。我需要使用 java sdk 执行类似下面的 azure cli 命令的类似操作:

azure network application-gateway backend-health show "$1" "$2" --json \ | jq -r '.backendAddressPools[].backendHttpSettingsCollection[].servers[] | 选择(.health ==“健康”)| 。地址'

4

1 回答 1

2

我试图通过Azure Java SDKhealth()的类的方法获取管道命令的健康值,但失败了,因为我现在无法通过根类的实现路径ApplicationGatewayBackendHealthServer获取 Object 似乎没有实现。ApplicationGatewayBackendHealthServerAzure

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

我试图研究AzureCLI& 其他 SDK 的源代码,最后我azure.details.logAzureCLI.

满足您需求的 REST API 如下第一步。

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

使用headerPOST对上述 REST API 进行请求,您可以通过request with headerAuthorization: Bearer <accessToken>从响应标头中获取下一个动态 REST API,如下所示。locationGETAuthorization: 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但我在 Java 中使用//null获取了第二步的响应内容。我不知道发生了什么以及为什么,即使在 Golang/Jquery 中实现的代码也可以正常工作。这似乎是由Java中HTTP协议栈的实现引起的。OKHttpApache HttpClientHttpURLConnection

同时,如果您遇到上述 REST API 权限的错误信息,请参考https://github.com/JamborYao/ArmManagement解决。

希望它对你有帮助。如果您可以解决第二步问题,请与我们分享解决方案,我将继续尝试解决。

于 2017-02-23T12:47:56.503 回答