ADK-gateway/README.md
2026-08-04 17:20:08 +08:00

139 lines
4.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# A2A 智能网关gateway
基于设计文档《A2A需求分析-网关-K8s全链路架构详细设计方案》实现的智能网关核心服务。
本期范围:**网关核心 + 可视化运维后台**。Agent/CLI 为预留接口K8s 算力层不实际部署。
## 技术栈
- 后端Python + FastAPI + Uvicorn + redis-pyasyncio
- 存储Redis任务池 / Agent 池 / 调度索引 / 心跳 ZSet / 审计日志)
- 前端Vue 3 + Vite + Element Plus完整运维后台
- 后台任务asyncio 循环(心跳扫描 / 调度 / 超时检查),配 Redis 分布式锁防多实例重复执行
## 目录结构
```
gateway/
├── docker-compose.yml # 本地一键启动 Redis
├── requirements.txt # Python 依赖
├── .env.example # 环境变量示例
├── backend/
│ ├── app/
│ │ ├── main.py # FastAPI 入口
│ │ ├── config.py # 配置
│ │ ├── constants.py # Redis 键名/TTL/状态常量
│ │ ├── models/schemas.py # Pydantic 模型
│ │ ├── repository/ # Redis 存储层task/agent/log
│ │ ├── services/ # 业务服务(任务/Agent/调度/心跳/通信/超时/锁)
│ │ ├── api/ # 路由cli/agent/admin
│ │ ├── middleware.py # 日志中间件
│ │ └── scheduler_loop.py # 后台任务循环
│ └── tests/ # 单元与集成测试
└── frontend/ # Vue3 + Element Plus 运维后台
```
## 快速启动
### 1. 启动 Redis
```bash
docker compose up -d
```
无 Docker 时需本机提供 Redis 实例,并设置 `REDIS_URL`
### 2. 启动后端
```bash
cd backend
pip install -r ../requirements.txt
# 复制 .env.example 为 .env 并按需修改
uvicorn app.main:app --host 0.0.0.0 --port 8000
```
接口文档Swaggerhttp://localhost:8000/docs
### 3. 启动前端运维后台
```bash
cd frontend
# 使用 Node >= 18推荐 18/20/22
npm install
npm run dev
```
访问 http://localhost:5173 Vite 已将 `/api` 代理到后端 `:8000`
### 4. 运行后端测试
```bash
cd backend
python -m pytest -q
```
## 核心机制
### 任务池Redis
- `task:info:{request_id}`任务全量信息Hash
- `task:pending` / `task:running`调度状态索引Set
- 状态流转pending → running → success/failed
- TTL 默认 24h 自动归档RequestID 幂等去重
### Agent 池Redis
- `agent:info:{agent_id}`Agent 信息Hash
- `agent:heartbeat`心跳时间戳ZSetscore=最后心跳)
- `agent:tag:{tag}`能力标签索引Set
- 心跳保活:网关每 60s 扫描,连续 120s 未心跳标记 offline
### 规则调度
标签精准匹配 → 负载过滤(当前负载 < 并发上限)→ 最低负载停留时间久者优先)→ 绑定 Agent 并下发
### 认证
CLI 提交任务与 Agent 注册时请求体必须携带 `auth` 字段值须与网关配置的 `GATEWAY_AUTH``.env` 中设置一致否则返回 `401`示例
```bash
curl -X POST http://localhost:8000/api/cli/tasks \
-H "Content-Type: application/json" \
-d '{"auth": "<GATEWAY_AUTH>", "task_type": "compile", "task_tags": ["build"], "payload": {"cmd": "python -m build"}}'
```
### 预留 Agent 协议接口
> 所有接口中 `register` 必须携带 `auth` 字段,其余接口以已注册的 `agent_id` 关联身份。
| 接口 | 说明 |
| --- | --- |
| `POST /api/agent/register` | Agent 启动注册`auth` + 能力标签并发上限地址 |
| `POST /api/agent/unregister` | 优雅注销 |
| `POST /api/agent/heartbeat` | 心跳约每 10s同步负载 |
| `POST /api/agent/result` | 任务结果回传 |
### 通信中转
`RequestID + AgentID` 双维度关联 CLI 会话与 Agent正向下发任务指令反向回传进度与结果本期为状态机闭环 + 日志记录实际网络下发由 Agent 接入时扩展
## 运维后台
- 任务管理列表 / 筛选 / 详情 / 进度 / 取消
- Agent 管理卡片网格 / 标签 / 负载 / 心跳 / 离线高亮
- 日志审计全链路时间线 RequestID / AgentID 筛选
- 手动管控取消任务 / 重置任务 / 下线 Agent
## 环境变量
`.env.example`关键参数
| 变量 | 默认 | 说明 |
| --- | --- | --- |
| `REDIS_URL` | `redis://localhost:6379/0` | Redis 连接 |
| `HEARTBEAT_SCAN_INTERVAL` | `60` | 心跳扫描间隔 |
| `AGENT_HEARTBEAT_TIMEOUT` | `120` | 心跳超时剔除阈值 |
| `DISPATCH_INTERVAL` | `2` | 调度循环间隔 |
| `TASK_TIMEOUT_CHECK_INTERVAL` | `5` | 任务超时检查间隔 |
| `DEFAULT_TASK_TIMEOUT` | `3600` | 任务默认超时 |
| `TASK_TTL` | `86400` | 任务数据保留 TTL |