java HttpURLConnection 을 이용한 GCP에서 VM compute instance metadata 가져오기
URL은 아래 문서 참조 https://cloud.google.com/compute/docs/storing-retrieving-metadata
import java.net.*;
import java.io.*;
import javax.servlet.http.*;
public class GetMetadata {
public static String getHostName() throws Exception {
URL url = new URL("http://metadata.google.internal/computeMetadata/v1/instance/hostname");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Metadata-Flavor", "Google");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = rd.readLine();
rd.close();
return line;
}
public static String getInstanceName() throws Exception {
URL url = new URL("http://metadata.google.internal/computeMetadata/v1/instance/name");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Metadata-Flavor", "Google");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = rd.readLine();
rd.close();
return line;
}
'Cloud & Virtualization' 카테고리의 다른 글
Docker Container의 Logging 구조 요약 (1) | 2018.07.12 |
---|---|
구글 클라우드에서 Jenkins로 Docker Image Build/Push하기 (0) | 2018.07.10 |
Docker를 통한 WordPress 초간단 배포 (0) | 2018.05.28 |
VMWare ESXi 가상머신에 USB Controller Passthrough 설정 (0) | 2018.05.28 |
GCP Dataflow : Raw data 가공 후 Big Query 입력 Apache Beam(Python) 샘플 코드 (0) | 2018.05.28 |