|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package com.dsideal.Res.Plugin;
|
|
|
|
|
|
|
|
|
|
import com.jfinal.plugin.IPlugin;
|
|
|
|
|
import io.milvus.pool.MilvusClientV2Pool; // Milvus V2版本连接池类
|
|
|
|
|
import io.milvus.pool.PoolConfig; // 连接池配置类
|
|
|
|
|
import io.milvus.v2.client.ConnectConfig; // 连接配置类
|
|
|
|
@ -10,10 +11,13 @@ import org.slf4j.LoggerFactory; // 日志工厂
|
|
|
|
|
import java.time.Duration; // 时间间隔类
|
|
|
|
|
|
|
|
|
|
@Getter // 自动为所有字段生成getter方法
|
|
|
|
|
public class MilvusPlugin {
|
|
|
|
|
public class MilvusPlugin implements IPlugin {
|
|
|
|
|
private static MilvusPlugin instance; // 单例实例
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MilvusPlugin.class); // 日志记录器
|
|
|
|
|
private MilvusClientV2Pool pool; // Milvus连接池实例
|
|
|
|
|
private String host; // Milvus服务地址
|
|
|
|
|
private int port; // Milvus服务端口
|
|
|
|
|
private int maxConnections; // 最大连接数
|
|
|
|
|
|
|
|
|
|
// 私有构造方法,确保单例模式
|
|
|
|
|
private MilvusPlugin() {
|
|
|
|
@ -27,9 +31,43 @@ public class MilvusPlugin {
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化连接池
|
|
|
|
|
public void init(String host, int port, int maxConnections) {
|
|
|
|
|
// 设置Milvus连接配置
|
|
|
|
|
public void setConfig(String host, int port, int maxConnections) {
|
|
|
|
|
this.host = host;
|
|
|
|
|
this.port = port;
|
|
|
|
|
this.maxConnections = maxConnections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从连接池获取客户端连接
|
|
|
|
|
public MilvusClientV2 getClient() {
|
|
|
|
|
try {
|
|
|
|
|
if (pool != null) {
|
|
|
|
|
return pool.getClient("client_name"); // 使用固定key获取连接
|
|
|
|
|
} else {
|
|
|
|
|
logger.error("Milvus连接池未初始化");
|
|
|
|
|
throw new RuntimeException("Milvus连接池未初始化");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("获取Milvus连接失败: " + e.getMessage(), e);
|
|
|
|
|
throw new RuntimeException("获取Milvus连接失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 归还客户端连接到连接池
|
|
|
|
|
public void returnClient(MilvusClientV2 client) {
|
|
|
|
|
if (client != null && pool != null) {
|
|
|
|
|
pool.returnClient("client_name", client); // 使用固定key归还连接
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean start() {
|
|
|
|
|
try {
|
|
|
|
|
if (host == null || port <= 0 || maxConnections <= 0) {
|
|
|
|
|
logger.error("Milvus连接配置不完整");
|
|
|
|
|
throw new RuntimeException("Milvus连接配置不完整");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建连接配置
|
|
|
|
|
ConnectConfig connectConfig = ConnectConfig.builder()
|
|
|
|
|
.uri("http://" + host + ":" + port) // 设置Milvus服务地址
|
|
|
|
@ -47,33 +85,24 @@ public class MilvusPlugin {
|
|
|
|
|
// 创建连接池实例
|
|
|
|
|
pool = new MilvusClientV2Pool(poolConfig, connectConfig);
|
|
|
|
|
logger.info("Milvus连接池初始化成功,最大连接数:{}", maxConnections);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Milvus初始化失败: " + e.getMessage(), e);
|
|
|
|
|
throw new RuntimeException("Milvus初始化失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从连接池获取客户端连接
|
|
|
|
|
public MilvusClientV2 getClient() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean stop() {
|
|
|
|
|
try {
|
|
|
|
|
return pool.getClient("client_name"); // 使用固定key获取连接
|
|
|
|
|
if (pool != null) {
|
|
|
|
|
pool.close(); // 释放所有连接资源
|
|
|
|
|
logger.info("Milvus连接池已关闭");
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("获取Milvus连接失败: " + e.getMessage(), e);
|
|
|
|
|
throw new RuntimeException("获取Milvus连接失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 归还客户端连接到连接池
|
|
|
|
|
public void returnClient(MilvusClientV2 client) {
|
|
|
|
|
if (client != null) {
|
|
|
|
|
pool.returnClient("client_name",client); // 使用固定key归还连接
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭连接池
|
|
|
|
|
public void close() {
|
|
|
|
|
if (pool != null) {
|
|
|
|
|
pool.close(); // 释放所有连接资源
|
|
|
|
|
logger.error("关闭Milvus连接池失败: " + e.getMessage(), e);
|
|
|
|
|
throw new RuntimeException("关闭Milvus连接池失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|