【问题标题】:How to check the health of application gateway in Azure如何在 Azure 中检查应用程序网关的运行状况
【发布时间】:2017-02-21 12:40:24
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    我尝试通过 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和其他SDK的源代码,最后我从AzureCLI的详细日志azure.details.log中得到了你想要的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>

    对上述带有标头Authorization: Bearer <accessToken> 的REST API 执行POST 请求,您可以通过带有标头Authorization: Bearer <accessToken>GET 请求从响应标头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);
    

    使用第一步 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 中使用OKHttp/Apache HttpClient/HttpURLConnection 获得了第二步的响应内容null。我不知道发生了什么以及为什么,即使在 Golang/Jquery 中实现的代码也可以正常工作。好像是Java中HTTP协议栈的实现造成的。

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

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

    【讨论】:

    • 感谢彼得的完整回答。这很有帮助。
    • 在 java 中,如果您在 azure 控制台中创建了 Web 应用程序,就会发生这种情况。如果应用是原生的,那么一切都很好。
    • 另一种想法,我注意到了:有时我从这个端点management.azure.com/subscriptions/<subscriptionId>/… 得到 202,例如 f7bfd1fd-e3ea-42f7-9711-44f3229ff877>?api-version= 因此,我必须使用 Retry-After 值重试 2-3 次,这根本不准确,所有时间都是 10 秒,但有时我需要执行 3 次重试,然后我会得到后端的状态。
    猜你喜欢
    • 2021-06-06
    • 2019-12-02
    • 2019-09-22
    • 2013-11-13
    • 1970-01-01
    • 2023-03-31
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多