| | |
| | | package models |
| | | |
| | | import ( |
| | | "context" |
| | | "fmt" |
| | | "log" |
| | | "vamicro/api-gateway/models" |
| | | commonModels "vamicro/camera-common/models" |
| | | |
| | | "github.com/milvus-io/milvus-sdk-go/v2/client" |
| | | ) |
| | | |
| | | var dbClient *MilvusClient |
| | | |
| | | // MilvusClient 封装Milvus操作的结构体 |
| | | type MilvusClient struct { |
| | | client client.Client |
| | | } |
| | | |
| | | // NewMilvusClient 创建Milvus客户端 |
| | | func NewMilvusClient(ctx context.Context, addr string) (*MilvusClient, error) { |
| | | milvusClient, err := client.NewGrpcClient(ctx, addr) |
| | | if err != nil { |
| | | return nil, fmt.Errorf("failed to connect to Milvus: %v", err) |
| | | } |
| | | return &MilvusClient{client: milvusClient}, nil |
| | | } |
| | | |
| | | // Close 关闭连接 |
| | | func (m *MilvusClient) Close() error { |
| | | return m.client.Close() |
| | | } |
| | | |
| | | // 初始化milvus |
| | | func InitVectorDb() { |
| | | // 连接到Milvus服务器 |
| | | ctx := context.Background() |
| | | //milvusClient, err := NewMilvusClient(ctx, "192.168.1.232:19530") |
| | | |
| | | config := models.GetConfig() |
| | | url := config.MilvusUrl |
| | | |
| | | milvusClient, err := NewMilvusClient(ctx, url) |
| | | if err != nil { |
| | | log.Fatal("Failed to connect to Milvus:", err) |
| | | } |
| | | //defer milvusClient.client.Close() |
| | | |
| | | fmt.Println("Successfully connected to Milvus") |
| | | |
| | | //初始化 |
| | | dbClient = milvusClient |
| | | commonModels.SetClientDB(dbClient.client) |
| | | } |
| | | |
| | | // GetDB ... |
| | | func GetDBCient() *MilvusClient { |
| | | return dbClient |
| | | } |
| | | |
| | | func CloseDBClient() { |
| | | dbClient.Close() |
| | | } |
| | | package models
|
| | |
|
| | | import (
|
| | | "context"
|
| | | "fmt"
|
| | | "log"
|
| | | "vamicro/api-gateway/models"
|
| | | commonModels "vamicro/camera-common/models"
|
| | |
|
| | | "github.com/milvus-io/milvus-sdk-go/v2/client"
|
| | | )
|
| | |
|
| | | var dbClient *MilvusClient
|
| | |
|
| | | // MilvusClient 封装Milvus操作的结构体
|
| | | type MilvusClient struct {
|
| | | client client.Client
|
| | | }
|
| | |
|
| | | // NewMilvusClient 创建Milvus客户端
|
| | | func NewMilvusClient(ctx context.Context, addr string) (*MilvusClient, error) {
|
| | | milvusClient, err := client.NewGrpcClient(ctx, addr)
|
| | | if err != nil {
|
| | | return nil, fmt.Errorf("failed to connect to Milvus: %v", err)
|
| | | }
|
| | | return &MilvusClient{client: milvusClient}, nil
|
| | | }
|
| | |
|
| | | // Close 关闭连接
|
| | | func (m *MilvusClient) Close() error {
|
| | | return m.client.Close()
|
| | | }
|
| | |
|
| | | // 初始化milvus
|
| | | func InitVectorDb() {
|
| | | // 连接到Milvus服务器
|
| | | ctx := context.Background()
|
| | | //milvusClient, err := NewMilvusClient(ctx, "192.168.1.232:19530")
|
| | |
|
| | | config := models.GetConfig()
|
| | | url := config.MilvusUrl
|
| | |
|
| | | milvusClient, err := NewMilvusClient(ctx, url)
|
| | | if err != nil {
|
| | | log.Fatal("Failed to connect to Milvus:", err)
|
| | | }
|
| | | //defer milvusClient.client.Close()
|
| | |
|
| | | fmt.Println("Successfully connected to Milvus")
|
| | | |
| | | //初始化
|
| | | dbClient = milvusClient
|
| | | commonModels.SetClientDB(dbClient.client)
|
| | | }
|
| | |
|
| | | // GetDB ...
|
| | | func GetDBCient() *MilvusClient {
|
| | | return dbClient
|
| | | }
|
| | |
|
| | | func CloseDBClient() {
|
| | | dbClient.Close()
|
| | | }
|