Google compute engine 启动和停止google计算引擎上的实例
我想在google compute engine上启动/恢复和停止/挂起实例,但它给出了“java.lang.UnsupportedOperationException”。还有其他方法吗 执行这些操作Google compute engine 启动和停止google计算引擎上的实例,google-compute-engine,google-cloud-platform,jclouds,Google Compute Engine,Google Cloud Platform,Jclouds,我想在google compute engine上启动/恢复和停止/挂起实例,但它给出了“java.lang.UnsupportedOperationException”。还有其他方法吗 执行这些操作 public class Example { public static void main(String[] args) { String provider = "google-compute-engine"; String identity =
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;
}
}
}
公共类示例{
公共静态void main(字符串[]args)
{
字符串提供者=“谷歌计算引擎”;
字符串标识=“***@developer.gserviceaccount.com”;
String credential=“私钥路径”;
字符串groupName=“newgroup”;
凭证=getCredentialFromJsonKeyFile(凭证);
Iterable modules=的不可变集(
新的SSHJSHClientModule(),
新的SLF4JLoggingModule(),
新的EnterpriseConfigurationModule());
ContextBuilder=ContextBuilder.newBuilder(提供程序)
.凭证(身份、凭证)
.模块(模块);
ComputeService compute=builder.buildView(ComputeServiceContext.class).getComputeService();
suspendNode(“实例id”);
//compute.suspendNodeMatch(谓词和(inGroup(groupName));
系统输出打印项次(“暂停”);
compute.getContext().close();
}
私有静态字符串getCredentialFromJsonKeyFile(字符串文件名){
试一试{
字符串fileContents=Files.toString(新文件(文件名),UTF_8);
供应商凭证Supplier=新的GoogleCredentialsFromJson(文件内容);
字符串credential=credentialSupplier.get().credential;
返回凭证;
}捕获(IOE异常){
System.err.println(“从“%s”读取私钥的异常:”+文件名);
e、 printStackTrace();
系统出口(1);
返回null;
}
}
}
输出:
挂起节点(节点id)
线程“main”java.lang.UnsupportedOperationException中出现异常:GCE不支持挂起
位于org.jclouds.googlecomputengine.compute.googlecomputengineserviceadapter.suspendNode(googlecomputengineserviceadapter.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)
您可以从API停止实例
POST https://www.googleapis.com/compute/v1/projects/<project>/zones/<zone>/instances/<instance>/stop
POSThttps://www.googleapis.com/compute/v1/projects//zones//instances//stop
其中:
- URL中的project是您的项目id
- URL中的区域是请求区域的名称
- URL中的实例是要停止的实例的名称
以下是便携式jclouds ComputeService中不直接支持的,但从ComputeServiceContext中,您可以获得GoogleComputengineAPI和InstanceApi,并使用其中的启动/停止方法
仅供参考,有一个正在进行的补丁程序可以添加对ComputeService中的启动/停止操作的支持:有没有办法使用jclouds或google api java client来实现这一点?如果您访问,您将获得Compute Engine api client Library for java,您可以在其中浏览“JavaDoc参考”。您应该将“setStatus”视为一种选项。这是一种替代方法。我的答案对您的问题有效吗?。