不同服务注册不同组件模块

This commit is contained in:
Cold
2025-12-09 17:55:08 +08:00
committed by 张斌
parent 87b3ac9878
commit 43a8834c5a
7 changed files with 347 additions and 45 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/gogf/gf/v2/encoding/gjson"
@@ -14,36 +15,39 @@ import (
)
var (
// globalClient 全局 RAGFlow 客户端(单例,自动初始化)
// globalClient 全局 RAGFlow 客户端(单例,延迟初始化)
globalClient *Client
clientOnce sync.Once
)
// init 包初始化时自动创建全局客户端
func init() {
ctx := context.Background()
// initClient 延迟初始化客户端
func initClient() {
clientOnce.Do(func() {
ctx := context.Background()
// 读取配置
baseURL, apiKey := loadConfig(ctx)
// 读取配置
baseURL, apiKey := loadConfig(ctx)
// 如果配置不完整,跳过初始化
if baseURL == "" || apiKey == "" {
g.Log().Warning(ctx, "⚠️ RAGFlow 配置未找到,请在项目 config.yml 中添加 ragflow.base_url 和 ragflow.api_key")
return
}
// 如果配置不完整,跳过初始化
if baseURL == "" || apiKey == "" {
g.Log().Warning(ctx, "⚠️ RAGFlow 配置未找到,请在项目 config.yml 中添加 ragflow.base_url 和 ragflow.api_key")
return
}
// 初始化全局客户端
httpClient := gclient.New()
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
httpClient.SetHeader("Content-Type", "application/json")
httpClient.SetTimeout(60 * time.Second) // RAGFlow AI 推理需要较长时间
// 初始化全局客户端
httpClient := gclient.New()
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
httpClient.SetHeader("Content-Type", "application/json")
httpClient.SetTimeout(180 * time.Second) // RAGFlow AI 推理需要较长时间
globalClient = &Client{
BaseURL: strings.TrimSuffix(baseURL, "/"),
APIKey: apiKey,
HTTPClient: httpClient,
}
globalClient = &Client{
BaseURL: strings.TrimSuffix(baseURL, "/"),
APIKey: apiKey,
HTTPClient: httpClient,
}
g.Log().Infof(ctx, "✅ RAGFlow 全局客户端初始化成功: baseURL=%s", baseURL)
g.Log().Infof(ctx, "✅ RAGFlow 全局客户端初始化成功: baseURL=%s", baseURL)
})
}
// loadConfig 从配置文件加载 RAGFlow 配置
@@ -54,9 +58,10 @@ func loadConfig(ctx context.Context) (baseURL, apiKey string) {
return
}
// GetGlobalClient 获取全局客户端
// GetGlobalClient 获取全局客户端(延迟初始化)
// 使用示例client := ragflow.GetGlobalClient()
func GetGlobalClient() *Client {
initClient()
return globalClient
}