首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >google计算引擎上的启动和停止实例

google计算引擎上的启动和停止实例
EN

Stack Overflow用户
提问于 2015-05-25 10:11:27
回答 2查看 628关注 0票数 3

我想在google上启动/恢复和停止/挂起实例,但是它为"java.lang.UnsupportedOperationException".Is提供了执行这些操作的其他方法吗?

代码语言:javascript
复制
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)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-27 13:54:08

在可移植的jclouds中不直接支持它,但是可以从ComputeServiceContext中获得GoogleComputeEngineApi和InstanceApi,并在其中使用start/stop方法。

FYI,有一个正在进行的修补程序可以在ComputeService:https://github.com/jclouds/jclouds-labs-google/pull/141中添加对开始/停止操作的支持。

票数 1
EN

Stack Overflow用户

发布于 2015-05-25 10:29:10

您可以从API中停止一个实例。

代码语言:javascript
复制
POST https://www.googleapis.com/compute/v1/projects/<project>/zones/<zone>/instances/<instance>/stop

其中:

  • URL中的项目是您的项目id。
  • 区域是请求的区域名称。
  • URL中的实例是要停止的实例的名称。

这是文档

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30435909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档