【问题标题】:Official Google Cloud Java SDK with examples带有示例的官方 Google Cloud Java SDK
【发布时间】:2021-03-23 06:26:25
【问题描述】:

另外,是否有一个示例 maven 项目说明使用 Java SDK 创建虚拟机/实例? 以及一个示例,说明所需的所有依赖项和示例代码

寻找与 AWS Java SDK 非常相似的方法来创建 VM,如下面的链接 https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/ec2/src/main/java/com/example/ec2/CreateInstance.java

找到了这个,但不确定这是否是官方支持的 SDK。 https://github.com/googleapis/google-cloud-java 如果有创建 VM 的示例,请分享。

【问题讨论】:

    标签: google-compute-engine


    【解决方案1】:

    创建计算引擎实例的示例代码可以在here找到:

    /*
     * Copyright 2016 Google LLC
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *       http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.google.cloud.examples.compute.snippets;
    
    import com.google.cloud.ServiceOptions;
    import com.google.cloud.compute.v1.AttachedDisk;
    import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
    import com.google.cloud.compute.v1.Instance;
    import com.google.cloud.compute.v1.InstanceClient;
    import com.google.cloud.compute.v1.NetworkInterface;
    import com.google.cloud.compute.v1.Operation;
    import com.google.cloud.compute.v1.ProjectZoneMachineTypeName;
    import com.google.cloud.compute.v1.ProjectZoneName;
    import java.io.IOException;
    
    /** A snippet for Google Cloud Compute Engine showing how to create a virtual machine instance. */
    public class CreateInstance {
      private static final String ZONE = "us-central1-a";
      private static final String DEFAULT_IMAGE =
          "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20150710";
      private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId();
    
      public static void main(String... args) throws IOException {
        try (InstanceClient instanceClient = InstanceClient.create()) {
          ProjectZoneName zone = ProjectZoneName.of(DEFAULT_PROJECT, ZONE);
          String machineType =
              ProjectZoneMachineTypeName.of("n1-standard-1", DEFAULT_PROJECT, ZONE).toString();
          AttachedDisk disk =
              AttachedDisk.newBuilder()
                  .setBoot(true)
                  .setAutoDelete(true)
                  .setType("PERSISTENT")
                  .setInitializeParams(
                      AttachedDiskInitializeParams.newBuilder().setSourceImage(DEFAULT_IMAGE).build())
                  .build();
          NetworkInterface networkInterface = NetworkInterface.newBuilder().setName("default").build();
          Instance instanceResource =
              Instance.newBuilder()
                  .setName("instance-name")
                  .setMachineType(machineType)
                  .addDisks(disk)
                  .addNetworkInterfaces(networkInterface)
                  .build();
          Operation response = instanceClient.insertInstance(zone.toString(), instanceResource);
          if (response.getError() == null) {
            System.out.println("Instance was created successfully");
          } else {
            // inspect operation.getErrors()
            throw new RuntimeException("Instance creation failed");
          }
        }
      }
    }
    
    

    如果您想使用 Maven,这里有一个很棒的教程 here 和一个快速入门指南 here

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 2018-12-19
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多