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

时间:2017-02-21 12:40:24

标签: java azure azure-java-sdk azure-application-gateway

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

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

1 个答案:

答案 0 :(得分:2)

我尝试通过Azure Java SDK的类ApplicationGatewayBackendHealthServer的方法health()获取管道命令的运行状况值,但是因为似乎没有实现我无法获得ApplicationGatewayBackendHealthServer现在通过根类Azure的实现路径对象。

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

我尝试研究AzureCLI&amp;的源代码。其他SDK,最后我从azure.details.log的详细日志AzureCLI中获得了所需的REST API。

满足您需求的REST API如下所示。

  

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

使用标头POST执行上述REST API的Authorization: Bearer <accessToken>请求,您可以通过location请求从响应标头GET获取下一个动态REST API标题为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获取内容,但我使用null / OKHttp / {{获得了第二步的回复内容Apache HttpClient Java中的1}}我不知道发生了什么事。为什么,即使在Golang / Jquery中实现的代码也能正常工作。它似乎是由Java中HTTP协议栈的实现引起的。

同时,如果您收到有关上述REST API权限的错误信息,请参阅https://github.com/JamborYao/ArmManagement以解决此问题。

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