2017-02-21 4 views
0

java sdk를 사용하여 응용 프로그램 게이트웨이의 상태를 확인하는 방법. java sdk를 사용하여 azure cli 명령과 비슷한 작업을 수행해야합니다.Azure에서 응용 프로그램 게이트웨이의 상태를 확인하는 방법

azure 네트워크 응용 프로그램 게이트웨이 백엔드 상태 표시 "$ 1" "$ 2"--json \ | jq -r '.backendAddressPools []. backendHttpSettingsCollection []. servers [] | 선택 (.health == "건강") | 내가 통해 ApplicationGatewayBackendHealthServer 객체를 얻을 수 없다는 것을 구현되지 않는 것 때문에 .address '

답변

2

나는 푸른 자바 SDK의 클래스 ApplicationGatewayBackendHealthServer의 방법 health()를 통해 파이프 라인 명령의 건강 가치를 얻을하려고했으나 실패 루트 클래스의 구현 경로 Azure.

그래서 Azure REST API docs에서 azure network application-gateway backend-health show <resource-group-name> <applicationgateway-name> 명령의 응답을 얻기 위해 관련 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>

여기 내 예제 코드입니다. 2 단계 동적 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); 

: 1 단계 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를 통해 콘텐츠를 할 수 있었다 ,하지만 두 번째 단계는 응답 내용 null을 받았습니다. g OKHttp/Apache HttpClient/HttpURLConnection (자바) &이 무슨 일인지 모르겠지만, Golang/Jquery에서 구현 된 코드조차도 정상적으로 작동합니다. 그것은 자바에서 HTTP 프로토콜 스택의 구현에 의해 발생하는 것으로 보인다.

한편 위의 REST API에 대한 권한 관련 오류 정보가 있으면 https://github.com/JamborYao/ArmManagement을 참조하여 해결하십시오.

희망이 도움이됩니다. 2 단계 문제를 해결할 수 있다면 솔루션을 Google과 공유하십시오. 문제 해결을 위해 계속 노력하겠습니다.

+0

답장을 보내 주신 Peter에게 감사드립니다. 매우 도움이됩니다. –

+0

푸른 색 콘솔에 웹 응용 프로그램을 만든 경우 자바에서이 현상이 발생합니다. 앱이 기본 인 경우 모든 항목이 정상입니다. –

+0

@AurelAvramescu Great !!! 공유해 주셔서 감사합니다. –