如何检查Azure中应用程序网关的运行状况
如何使用java sdk检查应用程序网关的运行状况。 我需要执行类似的操作,如下所示使用java sdk的azure cli命令:如何检查Azure中应用程序网关的运行状况
azure网络应用程序 - 网关后端 - 健康显示“$ 1”“$ 2”--json \ | jq -r'.backendAddressPools []。backendHttpSettingsCollection []。servers [] |选择(.health ==“健康”)| 。地址
我试图通过Azure的Java SDK中的类ApplicationGatewayBackendHealthServer
的方法health()
让您的管道命令的保健价值,但失败了,因为它似乎并没有被执行,我无法通过获得ApplicationGatewayBackendHealthServer
对象现在从根类Azure
开始的实现路径。
因此,我试图搜索相关的REST API以获取命令对Azure REST API docs的响应,但也失败了,因为它不存在。
我试图在AzureCLI
&其他SDK源代码的研究,最后我得到了REST API的,你从细节日志AzureCLI
azure.details.log
希望。
您需要的REST API如下所示为第一步。
https://management.azure.com/subscriptions/<subscriptionId>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/applicationGateways/<applicationGateway-name>/backendhealth?api-version=<api-version>
否则用于上述REST API与头Authorization: Bearer <accessToken>
的POST
请求,则可以通过GET
请求与头Authorization: Bearer <accessToken>
得到响应头location
下一动态REST API像的下方。
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);
使用第1步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);
使用第2步动态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
g中的OKHttp
/Apache HttpClient
/HttpURLConnection
。我不知道发生了什么&为什么,即使在Golang/Jquery中实现的代码工作正常。这似乎是由Java中实现HTTP协议栈引起的。
同时,如果您对上述REST API的权限有一些错误信息,请参考https://github.com/JamborYao/ArmManagement来解决。
希望它对你有帮助。如果您可以解决第二步问题,请与我们分享解决方案,我会继续尝试解决它。
感谢彼得的完整答案。这非常有帮助。 –
在java中,如果您已经在Azure控制台中创建了Web应用程序,就会发生这种情况。如果应用程序是Native,那么一切都很好。 –
@AurelAvramescu太棒了!感谢您的分享。 –