如何在Azure中检查应用程序网关的运行状况 [英] How to check the health of application gateway in Azure
问题描述
如何使用java sdk检查应用程序网关的运行状况.我需要使用java sdk执行类似下面的azure cli命令的类似操作:
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网络应用程序-网关后端运行状况显示"$ 1""$ 2" --json \ |jq -r'.backendAddressPools [].backendHttpSettingsCollection [].servers [] |select(.health ==健康")|.address'
推荐答案
我试图通过类 ApplicationGatewayBackendHealthServer
Java SDK,但失败了,因为现在似乎无法通过根类 Azure
的实现路径来获取 ApplicationGatewayBackendHealthServer
对象.
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.
因此,我尝试搜索相关的REST API,以获取命令 azure network application-gateway backend-health show< resource-group-name>的响应.< applicationgateway-name>
在Azure REST API上文档,但也失败了,因为该文档不存在.
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.
我试图研究 AzureCLI
&其他SDK,最后我从 AzureCLI
的详细日志 azure.details.log
中获得了所需的REST API.
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
.
第一步所需的REST API如下所示.
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>
使用标头 Authorization:Bearer< accessToken>
对上述REST API进行 POST
请求,您可以从响应标头 Authorization:Bearer< accessToken>
的 GET
请求>位置.
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,例如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:
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);
使用第二步动态REST API:
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());
注意: 我能够通过 POSTMAN
获取内容,但是使用 OKHttp
获得第二步的响应内容 null
/ Apache HttpClient
/ HttpURLConnection
在Java中.我不知道发生了什么事为什么,即使在Golang/Jquery中实现的代码也可以正常工作.
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.
同时,如果您获得有关上述REST API权限的一些错误信息,请参阅 https://github.com/JamborYao/ArmManagement 解决.
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.
这篇关于如何在Azure中检查应用程序网关的运行状况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!