da9c76f073
此前 gateway /readyz 用的是 Enabled() 启动期降级标志(反映不了运行中 PG 掉线,LB 会继续 往已不可用实例导流);dispatcher/mcp-go 干脆没有 HTTP 探针(只有 NATS ServeHealth,k8s/LB 够不着、只能靠 gateway 经 NATS 代探)。 - gateway /readyz 改用 db.Ping 实时探活。只把 **DB 当硬依赖门**:Redis 掉线仍可服务(限流有 A5 进程内 fail-safe 兜底、SSE 回落 live NATS),一 blip 就把全部实例踢出轮转反而制造整站 故障,故 Redis 只上报不 gate。NATS 启动即连(fatal)不单列。 - 新 sundynix-shared/health 包:Serve/Handler 提供 /healthz(恒 200 liveness) + /readyz(由 ready() 决定 readiness)。dispatcher(:8091, DISPATCHER_HEALTH_ADDR)与 mcp-go(:8092, MCP_GO_HEALTH_ADDR)各起一个,readiness = NATS 连接可用(bus.IsConnected)。接入各自优雅停机。 - bus 加 IsConnected()(nc.IsConnected 实时);dispatcher Subscriber 透传。 四模块 build+vet+test 绿;health 带 Handler 单测。探针端口仅内部用(对外仍只暴露 gateway)。 compose/k8s 的 healthcheck 编排属 C 层 ops,另做。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
140 lines
5.9 KiB
Go
140 lines
5.9 KiB
Go
// Command server 启动 sundynix-mcp-go —— 第 5 层 Go I/O 型 MCP 工具微服务。
|
||
package main
|
||
|
||
import (
|
||
"context"
|
||
"log"
|
||
"os"
|
||
"os/signal"
|
||
"syscall"
|
||
"time"
|
||
|
||
"github.com/sundynix/sundynix-shared/blob"
|
||
sharedbus "github.com/sundynix/sundynix-shared/bus"
|
||
"github.com/sundynix/sundynix-shared/contract"
|
||
"github.com/sundynix/sundynix-shared/health"
|
||
|
||
"github.com/sundynix/sundynix-mcp-go/internal/history"
|
||
"github.com/sundynix/sundynix-mcp-go/internal/mcp"
|
||
"github.com/sundynix/sundynix-mcp-go/internal/memory"
|
||
"github.com/sundynix/sundynix-mcp-go/internal/rag"
|
||
|
||
"github.com/sundynix/sundynix-shared/otelx"
|
||
"github.com/sundynix/sundynix-shared/prompts"
|
||
"github.com/sundynix/sundynix-shared/secrets"
|
||
)
|
||
|
||
func main() {
|
||
secrets.MustHaveKeyInProd() // 生产须配 SUNDYNIX_SECRET_KEY 以解密下发的 api_key 密文
|
||
otelx.SetupSlog("sundynix-mcp-go") // 结构化日志 + 链路感知(工具日志带 trace_id)
|
||
|
||
// 链路追踪:工具服务端 span 续上 dispatcher 的 trace(tool.call → tool.serve)。
|
||
shutdownTrace, _ := otelx.Init(context.Background(), "sundynix-mcp-go")
|
||
defer func() { _ = shutdownTrace(context.Background()) }()
|
||
|
||
// 受管 prompt:登记内置默认 + 加载 PROMPTS_FILE 覆盖(不重编译即可改图谱抽取词)。
|
||
prompts.SetDefault(prompts.GraphExtract,
|
||
"你是知识图谱抽取器。从用户文本中抽取知识三元组,输出 JSON 数组,每项形如 {\"s\":\"主体\",\"p\":\"关系\",\"o\":\"客体\"}。实体用简洁名词,关系用简短动词短语。只输出 JSON,不要任何解释或代码块标记。")
|
||
prompts.LoadFile()
|
||
|
||
natsURL := envOr("NATS_URL", "nats://localhost:4222")
|
||
// DSN 用 127.0.0.1 而非 localhost:免 pgx 每连接 DNS 解析,避免高并发连接池扩容时 DNS 雪崩
|
||
// (见 LOAD_TEST_REPORT §4.3)。
|
||
pgDSN := envOr("POSTGRES_DSN", "postgres://sundynix:sundynix@127.0.0.1:5432/sundynix?sslmode=disable")
|
||
redisAddr := envOr("REDIS_ADDR", "localhost:6379")
|
||
milvusAddr := envOr("MILVUS_ADDR", "localhost:19530")
|
||
embBase := envOr("EMBED_BASE_URL", "") // OpenAI 兼容 embeddings 端点(空=向量检索降级)
|
||
embKey := envOr("EMBED_API_KEY", "")
|
||
embModel := envOr("EMBED_MODEL", "")
|
||
rerankBase := envOr("RERANK_BASE_URL", "") // DashScope 文本重排端点(空=不启用 rerank)
|
||
rerankKey := envOr("RERANK_API_KEY", "")
|
||
rerankModel := envOr("RERANK_MODEL", "")
|
||
neo4jURI := envOr("NEO4J_URI", "neo4j://localhost:7687") // GraphRAG 图谱(连不上则降级)
|
||
neo4jUser := envOr("NEO4J_USER", "neo4j")
|
||
neo4jPass := envOr("NEO4J_PASS", "sundynix")
|
||
|
||
b, err := sharedbus.Connect(natsURL)
|
||
if err != nil {
|
||
log.Fatalf("[mcp_go] nats connect: %v", err)
|
||
}
|
||
defer b.Close()
|
||
log.Printf("[mcp_go] connected %s", natsURL)
|
||
|
||
// 报告源/产物落对象存储(与 gateway 同一 MinIO/bucket),跨进程/多机共享,
|
||
// 替代此前 SUNDYNIX_REPORTS_DIR 本地盘耦合;连不上则回退本地盘(单机降级)。
|
||
blobStore := blob.Open(
|
||
envOr("MINIO_ENDPOINT", "localhost:9000"),
|
||
envOr("MINIO_ACCESS_KEY", "minioadmin"),
|
||
envOr("MINIO_SECRET_KEY", "minioadmin"),
|
||
envOr("MINIO_BUCKET", "sundynix-docs"),
|
||
)
|
||
|
||
mem := memory.Open(pgDSN) // 偏好记忆:sundynix_user_profile(连不上则降级)
|
||
defer mem.Close()
|
||
hist := history.Open(redisAddr) // 会话短期历史:Redis(连不上则降级)
|
||
defer hist.Close()
|
||
|
||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||
defer stop()
|
||
|
||
// HTTP 健康探针:给 k8s/LB 直接探(此前只有 NATS 应答,编排器够不着)。
|
||
// readiness = NATS 连接可用(能收工具调用);liveness = 进程能应答。
|
||
healthShutdown := health.Serve("mcp-go", envOr("MCP_GO_HEALTH_ADDR", ":8092"), b.IsConnected)
|
||
defer func() {
|
||
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
defer scancel()
|
||
healthShutdown(sctx)
|
||
}()
|
||
|
||
// RAG 核心链:embedding + Milvus(向量) + Bleve(全文) + Neo4j(图谱) + 可选 rerank
|
||
ragEngine := rag.Open(ctx, rag.Config{
|
||
MilvusAddr: milvusAddr,
|
||
EmbedBase: embBase, EmbedKey: embKey, EmbedModel: embModel,
|
||
RerankBase: rerankBase, RerankKey: rerankKey, RerankModel: rerankModel,
|
||
Neo4jURI: neo4jURI, Neo4jUser: neo4jUser, Neo4jPass: neo4jPass,
|
||
})
|
||
defer ragEngine.Close()
|
||
|
||
// 配置控制面:取激活 embedding(向量) + chat(图谱抽取) 配置并订阅热更新。
|
||
applyEmbed := func(cfg *contract.ModelConfig) {
|
||
if cfg != nil {
|
||
ragEngine.SetEmbedding(cfg.BaseURL, cfg.APIKey, cfg.Model)
|
||
}
|
||
}
|
||
applyChat := func(cfg *contract.ModelConfig) {
|
||
if cfg != nil {
|
||
ragEngine.SetChat(cfg.BaseURL, cfg.APIKey, cfg.Model)
|
||
}
|
||
}
|
||
// 先订阅热更新(控制台改配置即生效)。
|
||
if _, err := b.SubscribeConfigUpdated(contract.ConfigKindChat, applyChat); err != nil {
|
||
log.Printf("[mcp_go] subscribe chat config: %v", err)
|
||
}
|
||
if _, err := b.SubscribeConfigUpdated(contract.ConfigKindEmbedding, applyEmbed); err != nil {
|
||
log.Printf("[mcp_go] subscribe embedding config: %v", err)
|
||
}
|
||
// 后台重试拉初始配置:容忍 gateway 晚于本服务启动(避免一次性扑空致 RAG 长期降级)。
|
||
go b.RequestConfigWithRetry(ctx, contract.ConfigKindEmbedding, applyEmbed)
|
||
go b.RequestConfigWithRetry(ctx, contract.ConfigKindChat, applyChat)
|
||
|
||
// Prompt 控制面:拉取激活集覆盖内置默认 + 订阅热更新(管理端激活某版即生效,不重启)。
|
||
go b.RequestPromptsWithRetry(ctx, prompts.ApplyOverrides)
|
||
if _, err := b.SubscribePromptsUpdated(prompts.ApplyOverrides); err != nil {
|
||
log.Printf("[mcp_go] subscribe prompts: %v", err)
|
||
}
|
||
|
||
gw := mcp.NewGateway(b, mem, hist, ragEngine, blobStore, pgDSN)
|
||
|
||
log.Println("[mcp_go] serving MCP over sundynix.tools.go.* (Ctrl-C to quit)")
|
||
if err := gw.Serve(ctx); err != nil && err != context.Canceled {
|
||
log.Fatalf("[mcp_go] exit: %v", err)
|
||
}
|
||
}
|
||
|
||
func envOr(key, def string) string {
|
||
if v := os.Getenv(key); v != "" {
|
||
return v
|
||
}
|
||
return def
|
||
}
|