gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:00:59 25 4
gpt4 key购买 nike

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

azure 网络应用程序网关后端运行状况显示“$1”“$2”--json\| jq -r '.backendAddressPools[].backendHttpSettingsCollection[].servers[] | jq -r '.backendAddressPools[].backendHttpSettingsCollection[].servers[] |选择(.health == "健康") | .地址'

最佳答案

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

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

我尝试研究AzureCLI的源代码和其他SDK,最后我从详细日志中得到了你想要的REST API azure.details.logAzureCLI .

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

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

执行POST对上述 REST API 的请求, header 为 Authorization: Bearer <accessToken> ,可以从响应头location获取下一个动态REST API就像下面通过 GET带有 header 的请求 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/HttpURLConnection在 java 。我不知道发生了什么以及为什么,即使在 Golang/Jquery 中实现的代码也可以正常工作。看来是Java中HTTP协议(protocol)栈的实现造成的。

同时,如果您收到上述REST API权限相关的错误信息,请引用https://github.com/JamborYao/ArmManagement来解决它。

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

关于java - 如何检查 Azure 中应用程序网关的运行状况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42367621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com