在同一台主機運行多個 Gateway
當你需要在同一台機器上跑多個實例時,最頭痛的就是設定檔互相覆蓋,或是 Port 被佔用的問題。如果你想在跑主程式的同時,另外弄一個「救援機器人(Rescue bot)」來應對緊急狀況,開發環境的隔離就變得非常重要。
通常一個 Gateway 就能處理多個連線,但如果你追求更高的隔離性或冗餘備援,你就需要學會如何在同一個環境下跑多個 Gateway。
需要準備的東西
Section titled “需要準備的東西”要確保多個 Gateway 不會互相打架,你必須隔離以下項目:
OPENCLAW_CONFIG_PATH:每個實例獨立的設定檔OPENCLAW_STATE_DIR:每個實例獨立的 Session、憑證與快取agents.defaults.workspace:每個實例獨立的工作區路徑gateway.port(或--port):每個實例唯一的連接埠- 衍生 Port(如 Browser 或 Canvas):確保不會重疊
推薦使用 --profile 參數,它會自動幫你處理 OPENCLAW_STATE_DIR 和 OPENCLAW_CONFIG_PATH 的路徑,並在服務名稱加上後綴。
1. 啟動主實例 (Main)
Section titled “1. 啟動主實例 (Main)”# 初始化openclaw --profile main setup# 啟動 Gateway 並指定 Portopenclaw --profile main gateway --port 187892. 啟動救援實例 (Rescue)
Section titled “2. 啟動救援實例 (Rescue)”# 初始化openclaw --profile rescue setup# 啟動救援 Gateway,使用不同的 Portopenclaw --profile rescue gateway --port 190013. 安裝為系統服務
Section titled “3. 安裝為系統服務”如果你想讓它們在背景運行:
openclaw --profile main gateway installopenclaw --profile rescue gateway install救援機器人 (Rescue-bot) 指南
Section titled “救援機器人 (Rescue-bot) 指南”在同一台主機跑第二個 Gateway 的核心目的,是為了在主機器人掛掉時,你還有一個獨立的通道可以進行偵錯或修改設定。
關鍵規則: Port 間隔至少要預留 20 個。例如主實例用 18789,救援實例建議從 19789 開始,這樣自動衍生的 Browser/Canvas Port 才不會相撞。
# 主機器人 (使用預設配置)openclaw onboardopenclaw gateway install
# 救援機器人 (使用隔離的 profile 與 port)openclaw --profile rescue onboard# 注意:# - workspace 名稱預設會加上 -rescue 後綴# - Port 建議離主 Port 遠一點(例如 19789)# - 其餘 onboard 流程與一般模式相同
# 安裝救援服務openclaw --profile rescue gateway installPort 映射邏輯
Section titled “Port 映射邏輯”當你設定了 gateway.port(基礎 Port),系統會自動推算以下 Port:
- Browser Control Service: 基礎 Port + 2
- canvasHost.port: 基礎 Port + 4
- CDP Ports: 從
browser.controlPort + 9到+ 108之間自動分配
如果你在設定檔或環境變數中手動覆蓋這些值,請務必確保每個實例的數值都是唯一的。
瀏覽器與 CDP 衝突
Section titled “瀏覽器與 CDP 衝突”這是最常見的坑。請注意:
- 不要將多個實例的
browser.cdpUrl指向同一個值。 - 每個實例必須有自己的 Browser Control Port 和 CDP 範圍。
- 如果需要手動指定,請在設定檔中使用
browser.profiles.<name>.cdpPort。 - 若使用遠端 Chrome,請設定
browser.profiles.<name>.cdpUrl。
檢查運行狀態
Section titled “檢查運行狀態”你可以隨時檢查不同 Profile 的運行情況:
openclaw --profile main statusopenclaw --profile rescue statusopenclaw --profile rescue browser status如果你在設定過程中遇到問題,可以詢問 AI Setup Assistant 獲取即時幫助。
OpenClaw Expert
還是卡住了?
如果這篇文件沒有解決你的情境,直接問 OpenClaw Expert,拿到可執行步驟。