Governance Service 架构设计:流式 LLM Agent 的异步治理层

comsince
FshareIM Team本文介绍 streaming-llm-agent 多模块项目中 Governance Service 的详细架构设计。Governance Service 是异步治理层,不在对话关键路径上——Chat Service 通过 Kafka fire-and-forget 发布事件,Governance Service 异步消费,完成审计落地、成本计算、指标汇总和配额管理。
日期:2026-06-12
所属项目:streaming-llm-agent(多模块,Plan 2)
前置依赖:Chat Service(Plan 1)已完成,Kafka topics 和 Redis 配额计数器已由 Chat Service 写入
1. 定位与职责
Governance Service 是异步治理层,不在对话关键路径上。Chat Service 通过 Kafka fire-and-forget 发布事件,Governance Service 异步消费,完成审计落地、成本计算、指标汇总和配额管理。
设计原则:Governance 宕机不影响 Chat Service 对话,配额扣减通过 Redis 计数器在 Chat Service 本地完成,不走同步 HTTP 调用。
2. 项目结构
2.1 多模块位置
2.2 包结构
2.3 技术栈
| 依赖 | 用途 |
|---|---|
spring-boot-starter-web | WebMVC,Admin REST API |
spring-boot-starter-actuator | /actuator/prometheus |
spring-kafka | Kafka Consumer |
mybatis-plus-boot-starter:3.5.7 | MyBatis-Plus ORM |
mysql-connector-j | MySQL JDBC |
micrometer-registry-prometheus | Prometheus 指标 |
spring-boot-starter-data-redis | 读 Redis 配额计数、写配额策略 |
lombok | 简化代码 |
3. Kafka 消费设计
3.1 AuditEventConsumer(直联模式)
3.2 CancelEventConsumer
3.3 可靠性保障
| 机制 | 说明 |
|---|---|
| 幂等 | audit_log.request_id 唯一索引,重复消费不写两条 |
| Offset 提交 | MANUAL_IMMEDIATE,DB 写入成功后提交,保证 at-least-once |
| Dead Letter Topic | 反序列化失败发 ai-audit-dlt,不阻塞主消费 |
| DB 写入失败 | RetryTemplate 重试 3 次,超限后记录告警日志 |
4. 数据库 Schema(governance_db)
4.1 audit_log — 审计日志表
4.2 billing_record — 账单记录表
4.3 model_pricing — 模型单价表(Admin API 动态修改)
5. Admin REST API(/admin/**)
所有端点在端口 8081,无鉴权(生产环境由 API Gateway 层控制)。
5.1 审计查询
| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /admin/audit | 分页查询,支持 userId、from、to、page、size 参数 |
| GET | /admin/audit/{traceId} | 按 traceId 精确查单条,前端报错定位用 |
5.2 成本汇总
| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /admin/billing/summary | 按 userId + month(yyyy-MM)汇总成本 |
5.3 配额管理
| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /admin/quota/usage/{userId} | 读 Redis 当前 RPM 计数 + 并发槽 |
| PUT | /admin/quota/policy/{userId} | 修改用户配额上限(写 Redis Hash ai:quota:policy:{userId}) |
| DELETE | /admin/quota/session/{sessionId} | 强制清除会话历史(DEL Redis key) |
Chat Service 联动说明:
PUT /admin/quota/policy写入 Redis Hash 后,RedisQuotaService需同步支持「动态策略读取」:先查ai:quota:policy:{userId}是否存在,存在则用其中的userRpm/userConcurrent,否则回退到ChatProperties默认值。这部分改动属于 Chat Service 小幅更新,在 Governance Service 实现计划中作为独立任务包含。
5.4 模型单价管理
| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /admin/pricing | 列出所有模型单价 |
| PUT | /admin/pricing/{model} | 更新指定模型单价,立即生效 |
6. Prometheus 指标
6.1 Governance Service 暴露
| 指标名 | 类型 | 标签 | 说明 |
|---|---|---|---|
gov_cost_tokens_input_total | Counter | model, userId | 输入 Token 累计 |
gov_cost_tokens_output_total | Counter | model, userId | 输出 Token 累计 |
gov_cost_usd_total | Counter | model, userId | 费用累计(USD) |
gov_kafka_consume_lag | Gauge | topic | Kafka 消费积压量 |
gov_audit_process_duration_ms | Timer | — | 审计事件处理耗时 |
gov_dead_letter_total | Counter | topic | 进入 DLT 的消息数 |
gov_db_write_error_total | Counter | table | DB 写入失败次数 |
6.2 告警规则(prometheus/rules/governance.yml)
7. 配置参考
8. 扩展点
| 扩展场景 | 改动范围 |
|---|---|
| 多租户 | audit_log + billing_record 加 tenant_id 列,Admin API 加 /tenants/{id}/ 前缀 |
| 新增通知渠道 | MetricsExporterService 加告警方法,或在 Grafana AlertManager 配置 receiver |
| 历史对话分析 | 新增 AnalyticsService 读 audit_log,Admin API 加 /admin/analytics/** |
| 账单导出 | BillingExportService 聚合 billing_record,生成 CSV/Excel |