开始是通过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. 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  So ...

  2. SEO-百度推出新算法如何应对

    > 如何知道百度推出新算法百度推出算法的趋势> 学SEO目的做排名,长流量,赚钱> 最近一年百度搜索变动1> 2012年6月:6/22, 6/28事件,百度地震,4.5%网站被 ...

  3. (30)批处理文件.bat

    批处理文件(bat) 简单的说,批处理的作用就是自动的连续执行多条命令 .编写bat处理文件可以使用记事本的方式: 常见批处理文件的命令: echo 表示显示此命令后的字符 tiltle 设置窗口的标 ...

  4. 看了一个烟花的html作品 --引用:http://www.w3cfuns.com/blog-5444049-5404365.html

    最近老大想把项目改成响应式,一直在学习没时间更新博客.今天看到一个原生的js烟花项目,感觉很好,把记下来,以后把妹用. [run]<!DOCTYPE html><html>&l ...

  5. Win10上编译CoreCLR的Windows和Linux版本

    一.编译环境 首先,不管是Windows还是Linux版本CoreCLR的编译,都是在Windows10上进行的. 二.CoreCLR for Windows 在Windows上做编译怎么能少得了Vi ...

  6. java中有符号和无符号数据类型发生转换

    package com.itheima.test01;/* * byte short int long float double 是有符号位的数 * char boolean 是无符号位的数 * 补码 ...

  7. Zeppelin interperter 模式设置总结

    如有错漏,望请指正,不胜感激. 参考:[zeppelin官网]:https://zeppelin.apache.org/docs/latest/interpreter/spark.html#inter ...

  8. 腾讯QQ会员技术团队:以手机QQ会员H5加速为例,为你揭开sonic技术内幕

    目前移动端越多越多的网页开始H5化,一方面可以减少安装包体积,另一方面也方便运营.但是相对于原生界面而言,H5的慢速问题一定被大家所诟病,针对这个问题,目前手Q存在几种方案,最常见的便是离线包方案,但 ...

  9. css3渐变生成器网页

    http://westciv.com/tools/gradients/ http://www.colorzilla.com/gradient-editor/

  10. Alamofire源码解读系列(九)之响应封装(Response)

    本篇主要带来Alamofire中Response的解读 前言 在每篇文章的前言部分,我都会把我认为的本篇最重要的内容提前讲一下.我更想同大家分享这些顶级框架在设计和编码层次究竟有哪些过人的地方?当然, ...