【问题标题】:Suspend Google Cloud Platform instance using Java使用 Java 暂停 Google Cloud Platform 实例
【发布时间】:2021-01-15 20:19:49
【问题描述】:

我有一个 Java Web 应用程序,我在其中创建 GCP VM 实例并对其执行操作。

我使用 Java Compute Engine API 来执行创建、启动、停止等任务。

我想表演:

Compute compute = getComputeObj();
Suspend suspend = compute.instances().suspend(getProjectId(), getOrder().getAvailabilityZone(), getOrder().getMachineName());
Operation response = suspend .execute();

但是,我无法使用此 API“暂停”机器,因为“暂停”处于 beta 版本(它不是 Compute Engine v1 API 的一部分,因此它不是 Java 包装器的一部分)。

在我看来,我有两个选择来解决这个问题,但我都无法完成:

  1. 创建一个继承Compute类的Java类“ComputeBeta”,并实现一个Suspend类来执行操作。这是有问题的,因为原始 Compute 类中的 URL 字段是 final 字段,因此我无法将 URL 从“v1”更改为“beta”url。

  2. 执行到 GCP beta API 的“常规”httpconnection 以暂停机器。这里的问题是我无法找到 API 身份验证的正确语法,并且得到了 401 响应。到目前为止我尝试过的:

String serviceAccountKey = getServiceAccountKey();

String baseUrl = "https://compute.googleapis.com";

String endpoint = "/compute/beta/projects/" + getOrder().getProject() 
                + "/zones/" + getOrder().getAvailabilityZone() + "/instances/" 
                + getOrder().getMachineName() + "/suspend";

URL serverUrl = new URL(baseUrl + endpoint);

httpConnection = (HttpsURLConnection)serverUrl.openConnection();
httpConnection.setRequestMethod(HttpMethods.POST);
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);


httpConnection.setFixedLengthStreamingMode(length);

httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpConnection.setRequestProperty("Authorization", "Bearer " + serviceAccountKey);
httpConnection.connect();

int responseCode = httpConnection.getResponseCode();
String responseMessage = httpConnection.getResponseMessage();

任何有关如何前进的帮助将不胜感激!

【问题讨论】:

    标签: java google-cloud-platform virtual-machine google-compute-engine google-compute-api


    【解决方案1】:

    您不必像这样使用您的服务帐户密钥。此密钥允许您创建凭据,然后使用它生成令牌。

     GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    // You can use explicitly your service account key file like this
    // GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));
    
     String token = credentials.refreshAccessToken().getTokenValue();
    ...
     httpConnection.setRequestProperty("Authorization", "Bearer " + token);
    
    

    您也可以使用 Google Transport 工厂来避免手动添加授权标头

     GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    // You can use explicitly your service account key file like this
    // GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));
    
    HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(credentials));
    // Here a GET request, but you can build a POST request also
    HttpRequest request = factory.buildGetRequest(new GenericUrl("YOUR_URL"));
    HttpResponse httpResponse = request.execute();
    System.out.println(CharStreams.toString(new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8)));
    

    如果你有依赖问题(通常不是因为你使用了包含它的计算引擎库),你可以添加这个依赖

            <dependency>
                <groupId>com.google.auth</groupId>
                <artifactId>google-auth-library-oauth2-http</artifactId>
                <version>0.21.1</version>
            </dependency>
    

    【讨论】:

    • 非常感谢,我现在就试试。就一个问题,这个依赖应该加在哪里?
    • 如果您使用 Maven,请在 pom.xml 文件的 dependencies 部分中。如果你使用Gradle,你要稍微改造一下,它是一个内联模型com.google.auth:google-auth-library-oauth2-http:0.21.1
    猜你喜欢
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多