运行场景
源码位于 examples/scenarios/。
默认 + overlay 列表(方案 C)
active: default 使用根配置;live、drill 为 overlay 场景。
yaml
# 方案 C:scenarios.active + scenarios.items[] 显式列表
#
# active: default = 根配置(不应用 overlay);其它 id 为 overlay 场景
# Admin 下拉顺序 = 默认 + items 数组顺序
version: v1
port: 8080
rules:
- host: shop.example.com
backend:
type: service
service:
name: origin-shop.internal
port: 8080
scenarios:
active: default
items:
- id: live
label: 直播
description: 商品读接口缓存
overlay:
cache:
host: redis.internal
port: 6379
prefix: "ingress:live:"
rules:
- host: shop.example.com
backend:
cache:
enabled: true
default: bypass
paths:
- match: /api/v1/products
match_type: prefix
action: cache
ttl: 60
- match: /api/v1/order
match_type: prefix
action: bypass
- id: drill
label: 演练
description: 短 TTL 压测
overlay:
rules:
- host: shop.example.com
backend:
cache:
enabled: true
ttl: 10
default: bypass
paths:
- match: /api/v1/products
match_type: prefix
action: cache
ttl: 5可运行演示
yaml
# Runnable scenarios demo (方案 C).
#
# ingress validate -c examples/scenarios/ingress.yaml
# ingress run -c examples/scenarios/ingress.yaml
#
# Switch active scene:
# export INGRESS_SCENARIO=live # env override (runtime)
# # or edit scenarios.active + reload / Admin Console
version: v1
port: 8080
rules:
- host: shop.example.com
backend:
type: service
service:
name: origin-shop.internal
port: 8080
scenarios:
active: default
items:
- id: live
label: 直播
description: 商品读接口缓存,减轻原站 DB 压力
overlay:
cache:
host: 127.0.0.1
port: 6379
prefix: "ingress:live:"
rules:
- host: shop.example.com
backend:
cache:
enabled: true
default: bypass
paths:
- match: /api/v1/products
match_type: prefix
action: cache
ttl: 60
- match: /api/v1/order
match_type: prefix
action: bypass
- id: drill
label: 演练
description: 短 TTL 压测
overlay:
rules:
- host: shop.example.com
backend:
cache:
enabled: true
ttl: 10
default: bypass
paths:
- match: /api/v1/products
match_type: prefix
action: cache
ttl: 5电商日常 / 直播
单文件:基线为日常直连原站,live overlay 缓存商品读接口。
yaml
# E-commerce daily vs live streaming in one file (方案 C).
#
# Baseline (scenarios.active: default) = daily traffic, no overlay.
# Switch to live for high-concurrency product read caching.
#
# ingress validate -c examples/scenarios/ecommerce.yaml
# INGRESS_SCENARIO=live ingress run -c examples/scenarios/ecommerce.yaml
version: v1
port: 8080
cache:
ttl: 300
rules:
- host: shop.example.com
backend:
type: service
service:
name: origin-shop.internal
port: 8080
cache:
enabled: false
scenarios:
active: default
items:
- id: live
label: 直播
description: Redis + 商品列表/详情缓存;下单/库存不缓存
overlay:
cache:
host: 127.0.0.1
port: 6379
prefix: "ingress:live:"
rules:
- host: shop.example.com
backend:
cache:
enabled: true
default: bypass
paths:
- match: /api/v1/products
match_type: prefix
action: cache
ttl: 60
- match: /api/v1/products/
match_type: prefix
action: cache
ttl: 120
- match: /api/v1/cart
match_type: prefix
action: bypass
- match: /api/v1/order
match_type: prefix
action: bypass通配符基线 + 精确域名 overlay
基线 *.example.com;场景 sh-live 在通配符 rule 之前插入 sh.example.com,上海流量优先走 overlay。
yaml
# Baseline wildcard + scenario exact host (上海直播)
#
# active: default → only *.example.com
# active: sh-live → sh.example.com rule inserted before wildcard
#
# ingress validate -c examples/scenarios/wildcard-with-exact-overlay.yaml
version: v1
port: 8080
rules:
- host: "*.example.com"
host_type: wildcard
backend:
type: service
service:
name: default-origin.internal
port: 8080
scenarios:
active: default
items:
- id: sh-live
label: 上海直播
description: sh.example.com 精确域名 + 短 TTL 缓存
overlay:
rules:
- host: sh.example.com
backend:
type: service
service:
name: sh-origin.internal
port: 8080
cache:
enabled: true
ttl: 30
default: bypass
paths:
- match: /api/v1/products
match_type: prefix
action: cache
ttl: 15旧版独立文件
以下为引入 scenarios 前的独立配置,推荐改用 ecommerce.yaml。
yaml
# 日常场景(独立文件,legacy)。
#
# 推荐改用单文件方案 C:examples/scenarios/ecommerce.yaml
#
# 验证:
# ingress validate -c examples/scenarios/ecommerce-daily.yaml
version: v1
port: 8080
cache:
ttl: 60
rules:
- host: shop.example.com
backend:
type: service
service:
name: origin-shop.internal
port: 8080
# 日常:不启用 HTTP 响应缓存,所有请求打到原站
cache:
enabled: falseyaml
# 直播场景:高并发 + 商品读接口缓存,减轻原站 DB 压力。
#
# 与日常配置 examples/scenarios/ecommerce-daily.yaml 对比:
# - 启用 Redis 分布式缓存(多 ingress 实例共享)
# - 商品列表 / 详情走 HTTP 响应缓存(default: bypass,只缓存白名单路径)
# - 下单、库存、购物车等写路径不缓存
#
# 切换场景(运维):
# ingress run -c examples/scenarios/ecommerce-live-stream.yaml
# # 或 SIGHUP / admin POST /api/v1/reload 指向该文件
#
# 验证:
# ingress validate -c examples/scenarios/ecommerce-live-stream.yaml
version: v1
port: 8080
# 直播建议 Redis,多副本共享 httpcache 键
cache:
ttl: 300
host: 127.0.0.1
port: 6379
prefix: "ingress:live:"
rules:
- host: shop.example.com
backend:
type: service
service:
name: origin-shop.internal
port: 8080
cache:
enabled: true
ttl: 120
max_body_bytes: 2097152
skip_vary: true
default: bypass
paths:
# 商品列表(REST GET)
- match: /api/v1/products
match_type: prefix
action: cache
ttl: 60
# 商品详情 GET /api/v1/products/{id}
- match: ^/api/v1/products/[^/]+$
match_type: regex
action: cache
ttl: 120
# 若列表接口是 POST + JSON(只读查询),用 key_json 指纹
- match: /api/v1/product/list
match_type: exact
action: cache
ttl: 60
methods: [POST]
key_json: [page, page_size, category_id]
- match: /api/v1/product/detail
match_type: exact
action: cache
ttl: 120
methods: [POST]
key_json: [product_id]
# 写操作 / 实时库存 — 永不缓存
- match: /api/v1/cart
match_type: prefix
action: bypass
- match: /api/v1/order
match_type: prefix
action: bypass
- match: /api/v1/stock
match_type: prefix
action: bypass校验
bash
ingress validate -c examples/scenarios/design-option-c-list.yaml
ingress validate -c examples/scenarios/ingress.yaml
ingress validate -c examples/scenarios/ecommerce.yaml
ingress validate -c examples/scenarios/wildcard-with-exact-overlay.yaml语义与 Admin 用法见 运行场景指南。