第5章 - Gateway 网关
嗨,朋友!这一章我们来深入了解 OpenClaw 的核心——Gateway 网关。理解了 Gateway,你就理解了 OpenClaw 的"大脑"。
🤔 Gateway 是什么?
Gateway 就是 OpenClaw 的控制中心。所有消息、工具调用、会话管理都通过它来协调。你可以把它想象成一个"总调度台":
WhatsApp ─┐
Telegram ─┤
Discord ──┤──→ Gateway(控制中心)──→ AI 模型
Slack ────┤ │
Signal ───┘ ├─ 浏览器控制
├─ 文件操作
├─ Shell 执行
└─ 技能插件
Gateway 通过 WebSocket 协议通信,默认监听 ws://127.0.0.1:18789。
🚀 启动 Gateway
方式一:守护进程(推荐)
如果你在向导中选择了 --install-daemon,Gateway 已经在后台运行了:
# 查看 Gateway 状态
openclaw gateway status
# 预期输出:
# ✓ Gateway is running on 127.0.0.1:18789
方式二:前台运行
开发调试时,你可能想在前台运行以查看日志:
# 前台运行,显示详细日志
openclaw gateway --port 18789 --verbose
方式三:使用 systemctl 管理
在 Linux 上,Gateway 作为 systemd 用户服务运行:
# 查看服务状态
systemctl --user status openclaw-gateway
# 启动服务
systemctl --user start openclaw-gateway
# 停止服务
systemctl --user stop openclaw-gateway
# 重启服务
systemctl --user restart openclaw-gateway
# 查看日志
journalctl --user -u openclaw-gateway -f
提示
-f 参数表示实时跟踪日志输出,按 Ctrl+C 退出。
🖥️ Dashboard 仪表盘
Gateway 内置了一个 Web 仪表盘,方便你直观管理:
# 打开 Dashboard
openclaw dashboard
# Dashboard 地址
# http://127.0.0.1:18789/
Dashboard 提供了:
- 会话管理 - 查看和管理所有聊天会话
- 渠道状态 - 查看各聊天平台的连接状态
- 工具监控 - 查看工具的使用情况
- 日志查看 - 实时查看系统日志
注意
如果你在远程服务器上运行 Gateway,需要通过 SSH 隧道或 Tailscale 才能在本地浏览器中访问 Dashboard。直接示例:
# 在你的本地电脑上运行 SSH 隧道
ssh -L 18789:127.0.0.1:18789 你的用户名@你的服务器IP
然后在本地浏览器打开 http://127.0.0.1:18789/。
📊 Gateway 健康检查
定期检查 Gateway 的运行状况是个好习惯:
# 综合健康检查
openclaw doctor
# 检查 Gateway 状态
openclaw gateway status
openclaw doctor 会检查:
- Node.js 版本是否满足要求
- Gateway 是否正在运行
- 配置文件是否有效
- API Key 是否可用
- DM 策略是否安全
🔧 Gateway 配置详解
Gateway 的配置在 ~/.openclaw/openclaw.json 中:
{
"agent": {
"model": "anthropic/claude-opus-4-6"
},
"gateway": {
"port": 18789,
"bind": "127.0.0.1"
}
}
关键配置项
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
gateway.port | number | 18789 | 监听端口 |
gateway.bind | string | 127.0.0.1 | 绑定地址(保持本地) |
gateway.tailscale.mode | string | off | Tailscale 模式 |
agent.model | string | - | AI 模型标识 |
🔄 使用 screen/tmux 后台运行
如果你不想使用 systemd,也可以用 screen 或 tmux:
使用 screen
# 创建一个新的 screen 会话
screen -S openclaw
# 在 screen 中启动 Gateway
openclaw gateway --port 18789 --verbose
# 按 Ctrl+A 然后按 D 将 screen 放到后台
# 重新连接 screen
screen -r openclaw
使用 tmux
# 创建一个新的 tmux 会话
tmux new -s openclaw
# 在 tmux 中启动 Gateway
openclaw gateway --port 18789 --verbose
# 按 Ctrl+B 然后按 D 将 tmux 放到后台
# 重新连接 tmux
tmux attach -t openclaw
💬 发送测试消息
Gateway 运行后,试试直接通过 CLI 和 AI 对话:
# 发送简单消息
openclaw agent --message "你好!请介绍一下你自己。"
# 使用高级思考模式
openclaw agent --message "帮我分析一下 /var/log/syslog 的最近错误" --thinking high
# 发送消息到指定目标
openclaw message send --target +15555550123 --message "Hello from OpenClaw"
💪 练习题
- Gateway 的默认监听地址和端口是什么?
- 如何查看 Gateway 的实时日志?
- 如何在远程服务器上的 Gateway Dashboard 从本地浏览器访问?
答案提示
127.0.0.1:18789- 运行
journalctl --user -u openclaw-gateway -f或前台启动openclaw gateway --verbose - 使用 SSH 隧道:
ssh -L 18789:127.0.0.1:18789 user@server
下一步: 第6章 - 连接聊天渠道 →
