开始是通过java代码调用vsphere提供的原始接口,从而控制vcenter的操作。当第一个版本做完之后发现代码执行的速度特别慢,后来在网上看到有人用vijava(对vsphere原始接口封装)编程,自己就试着换了几个接口发现代码执行速度很快。所以第二版都换了vijava操作。下面就和大家一起学习下如何通过vijava控制vcenter。

首先去github上下载vijava项目,然后将其导入自己的项目。

利用vijava完成vcenter连接类的创建:所有代码都可以之间运行。

package com.iking.vmware.connection;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.ws.soap.SOAPFaultException;
import com.iking.exception.VcenterException;
import com.iking.vmware.bean.VsphereConst;
import com.iking.vmware.vim25.mo.InventoryNavigator;
import com.iking.vmware.vim25.mo.ManagedEntity;
import com.iking.vmware.vim25.mo.ServerConnection;
import com.iking.vmware.vim25.mo.ServiceInstance; /**
* @description 操作vcenter的连接和断开,以及定义公共应用类
* @date 2017年2月8日14:35:38
* @version 1.1
* @author DiWk
*/
public class ConnectedVimServiceBase {
public ServiceInstance si = null; /**
* @description 链接vcenter
* @date 2017年2月8日14:23:37
* @version 1.1
* @author DiWk
*/
public void connect(String url, String userName, String passWord) {
try {
si = new ServiceInstance(new URL("https://" + url + "/sdk"), userName, passWord, true);
} catch (SOAPFaultException sfe) {
VcenterException.printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* @description 断开vcenter链接
* @date 2017年2月8日14:23:37
* @version 1.1
* @author DiWk
*/
public void disconnect() {
try {
si.getServerConnection().logout();
} catch (SOAPFaultException sfe) {
VcenterException.printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* @description 获取链接URL
* @date 2017年2月8日14:23:37
* @version 1.1
* @author DiWk
*/
public URL getUrl() {
ServerConnection serverConnection = si.getServerConnection();
URL url = null;
if (serverConnection != null) {
url = serverConnection.getUrl();
} else {
return null;
}
return url;
}

利用vijava对获取集群对象及相关信息:

package com.iking.vmware.cluster;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.ws.soap.SOAPFaultException;
import com.iking.exception.VcenterException;
import com.iking.vmware.connection.ConnectedVimServiceBase;
import com.iking.vmware.vim25.ClusterComputeResourceSummary;
import com.iking.vmware.vim25.mo.ClusterComputeResource;
import com.iking.vmware.vim25.mo.Datastore;
import com.iking.vmware.vim25.mo.InventoryNavigator;
import com.iking.vmware.vim25.mo.ManagedEntity; /**
* @description 操作vcenter中的集群对象
* @date 2017年2月8日14:35:38
* @version 1.1
* @author DiWk
*/
public class ClusterComputerResourceSummary { private ConnectedVimServiceBase cs = null; //连接类声明 public ConnectedVimServiceBase getCs() {
return cs;
} public void setCs(ConnectedVimServiceBase cs) {
this.cs = cs;
} /**
* @description 获取vcenter中所有的集群对象
* @date 2017年2月3日10:42:09
* @return clusterList 集群对象集合
* @version 1.1
* @author DiWk
*/
public List<ClusterComputeResource> getClusterList() {
List<ClusterComputeResource> clusterList = new ArrayList<ClusterComputeResource>();
ClusterComputeResource clusterComputeResource = null;
try {
ManagedEntity[] managedEntities = new InventoryNavigator(cs.si.getRootFolder())
.searchManagedEntities("ClusterComputeResource");
if (managedEntities != null && managedEntities.length > 0) {
for (ManagedEntity managedEntity : managedEntities) {
clusterComputeResource = (ClusterComputeResource) managedEntity;
clusterList.add(clusterComputeResource);
}
}
else {
return null;
}
} catch (SOAPFaultException sfe) {
VcenterException.printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
return clusterList;
} /**
* @description 根据集群名称获取对应的集群对象
* @date 2017年2月3日10:44:02
* @return clusterList 集群对象集合
* @version 1.1
* @author DiWk
*/
public List<ClusterComputeResource> getClusterListByName(List<String> ClustersName) {
List<ClusterComputeResource> clusterList = new ArrayList<ClusterComputeResource>();
ClusterComputeResource clusterComputeResource = null;
try {
if (ClustersName == null || ClustersName.size() < 0) {
return null;
}
List<ClusterComputeResource> clusterList2 = getClusterList();
if (clusterList2 == null || clusterList2.size() < 0) {
return null;
}
for (String string : ClustersName) {
for (ClusterComputeResource clusterComputeResource2 : clusterList2) {
if (clusterComputeResource2.getName().equals(string)) {
clusterList.add(clusterComputeResource);
}
}
}
} catch (SOAPFaultException sfe) {
VcenterException.printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
return clusterList;
} /**
* @description 根据集群名称获取对应的集群summary
* @date 2017年2月3日10:54:18
* @return clusterSumList 集群对象summary集合
* @version 1.1
* @author DiWk
*/
public List<ClusterComputeResourceSummary> getClusterComputeResourceSummary(List<String> ClustersName) {
List<ClusterComputeResourceSummary> clusterSumList = new ArrayList<ClusterComputeResourceSummary>();
List<ClusterComputeResource> clusterListByName = null;
try {
clusterListByName = getClusterListByName(ClustersName);
if (clusterListByName != null && clusterListByName.size() > 0) {
for (ClusterComputeResource cluster : clusterListByName) {
ClusterComputeResourceSummary summary = (ClusterComputeResourceSummary) cluster.getSummary();
clusterSumList.add(summary);
}
} else {
return null;
}
} catch (SOAPFaultException sfe) {
VcenterException.printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
return clusterSumList;
} /**
* @description 根据集群名称获取集群关联的数据存储
* @date 2017年2月3日11:02:09
* @return clusterDataStore 集群所关联的数据存储的集合
* @version 1.1
* @author DiWk
*/
public List<Datastore> getDataStoreByClusterNm(List<String> ClustersName) {
List<Datastore> clusterDataStore = new ArrayList<Datastore>();
List<ClusterComputeResource> clusterListByName = null;
try {
clusterListByName = getClusterListByName(ClustersName);
if (clusterListByName != null && clusterListByName.size() > 0) {
for (ClusterComputeResource cluster : clusterListByName) {
Datastore[] datastores = cluster.getDatastores();
clusterDataStore.addAll(Arrays.asList(datastores));
}
} else {
return null;
}
} catch (SOAPFaultException sfe) {
VcenterException.printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
return clusterDataStore;
} //集群测试方法
public static void main(String[] args) {
ConnectedVimServiceBase cs = new ConnectedVimServiceBase();
cs.connect("192.168.1.253", "administrator@vsphere.local","Iking!@#456");
ClusterComputerResourceSummary cluster = new ClusterComputerResourceSummary();
cluster.setCs(cs);
List<ClusterComputeResource> clusterList = cluster.getClusterList();
if (clusterList != null && clusterList.size() > 0) {
for (ClusterComputeResource clusterComputeResource : clusterList) {
System.out.println(clusterComputeResource.getName());
}
}
}
}

vijava将集群对象和其属性进行了封装,当获取到ClusterComputeResource、ClusterComputeResourceSummary或者其他集群的对象,我们就能够获取到集群对象关联的属性,从而完成对集群的操作和监控。这一篇是关于集群的几个简单方法,完成更具体的需求还需要多多尝试。

Vmware Vsphere WebService之vijava 开发一-vcenter连接、及集群信息获取的更多相关文章

  1. Vmware Vsphere WebService之vijava 开发(二)一性能信息的采集(实时监控)

    最近一直没有更新这部分的内容,会利用五一时间完成vcenter这一个系列. 这里先给大家一本关于vijava开发的书,比较实用. 地址:http://pan.baidu.com/s/1gfkl9mj. ...

  2. Vmware Vsphere WebService SDK开发(第一讲)-基本知识学习

    刚开始这方面开发的时候,不知道如何下手,能够查到的资料特别少,而且看到很多网友和我一样也在找这方面的资料.接下来的一段时间我就结合自己所参与的项目,完成关于Vmware Vsphere WebServ ...

  3. VMware vSphere服务器虚拟化实验十五 vCenter vShield Manager

    VMware vSphere服务器虚拟化实验十五 vCenter vShield Manager VMware  vShield Manager是专为 VMware vCenter Server 集成 ...

  4. [MapReduce_add_1] Windows 下开发 MapReduce 程序部署到集群

    0. 说明  Windows 下开发 MapReduce 程序部署到集群 1. 前提 在本地开发的时候保证 resource 中包含以下配置文件,从集群的配置文件中拷贝 在 resource 中新建  ...

  5. windows下eclipse远程连接hadoop集群开发mapreduce

    转载请注明出处,谢谢 2017-10-22 17:14:09  之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...

  6. KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情

    KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...

  7. Vmware vsphere webservice sdk 连接打开慢的问题

    还在为VimService实例化速度慢的问题烦恼吗?这有一篇文章可以帮你解决问题,英文水平所限,就不翻译了,原文地址http://kb.vmware.com/selfservice/microsite ...

  8. VMWare安装Ubuntu及配置开发环境遇到的问题集

    安装完Ubuntu改为中文,发现是中英文混搭的界面 sudo apt-get install $(check-language-support --language=zh_CN)更新语言包. Ecli ...

  9. iOS开发----地图与导航--定位和位置信息获取

    要实现地图.导航功能,往往需要先熟悉定位功能,在iOS中通过Core Location框架进行定位操作.Core Location自身可以单独使用,和地图开发框架MapKit完全是独立的,但是往往地图 ...

随机推荐

  1. 1798: [Ahoi2009]Seq 维护序列seq

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 2930  Solved: 1087[Submit ...

  2. 基于JAVA语言的selenium测试基础总结

    目录一.基本语句1.循环控制(break,continue)3.字符的替换(replace,repalceFirst,replaceAll,regex)4.字符串的连接("+",a ...

  3. html中submit和button的区别(总结) [ 转自欣步同学 ]

    html中submit和button的区别(总结) submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了. 如果表单在点击提交按钮后需要用JS进行处理(包括输入验证 ...

  4. 构建微服务-使用OAuth 2.0保护API接口

    微服务操作模型 基于Spring Cloud和Netflix OSS 构建微服务-Part 1 基于Spring Cloud和Netflix OSS构建微服务,Part 2 在本文中,我们将使用OAu ...

  5. Jdom读取XML文件

    学习Spring时,我们经常看到很多xml配置文件,Spring通过在配置文件中的配置,使用IOC(控制反转),从而实现代码的灵活性,本篇我就为大家介绍一种解析xml方式--Jdom 首先我们到Jdo ...

  6. 图解Javascript——作用域、作用域链、闭包

    什么是作用域? 作用域是一种规则,在代码编译阶段就确定了,规定了变量与函数的可被访问的范围.全局变量拥有全局作用域,局部变量则拥有局部作用域. js是一种没有块级作用域的语言(包括if.for等语句的 ...

  7. Azure Messaging-ServiceBus Messaging消息队列技术系列6-消息回执

    上篇博文中我们介绍了Azure Messaging的重复消息机制.At most once 和At least once. Azure Messaging-ServiceBus Messaging消息 ...

  8. 给angularJs grid列上添加自定义按钮

    由于项目需要在angular 显示的表格中添加按钮,多次查询资料终于找到解决方法.就是给columnDefs 上的列增加 cellTemplate,同时绑定对应的触发事件,代码如下 columnDef ...

  9. Windows搭建以太坊的私有链环境

    1.下载Geth.exe 运行文件,并安装 https://github.com/ethereum/go-ethereum/releases/ 下载后,只有一个Geth.exe的文件 2.cmd进入按 ...

  10. exports和module.exports的区别

    总结:exports是module.exports的指向. 1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 ...