Skip to content
广告 · 本站推荐广告

桥接协议

桥接协议(Bridge Protocol)允许多个 Gateway 实例之间建立通信通道,实现跨网关的模型共享、会话转移和负载分担。

概述

text
┌──────────────┐    Bridge Protocol    ┌──────────────┐
│  Gateway A   │ ◀──────────────────▶ │  Gateway B   │
│  (办公室)     │    双向通信           │  (数据中心)   │
│              │                      │              │
│  Channel:    │                      │  Channel:    │
│  - OpenAI    │                      │  - Ollama    │
│  - Anthropic │                      │  - vLLM      │
└──────────────┘                      └──────────────┘

使用场景

桥接协议适用于需要多个 Gateway 协同工作的场景,例如:将本地 GPU 服务器的模型能力共享给远程 Gateway。

核心概念

桥接角色

角色说明
Initiator(发起者)主动建立桥接连接的 Gateway
Responder(响应者)接受桥接连接的 Gateway
Peer(对等节点)桥接建立后,双方成为对等节点

桥接能力

  • 模型共享:一个 Gateway 的 Channel 对另一个 Gateway 可见
  • 会话转移:将活跃会话从一个 Gateway 迁移到另一个
  • 负载分担:请求自动路由到负载较轻的 Gateway
  • 故障转移:某个 Gateway 离线时自动切换

协议规范

握手(Handshake)

桥接连接通过 WebSocket 建立,握手消息格式:

json
{
  "type": "bridge-hello",
  "gatewayId": "gw_abc123",
  "gatewayName": "Office Gateway",
  "version": "1.0",
  "token": "bridge-secret-token",
  "capabilities": {
    "shareChannels": true,
    "acceptSessions": true,
    "loadBalance": false
  }
}

握手确认

json
{
  "type": "bridge-hello-ok",
  "gatewayId": "gw_def456",
  "gatewayName": "Datacenter Gateway",
  "sharedChannels": [
    {
      "name": "local-llama",
      "provider": "ollama",
      "models": ["llama3", "codellama"]
    }
  ]
}

请求转发

json
{
  "type": "bridge-forward",
  "requestId": "req_789",
  "targetChannel": "local-llama",
  "payload": {
    "model": "llama3",
    "messages": [
      {"role": "user", "content": "Hello"}
    ]
  }
}

响应回传

json
{
  "type": "bridge-response",
  "requestId": "req_789",
  "payload": {
    "model": "llama3",
    "choices": [
      {"message": {"role": "assistant", "content": "Hi there!"}}
    ]
  }
}

配置桥接

发起桥接

json5
{
  "gateway": {
    "bridges": [
      {
        "name": "datacenter",
        "url": "wss://datacenter-gateway.example.com/bridge",
        "token": "${BRIDGE_TOKEN}",
        "shareChannels": true,
        "autoReconnect": true,
        "reconnectInterval": 5000
      }
    ]
  }
}

接受桥接

json5
{
  "gateway": {
    "bridge": {
      "enabled": true,
      "allowedTokens": ["${BRIDGE_TOKEN}"],
      "shareChannels": ["local-llama", "local-vllm"],
      "maxPeers": 5
    }
  }
}

CLI 管理

bash
# 查看桥接状态
openclaw bridge list

# 手动建立桥接
openclaw bridge connect --url wss://remote-gw.example.com/bridge --token xxx

# 断开桥接
openclaw bridge disconnect datacenter

# 查看共享的 Channel
openclaw bridge channels

使用场景

场景 1:共享本地 GPU 模型

text
MacBook (Gateway A)          GPU Server (Gateway B)
┌──────────────┐             ┌──────────────┐
│  客户端连接   │   Bridge    │  Ollama      │
│  OpenAI API  │ ◀─────────▶ │  llama3      │
│              │             │  codellama   │
└──────────────┘             └──────────────┘

场景 2:地理分布冗余

text
Gateway (北京)               Gateway (上海)
┌──────────────┐   Bridge    ┌──────────────┐
│  Channel A   │ ◀─────────▶ │  Channel B   │
│  用户 1~50   │   故障转移   │  用户 51~100 │
└──────────────┘             └──────────────┘

延迟考虑

跨地域桥接会引入网络延迟。流式(Streaming)响应可以缓解用户感知的延迟。

安全

  • 桥接连接使用独立的 Token 认证
  • 支持 TLS 加密传输
  • 可限制共享的 Channel 列表
  • 可限制最大对等节点数

最小共享

只共享必要的 Channel,避免过度暴露。使用 shareChannels 白名单明确指定可共享的 Channel。

相关文档

基于MIT协议开源 | 内容翻译自 官方文档,同步更新