更新日期: 2026-03-04
一个进程运行 Agent Core + Gateway。
# 启动
python main.py
# 配置
# config/gateway.yaml
gateway:
enabled: true
port: 8000
heartbeat_timeout: 60
适用场景:
两个独立进程。
# 终端 1:启动 Agent Core
python api_server.py
# 终端 2:启动 Gateway
python gateway_server.py
配置:
# config/gateway.yaml
gateway:
port: 8000
agent_api: http://localhost:8001 # Agent Core API
heartbeat_timeout: 60
适用场景:
三个独立进程。
# 终端 1:Agent Core
python api_server.py
# 终端 2:Gateway Core
python gateway_server.py
# 终端 3:Enterprise Gateway
python enterprise_gateway.py
适用场景:
# Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "gateway_server.py"]
# 构建
docker build -t a2a-gateway .
# 运行
docker run -p 8000:8000 a2a-gateway
# docker-compose.yml
version: '3.8'
services:
agent-core:
build: .
command: python api_server.py
ports:
- "8001:8001"
volumes:
- ./.trace:/app/.trace
gateway:
build: .
command: python gateway_server.py
ports:
- "8000:8000"
environment:
- AGENT_API=http://agent-core:8001
depends_on:
- agent-core
# 启动
docker-compose up -d
# 查看日志
docker-compose logs -f gateway
# config/gateway.yaml
gateway:
# 服务配置
host: 0.0.0.0
port: 8000
# 注册表配置
heartbeat_timeout: 60 # 心跳超时(秒)
cleanup_interval: 30 # 清理间隔(秒)
# Agent Core API
agent_api: http://localhost:8001
# config/gateway.yaml
gateway:
# ... 基础配置
# Enterprise 功能
enterprise:
enabled: true
# 认证
auth:
provider: feishu_oauth
feishu:
app_id: xxx
app_secret: xxx
# 审计
audit:
enabled: true
log_file: /var/log/gateway/audit.jsonl
# 成本管理
cost:
enabled: true
user_daily_limit: 1000 # Token 限额
# 检查 Gateway 状态
curl http://localhost:8000/health
# 返回
{
"status": "ok",
"agents_online": 5,
"uptime": 3600
}
# 暴露 Prometheus 指标
from prometheus_client import Counter, Gauge
agents_online = Gauge('gateway_agents_online', 'Number of online agents')
messages_sent = Counter('gateway_messages_sent', 'Total messages sent')
# 配置日志
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('/var/log/gateway/gateway.log'),
logging.StreamHandler()
]
)
部署多个 Gateway 实例:
# 实例 1
python gateway_server.py --port 8000
# 实例 2
python gateway_server.py --port 8001
# 实例 3
python gateway_server.py --port 8002
使用负载均衡器:
# nginx.conf
upstream gateway {
server localhost:8000;
server localhost:8001;
server localhost:8002;
}
server {
listen 80;
location /gateway/ {
proxy_pass http://gateway;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
使用 Consul 进行服务发现:
# 注册到 Consul
import consul
c = consul.Consul()
c.agent.service.register(
name='gateway',
service_id='gateway-1',
address='localhost',
port=8000,
check=consul.Check.http('http://localhost:8000/health', '10s')
)
症状:PC Agent 连接失败
排查:
curl http://localhost:8000/healthtelnet localhost 8000症状:send_to_agent 返回错误
排查:
GET /gateway/status/{agent_uri}症状:Agent 频繁离线
排查:
使用 SSL 证书:
import ssl
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain('cert.pem', 'key.pem')
uvicorn.run(app, host="0.0.0.0", port=8000, ssl=ssl_context)
添加 API Key 认证:
from fastapi import Header, HTTPException
async def verify_api_key(x_api_key: str = Header(...)):
if x_api_key not in valid_api_keys:
raise HTTPException(401, "Invalid API Key")