2017-02-21 8 views
0

java sdkを使用してアプリケーションゲートウェイの健全性をチェックする方法。 私は、Java SDKを使用して紺碧のCLIコマンド以下のように同様の操作を実行する必要があります。Azureのアプリケーションゲートウェイの健全性をチェックする方法

紺碧のネットワークアプリケーションゲートウェイバックエンド・健康・ショー「$ 1」「$ 2」--json \ | jq -r '.backendAddressPools []。backendHttpSettingsCollection []。servers [] |選択する(.health == "Healthy")| .address'

答えて

2

私が経由ApplicationGatewayBackendHealthServerオブジェクトを取得することができなかったことを実現することがないようなので、私はアズールのJava SDKのクラスApplicationGatewayBackendHealthServer方法health()を経由して、あなたのパイプラインコマンドの健康値を取得しようとしたが、失敗しましたルートクラスAzureからの実装パスです。

Azure REST API docsでコマンド​​の応答を取得するために関連するREST APIを検索しようとしましたが、既存のREST APIが失敗しました。あなたは詳細ログAzureCLIazure.details.logから欲しいと私はAzureCLI &他のSDKのソースコードを検索するにはしようとした

は、最終的に私は、REST APIを得ました。

必要なREST APIは、次のとおりです。ヘッダAuthorization: Bearer <accessToken>と上記のREST APIのPOST要求を行う

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

、あなたはヘッダ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>

ここに私のサンプルコードです。第二ステップの動的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を使用して

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); 

// 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を介してコンテンツを取得することができた 、私は第2ステップのための応答内容nullを持っていますg OKHttp/Apache HttpClient/HttpURLConnection(Java版)私は&が何故起こったのか分かりません。なぜなら、Golang/Jqueryで実装されたコードさえもうまくいきます。これは、JavaでHTTPプロトコルスタックの実装が原因であると思われます。

上記のREST APIのアクセス許可に関するエラー情報がある場合は、https://github.com/JamborYao/ArmManagementを参照して解決してください。

あなたのお役に立てば幸いです。第2段階の問題を解決できる場合は、解決策を私たちと共有してください。私はそれを解決するために引き続き努力します。

+0

完全な答えをPeterに感謝します。それはとても役に立ちます。 –

+0

Javaでは、これは、あなたがAzureコンソールにWebアプリケーションを作成した場合に起こります。アプリがネイティブの場合はすべて正常です。 –

+0

@AurelAvramescu素晴らしい!あなたの共有に感謝します。 –

関連する問題