基于springboot web
一、依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.libvirt</groupId>
<artifactId>libvirt</artifactId>
<version>0.5.2</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.13.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.37</version>
</dependency>
二、代码
package com.libvirtddemo.controller;
import com.alibaba.fastjson2.JSON; // 引入fastjson库,用于将对象转换为JSON字符串
import lombok.extern.slf4j.Slf4j; // 引入Slf4j注解,方便日志记录
import org.libvirt.Connect; // 引入libvirt库的Connect类
import org.libvirt.Library; // 引入libvirt库的Library类
import org.libvirt.LibvirtException; // 引入libvirt库的LibvirtException类
import org.springframework.web.bind.annotation.GetMapping; // 引入Spring的GetMapping注解
import org.springframework.web.bind.annotation.RestController; // 引入Spring的RestController注解
import java.util.HashMap; // 引入HashMap类
import java.util.Map; // 引入Map类
import java.util.Objects; // 引入Objects类
@RestController // 声明该类是一个RESTful API控制器
@Slf4j // 添加Slf4j注解,方便日志记录
public class LibvirtdController {
static private Map<String, Connect> s_connections = new HashMap<String, Connect>(); // 定义一个静态Map对象,用于存储连接对象
@GetMapping("/connection") // 处理GET请求的映射,路径为"/connection"
public String connectLibvirt(String hypervisorURI) throws LibvirtException {
log.info("hypervisorURI: " + hypervisorURI); // 记录日志,输出hypervisorURI的值
if (hypervisorURI == null || Objects.equals(hypervisorURI, "")) { // 判断hypervisorURI是否为空或者为空字符串
hypervisorURI = "qemu:///system"; // 如果为空,设置默认值为"qemu:///system"
}
log.info("Looking for libvirtd connection at: " + hypervisorURI); // 记录日志,输出连接的libvirtd的URI
Connect conn = s_connections.get(hypervisorURI); // 从连接Map中获取指定URI的连接对象
if (conn == null) { // 如果连接对象为空
log.info("No existing libvirtd connection found. Opening a new one"); // 记录日志,表示没有已经存在的连接对象
conn = new Connect(hypervisorURI, false); // 创建新的连接对象
Library.initEventLoop(); // 初始化事件循环
log.info("Successfully connected to libvirt at: " + hypervisorURI); // 记录日志,表示成功连接到libvirtd
s_connections.put(hypervisorURI, conn); // 将连接对象加入到连接Map中
} else { // 如果连接对象不为空
try {
long version = conn.getVersion(); // 获取libvirtd的版本号
log.info("version:{}", version); // 记录日志,输出版本号
} catch (LibvirtException e) {
log.error("Connection with libvirtd is broken: " + e.getMessage()); // 记录错误日志,表示与libvirtd的连接中断
log.info("Opening a new libvirtd connection to: " + hypervisorURI); // 记录日志,表示打开一个新的libvirtd连接
conn = new Connect(hypervisorURI, false); // 创建新的连接对象
s_connections.put(hypervisorURI, conn); // 将连接对象加入到连接Map中
}
}
log.info("conn:{}", JSON.toJSONString(conn)); // 记录日志,输出连接对象的JSON字符串形式
return JSON.toJSONString(conn); // 将连接对象转换为JSON字符串并返回
}
}
URL说明
qemu:///... QEMU and KVM URIs¶
To use QEMU support in libvirt you must be running the libvirtd daemon (named libvirt_qemud in releases prior to 0.3.0). The purpose of this daemon is to manage qemu instances.
The libvirtd daemon should be started by the init scripts when the machine boots. It should appear as a process libvirtd --daemon running as root in the background and will handle qemu instances on behalf of all users of the machine (among other things).
So to connect to the daemon, one of two different URIs is used:
qemu:///system connects to a system mode daemon.
qemu:///session connects to a session mode daemon.
(If you do libvirtd --help, the daemon will print out the paths of the Unix domain socket(s) that it listens on in the various different modes).
KVM URIs are identical. You select between qemu, qemu accelerated and KVM guests in the guest XML as described here.
三、测试
curl http://localhost:8080/connection?hypervisorURI=qemu%3A%2F%2F%2Fsystem
curl http://localhost:8080/connection
四、官网案例
https://java.libvirt.org/
import org.libvirt.*;
public class minitest {
public static void main(String[] args) {
Connect conn=null;
try{
conn = new Connect("test:///default", true);
} catch (LibvirtException e) {
System.out.println("exception caught:"+e);
System.out.println(e.getError());
}
try{
Domain testDomain=conn.domainLookupByName("test");
System.out.println("Domain:" + testDomain.getName() + " id " +
testDomain.getID() + " running " +
testDomain.getOSType());
} catch (LibvirtException e) {
System.out.println("exception caught:"+e);
System.out.println(e.getError());
}
}
}