Gateway
约 1340 字大约 4 分钟
2026-03-02
什么是 Gateway
Gateway 是 OpenClaw 的控制平面和核心枢纽,负责统一管理 Agent 运行时与消息路由。它是连接外部世界(各种聊天平台)和内部 Agents 的桥梁。
Gateway 的核心职责
- 消息路由:接收来自各个 Channel 的消息,路由到对应的 Agent
- 会话管理:维护跨渠道的会话状态和上下文
- 工具调度:协调 Skills 和工具的调用
- 事件处理:处理系统事件和钩子触发
- 状态监控:监控系统运行状态和性能指标
启动 Gateway
基础启动
# 默认启动(端口 18789)
openclaw gateway
# 指定端口
openclaw gateway --port 8080
# 详细日志模式
openclaw gateway --verbose
# 后台运行
openclaw gateway --daemon启动参数
| 参数 | 说明 | 默认值 |
|---|---|---|
--port | Web UI 端口 | 18789 |
--host | 绑定地址 | 127.0.0.1 |
--verbose | 详细日志 | false |
--daemon | 后台运行 | false |
--config | 配置文件路径 | ~/.openclaw/config.json |
--log-level | 日志级别 | info |
完整启动示例
# 生产环境启动
openclaw gateway \
--port 18789 \
--host 0.0.0.0 \
--daemon \
--log-level warn \
--config /etc/openclaw/config.jsonGateway 架构
组件结构
Gateway
├── Router(路由器)
│ ├── Message Router - 消息路由
│ ├── Agent Router - Agent 路由
│ └── Skill Router - Skill 路由
├── Session Manager(会话管理器)
│ ├── Session Store - 会话存储
│ ├── Context Manager - 上下文管理
│ └── Memory Manager - 记忆管理
├── Channel Manager(渠道管理器)
│ ├── Channel Registry - 渠道注册
│ ├── Connection Pool - 连接池
│ └── Message Queue - 消息队列
├── Skill Manager(技能管理器)
│ ├── Skill Registry - 技能注册
│ ├── Skill Loader - 技能加载器
│ └── Skill Executor - 技能执行器
└── Monitor(监控器)
├── Health Check - 健康检查
├── Metrics - 性能指标
└── Logger - 日志记录消息流程
用户消息
↓
Channel(接收)
↓
Gateway Router(路由)
↓
Session Manager(会话管理)
↓
Agent(处理)
↓
Skill(执行)
↓
Gateway Router(返回)
↓
Channel(发送)
↓
用户收到回复配置 Gateway
基础配置
Gateway 的配置文件位于 ~/.openclaw/config.json:
{
"gateway": {
"port": 18789,
"host": "127.0.0.1",
"logLevel": "info",
"maxConnections": 100,
"timeout": 30000
}
}路由配置
配置消息路由规则:
{
"routing": {
"rules": [
{
"match": {
"channel": "telegram",
"userId": "123456"
},
"target": {
"agentId": "main"
}
},
{
"match": {
"channel": "discord",
"serverId": "789012"
},
"target": {
"agentId": "coder"
}
}
],
"default": {
"agentId": "main"
}
}
}会话配置
配置会话管理策略:
{
"session": {
"ttl": 3600000,
"maxSessions": 1000,
"persistInterval": 60000,
"storage": {
"type": "file",
"path": "~/.openclaw/sessions"
}
}
}性能配置
优化 Gateway 性能:
{
"performance": {
"workers": 4,
"queueSize": 1000,
"batchSize": 10,
"concurrency": 5,
"cache": {
"enabled": true,
"maxSize": 100,
"ttl": 300000
}
}
}监控与日志
查看运行状态
# 查看 Gateway 状态
openclaw status
# 详细状态信息
openclaw status --verbose输出示例:
Gateway Status: Running
Port: 18789
Uptime: 2h 34m 12s
Active Sessions: 15
Connected Channels: 3
- telegram: connected
- discord: connected
- feishu: connected
Active Agents: 2
- main: ready
- coder: ready
Memory Usage: 245 MB
CPU Usage: 12%查看日志
# 实时日志
openclaw logs --follow
# 查看最近 100 条日志
openclaw logs --tail 100
# 按级别过滤
openclaw logs --level error
# 按组件过滤
openclaw logs --component gateway
# 按时间范围
openclaw logs --since "2026-03-01" --until "2026-03-02"日志级别
| 级别 | 说明 | 使用场景 |
|---|---|---|
debug | 调试信息 | 开发调试 |
info | 一般信息 | 日常运行 |
warn | 警告信息 | 潜在问题 |
error | 错误信息 | 错误排查 |
fatal | 致命错误 | 严重故障 |
配置日志
{
"logging": {
"level": "info",
"format": "json",
"outputs": [
{
"type": "console",
"colorize": true
},
{
"type": "file",
"path": "~/.openclaw/logs/gateway.log",
"maxSize": "100m",
"maxFiles": 10,
"compress": true
}
]
}
}守护进程管理
安装守护进程
# 安装系统服务
openclaw daemon install
# 启动守护进程
openclaw daemon start
# 停止守护进程
openclaw daemon stop
# 重启守护进程
openclaw daemon restart
# 查看守护进程状态
openclaw daemon status
# 卸载守护进程
openclaw daemon uninstallsystemd 配置(Linux)
手动创建 systemd 服务:
sudo nano /etc/systemd/system/openclaw.service添加以下内容:
[Unit]
Description=OpenClaw Gateway
After=network.target
[Service]
Type=simple
User=your-username
WorkingDirectory=/home/your-username
ExecStart=/usr/bin/openclaw gateway --daemon
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target启用并启动服务:
sudo systemctl enable openclaw
sudo systemctl start openclaw
sudo systemctl status openclawlaunchd 配置(macOS)
创建 plist 文件:
nano ~/Library/LaunchAgents/ai.openclaw.gateway.plist添加以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ai.openclaw.gateway</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/openclaw</string>
<string>gateway</string>
<string>--daemon</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/openclaw.log</string>
<key>StandardErrorPath</key>
<string>/tmp/openclaw.error.log</string>
</dict>
</plist>加载服务:
launchctl load ~/Library/LaunchAgents/ai.openclaw.gateway.plist
launchctl start ai.openclaw.gateway性能优化
调整工作进程数
# 根据 CPU 核心数调整
openclaw gateway --workers 4启用缓存
{
"cache": {
"enabled": true,
"type": "redis",
"host": "localhost",
"port": 6379,
"ttl": 300
}
}消息队列优化
{
"queue": {
"type": "redis",
"maxSize": 10000,
"batchSize": 50,
"flushInterval": 1000
}
}连接池配置
{
"connectionPool": {
"min": 5,
"max": 50,
"idleTimeout": 30000,
"acquireTimeout": 10000
}
}安全配置
访问控制
{
"security": {
"accessToken": "your-secret-token",
"allowedIPs": [
"127.0.0.1",
"192.168.1.0/24"
],
"rateLimit": {
"enabled": true,
"maxRequests": 100,
"windowMs": 60000
}
}
}HTTPS 配置
{
"https": {
"enabled": true,
"cert": "/path/to/cert.pem",
"key": "/path/to/key.pem",
"port": 443
}
}认证配置
{
"auth": {
"type": "jwt",
"secret": "your-jwt-secret",
"expiresIn": "24h"
}
}故障排查
Gateway 无法启动
# 检查端口占用
netstat -an | grep 18789
# 检查配置文件
openclaw config validate
# 查看详细错误
openclaw gateway --verbose消息路由失败
# 检查路由规则
openclaw routing list
# 测试路由
openclaw routing test --channel telegram --userId 123456
# 查看路由日志
openclaw logs --component router性能问题
# 查看性能指标
openclaw metrics
# 分析慢查询
openclaw logs --slow
# 检查资源使用
openclaw status --resources高可用部署
多实例部署
# 实例 1
openclaw gateway --port 18789 --instance 1
# 实例 2
openclaw gateway --port 18790 --instance 2负载均衡
使用 Nginx 进行负载均衡:
upstream openclaw {
server 127.0.0.1:18789;
server 127.0.0.1:18790;
}
server {
listen 80;
server_name openclaw.example.com;
location / {
proxy_pass http://openclaw;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}