Skip to content

Conversation

@missish
Copy link

@missish missish commented Jan 27, 2026

功能概述

为 v2node 新增三种负载均衡路由 Action,支持将流量分发到多个出站节点,实现高可用和负载均衡。

新增 Action

Action 说明 匹配方式
balancer 域名负载均衡 匹配指定域名
balancer_ip IP 负载均衡 匹配指定 IP/CIDR
default_balancer 默认负载均衡 匹配所有 TCP/UDP 流量

支持的负载均衡策略

策略 说明 是否需要 Observatory
random 随机选择(默认)
roundrobin 轮询
leastping 最低延迟
leastload 最低负载

配置格式

action_value 结构:

{
  "tag": "proxy-lb",
  "outbounds": [
    {"tag": "proxy-hk-1", "protocol": "vmess", "settings": {...}, "streamSettings": {...}},
    {"tag": "proxy-hk-2", "protocol": "trojan", "settings": {...}, "streamSettings": {...}}
  ],
  "strategy": "leastping",
  "fallbackTag": "direct"
}

字段说明:

字段 类型 必填 说明
tag string 负载均衡器标识,需唯一
outbounds array 出站配置数组,格式同 xray-core outbound
strategy string 负载均衡策略,默认 random
fallbackTag string 回退出站 tag,所有出站不可用时使用

API 下发示例

balancer(域名匹配):

{
  "action": "balancer",
  "match": ["geosite:netflix", "domain:openai.com"],
  "action_value": "{\"tag\":\"streaming-lb\",\"outbounds\":[...],\"strategy\":\"leastping\"}"
}

balancer_ip(IP 匹配):

{
  "action": "balancer_ip",
  "match": ["geoip:us", "1.1.1.1/32"],
  "action_value": "{\"tag\":\"us-lb\",\"outbounds\":[...],\"strategy\":\"roundrobin\"}"
}

default_balancer(默认出站):

{
  "action": "default_balancer",
  "match": [],
  "action_value": "{\"tag\":\"default-lb\",\"outbounds\":[...],\"strategy\":\"random\"}"
}

变更文件

文件 变更类型 说明
core/balancer.go 新增 负载均衡配置解析和构建逻辑
core/custom.go 修改 添加三个 Action 的处理逻辑
core/core.go 修改 集成 Observatory 配置
core/distro/all/all.go 修改 启用 observatory 组件导入

技术实现

  1. 出站配置:从 outbounds 数组解析并构建 xray-core OutboundHandlerConfig
  2. Selector 提取:自动从 outbounds 中提取 tag 作为负载均衡 selector
  3. Balancer 规则:构建 coreConf.BalancingRule 并添加到 RouterConfig
  4. Observatory:使用 leastping/leastload 策略时自动创建 Observatory 配置
    • ProbeURL: https://www.gstatic.com/generate_204
    • ProbeInterval: 30 秒
    • EnableConcurrency: true

注意事项

  1. default_outdefault_balancer 不应同时配置,会产生冲突(先配置的生效)
  2. 同一节点的多个 balancer action 共享同一个 Observatory 实例
  3. 出站 tag 必须唯一,重复的 tag 会被自动跳过

新增三种负载均衡 Action:
- balancer: 基于域名匹配的负载均衡
- balancer_ip: 基于 IP 匹配的负载均衡
- default_balancer: 默认出站负载均衡(匹配所有流量)

支持的负载均衡策略:
- random: 随机选择(默认)
- roundrobin: 轮询
- leastping: 最低延迟(需要 Observatory)
- leastload: 最低负载(需要 Observatory)
@missish missish changed the title feat: 新增负载均衡路由 Action 支持 feat: 新增负载均衡路由 Action 支持 (balancer/balancer_ip/default_balancer) Jan 27, 2026
@wyx2685
Copy link
Owner

wyx2685 commented Jan 27, 2026

有一个问题,v2board上action只能写最大11字符,balancer的种类名长度要缩一下

另外需要配套v2board的PR

@missish
Copy link
Author

missish commented Jan 27, 2026

有一个问题,v2board上action只能写最大11字符,balancer的种类名长度要缩一下

另外需要配套v2board的PR

抱歉我用的Xboard自行开发的route插件无法提供v2board的PR

@wyx2685
Copy link
Owner

wyx2685 commented Jan 27, 2026

那挂在这 有需要的人自用

@missish
Copy link
Author

missish commented Jan 27, 2026

那挂在这 有需要的人自用

好吧

- 新增 getActionPriority 函数定义 action 优先级
- 新增 sortRoutesByPriority 函数按优先级排序路由规则
- 优先级顺序: dns → block/protocol → block_ip/block_port → balancer/route → balancer_ip/route_ip → default_out/default_balancer
- 使用稳定排序保持同级规则的原始配置顺序
- ProbeInterval 单位是纳秒,之前设置 30 实际是 30 纳秒
- 修改为 10 * time.Second (10秒)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants