使用java连接Libvirtd

基于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的URIConnect 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);  // 记录日志,表示成功连接到libvirtds_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());}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/77426.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图书管理信息系统分析与设计

一、系统开发的可行性分析 &#xff08;一&#xff09;系统背景.必要性及意义 随着社会经济的迅速发展和科学技术的全面进步&#xff0c;计算机事业的飞速发展&#xff0c;以计算机与通信技术为基础的信息系统正处于蓬勃发展的时期。随着经济文化水平的显著提高&#xff0c;人…

认识网线上的各种参数标号

最近工作需要&#xff0c;接触了很多不同类型的网线&#xff0c;为了能够区分不同型号的网线&#xff0c;特意做一篇笔记用来学习&#xff0c;如有记录有误之处&#xff0c;欢迎大家指正~初步认识网线 常用的网络电缆有三种&#xff1a;双绞线、同轴电缆和光纤电缆&#xff08…

5. spark 参数问题

官网地址&#xff1a;spark 参数 在代码中设置参数 spark-default.conf < 命令行< 代码内部设置参数&#xff0c; 对于一常用的参数可以放在配置文件&#xff0c;对于一些个性化参数则通过 命令行传递比较好。 命令行 命令行传递参数&#xff0c;有一些快捷方式&…

thinkphp6 入门(4)--数据库操作 增删改查

一、设计数据库表 比如我新建了一个数据库表&#xff0c;名为test 二、配置数据库连接信息 本地测试 直接在.env中修改&#xff0c;不用去config/database.php中修改 正式环境 三、增删改查 引入Db库 use think\facade\Db; 假设新增的控制器路径为 app\test\control…

B+Tree 索引结构

文章目录 1. 数据库索引2. BTree 索引结构 1. 数据库索引 索引是为了提高数据的查询速度&#xff0c;相当于给数据进行编号&#xff0c;在查找数据的时候就可以通过编号快速找到对应的数据。索引用的是 B Tree 数据结构。 乱序插入数据&#xff0c;会自动按照 id 进行升序排…

ipad触控笔是哪几款?开学季便宜的ipad电容笔推荐

在新学期的时候&#xff0c;什么样的电容笔才是最好的选择&#xff1f;苹果原装的电容笔&#xff0c;非常的好用&#xff0c;功能也非常的全面&#xff0c;唯一的缺点&#xff0c;就是价格昂贵&#xff0c;不是普通的学生能够负担得起的&#xff0c;而且电容笔非常的重&#xf…

leetcode 15. 三数之和

给你一个整数数组 nums &#xff0c;判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k &#xff0c;同时还满足 nums[i] nums[j] nums[k] 0 。请 你返回所有和为 0 且不重复的三元组。 注意&#xff1a;答案中不可以包含重复的三元组。 15. 三…

WebServer 解析HTTP 响应报文

一、基础API部分&#xff0c;介绍stat、mmap、iovec、writev、va_list 1.1 stat​ 作用&#xff1a;获取文件信息 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>// 获取文件属性&#xff0c;存储在statbuf中 int stat(const char *…

kubernetes进阶 (三) 基础练习

前两天朋友给了我几道题&#xff0c;看着挺简单的&#xff0c;但实际做的时候发现坑不少&#xff0c;这里做下笔记 一、镜像构建部署lnmp 1、构建镜像 nginx、php、mysql 要求使用centos7作为基础镜像 2、使用deployment部署上面的容器,要求3个服务要放到一个pod中(虽然这样是…

查看Oracle_表名、字段名、注释、进程及杀进程等常用语句

-- 查看ORACLE 数据库中本用户下的所有表 SELECT table_name FROM user_tables; -- 查看ORACLE 数据库中所有用户下的所有表 select user,table_name from all_tables; -- 查看ORACLE 数据库中本用户下的所有列 select table_name,column_name from user_tab_columns; -- 查…

【工作记录】MQTT介绍、安装部署及springboot集成@20230912

背景 近期公司可能会有物联网设备相关项目内容&#xff0c;提前对用到的mqtt协议做预研和初步使用。 最初接触到mqtt协议应该是早些年的即时通讯吧&#xff0c;现在已经是物联网设备最热门的协议了。 作为记录&#xff0c;也希望能帮助到需要的朋友。 MQTT介绍 《MQTT 协议规…

ubuntu20.04安装cmake

配置国内ubuntu源 编辑/etc/apt/sources.list文件, 在文件最前面添加以下条目(操作前做好相应备份) cd /etc/apt sudo cp sources.list sources.list.bak sudo gedit sources.list增加阿里云&#xff0c;在source.list文件开头增加如下内容 deb http://mirrors.aliyun.com/ub…

spring实战笔记

Environment中获取配置 方式一&#xff1a;直接getProperties获取String bootstrapServers env.getProperty("hello.kafka.bootstrap-servers");方式二&#xff1a;将属性直接绑定到对象上BindResult<TestKafkaProperties> kafkaProperties Binder.get(env)…

在 CentOS 7 上安装中文字体

在 CentOS 7 上安装中文字体 1. 安装中文字体包&#xff1a;2. 配置字体&#xff1a;3. 更新字体缓存&#xff1a; 在 CentOS 7 上安装中文字体可以按照以下步骤进行&#xff1a; 1. 安装中文字体包&#xff1a; sudo yum install -y fontconfig sudo yum install -y cjkuni-…

Text Workflow for Mac,简单易用的文本转换工具

如果你需要一个能够将文本转换成多种语言和文件格式的工具&#xff0c;那么Text Workflow for Mac将是你的不二之选。 这个软件支持多种语言翻译和多种文件格式转换&#xff0c;让你可以轻松地将文本转换成你需要的形式。而且&#xff0c;它的操作非常简单&#xff0c;只需要几…

恢复硬盘数据很简单!记好这4个方法!

“我的电脑硬盘里超级多重要的数据&#xff0c;在清理时我一不留神把硬盘中部分文件删掉了。有没有实用的方法可以恢复这些硬盘数据呀&#xff1f;” 在使用电脑时&#xff0c;数据丢失是每个计算机用户都可能面临的问题。不过&#xff0c;当我们丢失数据时&#xff0c;不要太过…

房地产小程序 | 小程序赋能,房地产业务数字化升级

随着科技的不断发展&#xff0c;房地产行业正逐渐向数字化转型。在这个过程中&#xff0c;房地产小程序成为了一种重要的工具&#xff0c;可以帮助房地产企业提供更好的购房体验、增加销售额&#xff0c;并实现管理的便捷化。 优点 便捷购房体验&#xff1a;房地产小程序为用户…

jira流转issue条目状态transitions的rest实用脚本,issue状态改变调整

官方文档链接地址&#xff1a; POST Transition issue Performs an issue transition and, if the transition has a screen, updates the fields from the transition screen. sortByCategory To update the fields on the transition screen, specify the fields in the fiel…

git: ‘lfs‘ is not a git command unclear

首先可以尝试 git lfs install 是否可以&#xff0c;不可以后就看这个连接&#xff1a;https://stackoverflow.com/questions/48734119/git-lfs-is-not-a-git-command-unclear。 我的是ubuntu&#xff0c;所以&#xff1a; 保证这个前提&#xff1a; git-lfs requires git ve…

数据结构(C语言)——单链表

整体结构如上&#xff1a;看似简单&#xff0c;但第一次用C语言实现还是感觉有点吃力&#xff0c;尤其是特别容易让链表断裂 下面是代码&#xff1a;&#xff08;有链表的增删改查&#xff09; 注&#xff1a;这里E类型是用define将int进行了宏定义 #include <stdio.h> …