Gateway 服务运行手册

最后更新:2025-12-09 · 翻译自 docs.openclaw.ai

它是什么

如何运行(本地)

openclaw gateway --port 18789
# 完整的调试/跟踪日志输出到标准输出:
openclaw gateway --port 18789 --verbose
# 如果端口被占用,终止占用者然后启动:
openclaw gateway --force
# 开发循环(TS 更改时自动重载):
pnpm gateway:watch

远程访问

ssh -N -L 18789:127.0.0.1:18789 user@host

多个 Gateway(同一主机)

通常不需要:一个 Gateway 可以服务多个消息通道和 Agent。仅在需要冗余或严格隔离时才使用多个 Gateway(例如:救援机器人)。

如果你隔离状态 + 配置并使用唯一端口,则支持多实例。

开发配置文件 (--dev)

快速路径:运行一个完全隔离的开发实例(配置/状态/工作区),而不影响你的主要设置。

openclaw --dev setup
openclaw --dev gateway --allow-unconfigured
# 然后定位到开发实例:
openclaw --dev status
openclaw --dev health

协议(操作员视角)

完整文档:Gateway 协议

客户端发送的强制性第一帧:

req {type:"req", id, method:"connect", params:{
  minProtocol, maxProtocol,
  client:{id, displayName?, version, platform, deviceFamily?,
          modelIdentifier?, mode, instanceId?},
  caps, auth?, locale?, userAgent?
}}

Gateway 回复:

res {type:"res", id, ok:true, payload:hello-ok}
// 或 ok:false 加上错误信息,然后关闭

握手后:

方法(初始集合)

事件

错误代码(res.error 结构)

错误使用 { code, message, details?, retryable?, retryAfterMs? } 结构。

标准代码:

保活行为

重放 / 间隙

Gateway 服务管理(CLI)

使用 Gateway CLI 进行安装/启动/停止/重启/状态查询:

openclaw gateway status
openclaw gateway install
openclaw gateway stop
openclaw gateway restart
openclaw logs --follow

注意事项:

监督(systemd 用户单元)

OpenClaw 在 Linux/WSL2 上默认安装一个 systemd 用户服务。我们推荐单用户机器使用用户服务(更简单的环境,每用户配置)。对于多用户或始终在线的服务器,使用系统服务。

[Unit]
Description=OpenClaw Gateway (profile: <profile>, v<version>)
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/openclaw gateway --port 18789
Restart=always
RestartSec=5
Environment=OPENCLAW_GATEWAY_TOKEN=<token>
WorkingDirectory=/home/youruser

[Install]
WantedBy=default.target

启用持久化(必需,以便用户服务在注销/空闲后仍然存活):

sudo loginctl enable-linger youruser

操作检查

安全保证

CLI 辅助工具