路由与投递策略
目标:同一组渠道换六种策略各发一次,看懂每次的
deliveries形态;再补齐去重、免打扰、延迟与上线前预演。前置:服务已启动,先设好BASE与TOKEN(见发出第一条通知第 1 步),渠道至少三个可用(见接入一个渠道)。本页命令都在同一目录执行,路由 JSON 先写文件再-d @file。
0. 准备渠道集合
curl -sS -X POST "$BASE/v1/admin/channels" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"id":"ch-hook-v2","provider_id":"webhook","name":"新版本接收端","config":{"endpoint":"http://127.0.0.1:3001/hook","method":"POST"}}'
curl -sS -X POST "$BASE/v1/admin/channels" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"id":"ch-sms","provider_id":"sms-generic","name":"短信(演练用)","config":{"endpoint":"http://127.0.0.1:9/sms","app_key":"demo","app_secret":"demo"}}'ch-sms 故意指向不可达地址,用来演示 failover 的「前一跳彻底失败」。策略里引用的渠道必须在路由的 channel_ids 里,否则创建时 400 策略引用了未绑定的渠道 …——这条护栏防的是拼写错误静默丢通知。改策略用 PUT /v1/admin/routes/{id} 覆盖同一条路由即可,version 每次 +1。
1. fanout:全部发一遍
cat > r-fanout.json <<'JSON'
{ "id": "r-fanout", "name": "全发", "channel_ids": ["ch-hook", "ch-ding", "ch-mail"],
"strategy": { "type": "fanout" } }
JSON
curl -sS -X POST "$BASE/v1/admin/routes" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-fanout.json
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"route_id":"r-fanout","payload":{"title":"全发"}}'预期 deliveries:3 行各自独立推进;全成消息 sent,部分成 partially_sent,全败 failed。
2. failover:前序彻底失败才换下一跳
cat > r-fail.json <<'JSON'
{ "id": "r-fail", "name": "短信→钉钉", "channel_ids": ["ch-sms", "ch-ding"],
"strategy": { "type": "failover", "order": ["ch-sms", "ch-ding"] } }
JSON
curl -sS -X POST "$BASE/v1/admin/routes" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-fail.json
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"route_id":"r-fail","payload":{"title":"磁盘 92%"}}'order 缺省按 channel_ids 顺序,且必须是它的子集;写反了就是先发钉钉、短信永远不动。没有真实钉钉时把两处都换成 ["ch-sms","ch-hook"],顺序逻辑一样。
预期 deliveries:两行都在——第一行 pending(seq 0),第二行 waiting(未激活)。第一行 dead 后第二行转 pending;任一跳成功,其余 waiting 变 suppressed,消息 sent。「彻底失败」= 不可重试失败或 尝试耗尽(缺省 5 次),不是首次失败就换。
3. weighted:按权重分摊
cat > r-weight.json <<'JSON'
{ "id": "r-weight", "name": "加权分摊", "channel_ids": ["ch-hook", "ch-ding", "ch-mail"],
"strategy": { "type": "weighted", "weights": { "ch-hook": 70, "ch-ding": 20, "ch-mail": 10 } } }
JSON
curl -sS -X POST "$BASE/v1/admin/routes" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-weight.json
for i in 1 2 3 4 5; do
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d "{\"route_id\":\"r-weight\",\"payload\":{\"title\":\"加权 $i\"}}"
done预期 deliveries:每条消息恰好 1 行。发 5 条大概是「3~4 条走 ch-hook」——权重是概率不是配额。被选中的渠道不可用就失败,不会静默换路。
4. canary:10% 灰度
cat > r-canary.json <<'JSON'
{ "id": "r-canary", "name": "灰度 10%", "channel_ids": ["ch-hook", "ch-hook-v2"],
"strategy": { "type": "canary", "percent": 10, "primary": "ch-hook", "canary": ["ch-hook-v2"] } }
JSON
curl -sS -X POST "$BASE/v1/admin/routes" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-canary.json
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"route_id":"r-canary","payload":{"title":"灰度"}}'percent 取 0~100;canary 可以是多个渠道,命中时列表里全部收到。想「按租户灰度」而不是按流量,用第 6 节的 conditional。
预期 deliveries:1 行,要么 ch-hook 要么 ch-hook-v2。单次看不出 10%,连发几十条才看得出比例。
5. quorum:4 个里 3 个成功即可
cat > r-quorum.json <<'JSON'
{ "id": "r-quorum", "name": "四选三到达", "channel_ids": ["ch-hook", "ch-hook-v2", "ch-mail", "ch-sms"],
"strategy": { "type": "quorum", "k": 3 } }
JSON
curl -sS -X POST "$BASE/v1/admin/routes" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-quorum.json
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"route_id":"r-quorum","payload":{"title":"强提醒"}}'k 缺省等于渠道总数,越小越容易判成功。把 ch-mail 换成任何你能成功的渠道都能验证。
预期 deliveries:4 行先全 pending;成功数到 3 时剩余行被置 suppressed,消息 sent。suppressed 是策略性收手,不算失败。
6. conditional:按优先级与字段选渠道
cat > r-cond.json <<'JSON'
{ "id": "r-cond", "name": "按级别分流", "channel_ids": ["ch-sms", "ch-ding", "ch-mail"],
"strategy": {
"type": "conditional",
"cases": [
{ "when": "priority == 'urgent' || payload.severity == 'P1'", "channels": ["ch-sms", "ch-ding"] },
{ "when": "payload.severity == 'P2'", "channels": ["ch-ding"] }
],
"default_channels": ["ch-mail"] } }
JSON
curl -sS -X POST "$BASE/v1/admin/routes" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-cond.json
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"route_id":"r-cond","priority":"urgent","payload":{"title":"P1 告警","severity":"P1"}}'cases 按序求值,首个为真者生效,全假走 default_channels;表达式环境是 payload / variables / priority / target。
预期 deliveries:只出现被选中的渠道,本例 2 行(ch-sms、ch-ding)。把 priority 与 severity 都去掉再发,只有 1 行走 ch-mail。
7. 横切能力:去重、免打扰、延迟
以下几项与策略类型正交,可以叠加在任意策略上。
7.1 5 分钟内同告警只发一次
cat > r-fanout.json <<'JSON'
{ "id": "r-fanout", "name": "全发(带去重)", "channel_ids": ["ch-hook", "ch-ding"],
"strategy": { "type": "fanout", "dedup_window": "5m", "dedup_key_expr": "payload.alert_id" } }
JSON
curl -sS -X PUT "$BASE/v1/admin/routes/r-fanout" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-fanout.json
for i in 1 2; do
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"route_id\":\"r-fanout\",\"payload\":{\"title\":\"磁盘告警\",\"alert_id\":\"disk-92\"},\"idempotency_key\":\"d-$i\"}"
done第二次返回 200 {"message_id":"<第一次的 ID>","created":false,"deduped":true} 且不产生新投递。缺省去重键是 route_id——不写 dedup_key_expr 就退化成「同路由 5 分钟只发一条」,通常不是你想要的。幂等键管「同一请求重发」,去重窗口管「不同请求但同一告警」,两者独立。
7.2 免打扰:22:00–08:00 挂起
cat > r-fanout.json <<'JSON'
{ "id": "r-fanout", "name": "全发(夜间挂起)", "channel_ids": ["ch-hook", "ch-ding"],
"strategy": { "type": "fanout",
"quiet_hours": { "start": "22:00", "end": "08:00", "behavior": "hold", "tz": "Asia/Shanghai" } } }
JSON
curl -sS -X PUT "$BASE/v1/admin/routes/r-fanout" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d @r-fanout.json
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"route_id":"r-fanout","payload":{"title":"夜间告警"}}'behavior 目前仅 hold:窗口内到达的投递挂起到窗口结束再发,可跨午夜(22:00–08:00 就是常态),tz 缺省服务器时区。
预期:消息 queued,每行 pending 且 next_attempt_at 是次日 08:00,到点自动放行。
7.3 delay / schedule_at / 快照
curl -sS -X PUT "$BASE/v1/admin/routes/r-fanout" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"全发(延迟 30 秒)","channel_ids":["ch-hook"],"strategy":{"type":"fanout","delay":"30s"}}'
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"route_id":"r-fanout","schedule_at":"2026-12-01T09:00:00+08:00","payload":{"title":"定时"}}'策略 delay、quiet_hours、请求级 schedule_at 三者取最晚者作为实际投递时刻;上面这条会在 12 月 1 日 09:00 发,因为 schedule_at 比「现在 + 30 秒」晚。ttl 是独立的一条线:到期还没投出去就按失败落账。
另外,消息入队时会把策略、渠道配置、工作流 DSL 一起固化。刚才用 PUT 改完策略,之前入队的那条夜间告警仍按旧策略在 08:00 发出——version 变了不等于在途消息变了。以为「改了没用」时,先想想快照。
8. 上线前先看会打给谁
curl -sS -X POST "$BASE/v1/notify" -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"route_id":"r-cond","priority":"urgent","dry_run":true,"payload":{"severity":"P1"}}'返回 200 与 {"dry_run":true,"preview":{…}}:preview 里有 route_id、strategy_type、workflow、channels[](每项 id / provider_id / target),按需附带 scheduled_at 与 notes(去重窗口、免打扰挂起、定时、TTL 提示)。dry_run 与真实入队走同一套校验与展开,但零写入:不落库、不占幂等键、不占去重键。weighted / canary 有随机性,预览只代表这一次抽样。
两个 dry_run 不是一回事
POST /v1/notify {"dry_run":true} 预演策略路由;POST /v1/admin/routes/{id}/dry-run 只服务工作流路由(普通路由会 400)。工作流侧用法见写出第一个工作流。
你应该看到
| 策略 | deliveries 行数 | 终态特征 |
|---|---|---|
fanout | 全部渠道 | 互不影响,聚合出 sent / partially_sent / failed |
failover | 全部渠道 | 只有 seq 0 在跑,成功后其余 suppressed |
weighted / canary | 1(canary 列表多个时更多) | 单条消息命中一个或一组渠道 |
quorum | 全部渠道 | 达标后剩余行 suppressed,消息 sent |
conditional | 仅被选中渠道 | 未命中的渠道不产生投递行 |
如果卡住了
- 建路由
400 策略引用了未绑定的渠道 x:策略里出现的每个 ID 都要在channel_ids里。 failover永远只发第一跳:第二跳要等第一跳「彻底失败」(不可重试或 5 次耗尽)。conditional建路由400:表达式编译失败,检查引号与payload下真实存在的键名。- 改完策略新消息行为没变:确认
PUT的是同一条路由且version+1;在途消息按快照走,属预期。
更多排障路径见排障:干跑、运行记录与死信。