【问题标题】:Starting and stopping instances on google compute engine在谷歌计算引擎上启动和停止实例
【发布时间】:2015-08-06 18:59:04
【问题描述】:

我想在谷歌计算引擎上启动/恢复和停止/暂停实例,但它给出“java.lang.UnsupportedOperationException”。有没有其他方法 执行这些操作?

public class Example {

     public static void main(String[] args) 
     {
      String provider = "google-compute-engine";
      String identity = "****@developer.gserviceaccount.com";
      String credential = "path to private key";
      String groupName = "newgroup";
      credential = getCredentialFromJsonKeyFile(credential);
      Iterable<Module> modules = ImmutableSet.<Module> of(
              new SshjSshClientModule(),
              new SLF4JLoggingModule(),
              new EnterpriseConfigurationModule());
      ContextBuilder builder = ContextBuilder.newBuilder(provider)
              .credentials(identity, credential)
              .modules(modules);
      ComputeService compute=builder.buildView(ComputeServiceContext.class).getComputeService();

      compute.suspendNode("Instance id");
      //compute.suspendNodesMatching(Predicates.<NodeMetadata> and(inGroup(groupName)));
      System.out.println("suspended");
      compute.getContext().close();     
}

   private static String getCredentialFromJsonKeyFile(String filename) {
      try {
         String fileContents = Files.toString(new File(filename), UTF_8);
         Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(fileContents);
         String credential = credentialSupplier.get().credential;
         return credential;
      } catch (IOException e) {
         System.err.println("Exception reading private key from '%s': " + filename);
         e.printStackTrace();
         System.exit(1);
         return null;
      }
   }
}

输出:

暂停节点(节点id)

线程“main”java.lang.UnsupportedOperationException 中的异常:GCE 不支持挂起

在 org.jclouds.googlecomputeengine.compute.GoogleComputeEngineServiceAdapter.suspendNode(GoogleComputeEngineServiceAdapter.java:251)

在 org.jclouds.compute.strategy.impl.AdaptingComputeServiceStrategies.suspendNode(AdaptingComputeServiceStrategies.java:171)

在 org.jclouds.compute.internal.BaseComputeService.suspendNode(BaseComputeService.java:503)

在 org.jclouds.examples.compute.basics.Example.main(Example.java:79)

【问题讨论】:

  • 我的回答对你的问题有用吗?

标签: google-compute-engine google-cloud-platform jclouds


【解决方案1】:

在可移植的 jclouds ComputeService 中不直接支持,但是可以从 ComputeServiceContext 中获取 GoogleComputeEngineApi 和 InstanceApi,并使用其中的启动/停止方法。

仅供参考,有一个正在进行的补丁来添加对 ComputeService 中启动/停止操作的支持:https://github.com/jclouds/jclouds-labs-google/pull/141

【讨论】:

    【解决方案2】:

    您可以通过 API 停止实例。

    POST https://www.googleapis.com/compute/v1/projects/<project>/zones/<zone>/instances/<instance>/stop
    

    地点:

    • project 在 URL 中是你的项目 ID。
    • URL 中的 zone 是请求的区域名称。
    • URL 中的 instance 是要停止的实例的名称。

    这是docs

    【讨论】:

    • 有什么方法可以使用 jclouds 或 google api java 客户端吗?
    • 如果您转到developers.google.com/api-client-library/java/apis/compute/v1,您将获得适用于 Java 的 Compute Engine API 客户端库,您可以在其中浏览“JavaDoc 参考”。您应该看到“setStatus”作为一个选项。
    • 这是完成这项工作的另一种方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多