OpenStack-API开发
2021-04-24 03:27
介绍两种OpenStack-API(Java版)——jcoulds && openstack4j
Jclouds
1.介绍
jclouds -该API提供云计算环境的可移植抽象层以及云规范特性,支持包括 Amazon, VMWare, Azure, and OpenStack 等云计算平台,其中针对OpenStack,目前支持Nova、cinder、glance、Neutron、Keystone、swift等API接口,但是不支持Ceilometer的API接口。
2.使用与Jar包
官方文档——强烈推荐
http://jclouds.apache.org/guides/openstack/
jcloudsjar包使用maven安装下载管理即可;
3.举例
import com.google.common.collect.ImmutableSet;import com.google.common.io.Closeables;import com.google.inject.Module;import org.jclouds.ContextBuilder;import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;import org.jclouds.openstack.nova.v2_0.NovaApi;import org.jclouds.openstack.nova.v2_0.domain.Server;import org.jclouds.openstack.nova.v2_0.features.ServerApi;import java.io.Closeable;import java.io.IOException;import java.util.Set;public class JCloudsNova implements Closeable { private final NovaApi novaApi; private final Set regions; public static void main(String[] args) throws IOException { JCloudsNova jcloudsNova = new JCloudsNova(); try { jcloudsNova.listServers(); jcloudsNova.close(); } catch (Exception e) { e.printStackTrace(); } finally { jcloudsNova.close(); } } public JCloudsNova() { Iterable modules = ImmutableSet.of(new SLF4JLoggingModule()); String provider = "openstack-nova"; String identity = "demo:demo"; // 项目管理员和用户 String credential = "devstack";// 用户密码 novaApi = ContextBuilder.newBuilder(provider) .endpoint("http://xxx.xxx.xxx.xxx:5000/v2.0/")//API地址 .credentials(identity, credential) .modules(modules) .buildApi(NovaApi.class); regions = novaApi.getConfiguredRegions(); } private void listServers() { for (String region : regions) { ServerApi serverApi = novaApi.getServerApi(region); System.out.println("Servers in " + region); for (Server server : serverApi.listInDetail().concat()) { System.out.println(" " + server); } } } public void close() throws IOException { Closeables.close(novaApi, true); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
4.测试效果:
比较稳定,而且API文档很全,教好理解;
OpenStack4j
1.介绍
针对OpenStack的Web-API,包含Keystone、Nova、Neutron、Glance、Cinder
、Swift、Ceilometer、Heat的API支持,更新速度快,有问题需求及时联系Github可以得到解决。
2.使用与Jar包
官方文档——http://www.openstack4j.com/learn/
http://www.openstack4j.com/javadoc/
OpenStack4j -jar包使用提供的 openstack4j-2.0.1-withdeps-sources.jar即可,注意使用的jdk最好为1.7以上,jdk1.7最佳。java ee 5 或Java ee 6 均可。(使用Myeclipse时,jdk版本为1.6,容易报 version 51 错误。)
3.举例:
List openStackNetworkList=new ArrayList(); openStackNetworkList.add("df6effa2-a1b5-400a-8ab1-457673591aa1"); String openstackImageId = "" String openstackFlavorId= ""; String openstackServerName = ""; String openstackZone = "nova:compute2"; ServerCreate sc = Builders.server().name(openstackServerName) .flavor(openstackFlavorId) .image(openstackImageId) .networks(openStackNetworkList) .availabilityZone(openstackZone) .build(); // Boot the Server OSClient os = OSFactory.builder() .endpoint("http://x.x.x.x:5000/v2.0/") .credentials("xxx","xxx") .tenantName("xxx") .authenticate(); Server server = os.compute().servers().boot(sc);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
4.使用效果
API简单易用;而且支持Ceilometer和Heat,推荐使用。