yarp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseYARP Skill
YARP 技能
YARP (Yet Another Reverse Proxy) is the .NET reverse proxy used for API gateway routing in Sorcha. The gateway routes external requests to internal microservices while handling path transformations, security headers, and CORS. Key pattern: gateway-specific endpoints execute BEFORE which must be called last.
MapReverseProxy()YARP(Yet Another Reverse Proxy)是Sorcha中用于API网关路由的.NET反向代理。该网关将外部请求路由到内部微服务,同时处理路径转换、安全标头和CORS。关键模式:网关特定端点必须在之前执行,且必须最后调用。
MapReverseProxy()MapReverseProxy()Quick Start
快速开始
Basic Setup
基础设置
csharp
// Program.cs - Add YARP with configuration-based routes
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
// CRITICAL: MapReverseProxy() must be called LAST
app.MapReverseProxy();csharp
// Program.cs - 添加基于配置路由的YARP
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
// 重要:MapReverseProxy()必须最后调用
app.MapReverseProxy();Route Configuration
路由配置
json
{
"ReverseProxy": {
"Routes": {
"blueprint-route": {
"ClusterId": "blueprint-cluster",
"Match": { "Path": "/api/blueprint/{**catch-all}" },
"Transforms": [{ "PathPattern": "/api/{**catch-all}" }]
}
},
"Clusters": {
"blueprint-cluster": {
"Destinations": {
"destination1": { "Address": "http://blueprint-service:8080" }
}
}
}
}
}json
{
"ReverseProxy": {
"Routes": {
"blueprint-route": {
"ClusterId": "blueprint-cluster",
"Match": { "Path": "/api/blueprint/{**catch-all}" },
"Transforms": [{ "PathPattern": "/api/{**catch-all}" }]
}
},
"Clusters": {
"blueprint-cluster": {
"Destinations": {
"destination1": { "Address": "http://blueprint-service:8080" }
}
}
}
}
}Key Concepts
核心概念
| Concept | Usage | Example |
|---|---|---|
| Route | Maps external path to cluster | |
| Cluster | Backend service destination(s) | |
| Transform | Rewrites path before forwarding | |
| Catch-all | Captures remaining path segments | |
| 概念 | 用途 | 示例 |
|---|---|---|
| 路由 | 将外部路径映射到集群 | |
| 集群 | 后端服务目标地址 | |
| 转换 | 转发前重写路径 | |
| 通配捕获 | 捕获剩余路径段 | |
Common Patterns
常见模式
Path Prefix Stripping
路径前缀剥离
When: External API has service prefix, backend doesn't
json
{
"Match": { "Path": "/api/blueprint/{**catch-all}" },
"Transforms": [{ "PathPattern": "/api/{**catch-all}" }]
}Request: → Backend:
GET /api/blueprint/blueprintsGET /api/blueprints场景: 外部API包含服务前缀,但后端服务不包含
json
{
"Match": { "Path": "/api/blueprint/{**catch-all}" },
"Transforms": [{ "PathPattern": "/api/{**catch-all}" }]
}请求: → 后端:
GET /api/blueprint/blueprintsGET /api/blueprintsHealth Endpoint Mapping
健康端点映射
When: Unified health checks across services
json
{
"blueprint-status-route": {
"ClusterId": "blueprint-cluster",
"Match": { "Path": "/api/blueprint/status" },
"Transforms": [{ "PathPattern": "/api/health" }]
}
}场景: 跨服务统一健康检查
json
{
"blueprint-status-route": {
"ClusterId": "blueprint-cluster",
"Match": { "Path": "/api/blueprint/status" },
"Transforms": [{ "PathPattern": "/api/health" }]
}
}X-Forwarded Headers
X-Forwarded 标头
When: Backend needs original client info
json
"Transforms": [
{ "PathPattern": "/api/{**catch-all}" },
{ "X-Forwarded": "Set" }
]场景: 后端服务需要原始客户端信息
json
"Transforms": [
{ "PathPattern": "/api/{**catch-all}" },
{ "X-Forwarded": "Set" }
]See Also
另请参阅
- patterns - Route patterns and transformations
- workflows - Setup and testing workflows
- patterns - 路由模式与转换
- workflows - 设置与测试流程
Related Skills
相关技能
- See the aspire skill for service discovery integration
- See the minimal-apis skill for gateway-specific endpoints
- See the jwt skill for authentication pass-through
- See the docker skill for container networking
- 查看 aspire 技能了解服务发现集成
- 查看 minimal-apis 技能了解网关特定端点
- 查看 jwt 技能了解身份验证传递
- 查看 docker 技能了解容器网络
Documentation Resources
文档资源
Fetch latest YARP documentation with Context7.
How to use Context7:
- Use to search for "yarp"
mcp__context7__resolve-library-id - Query with using the resolved library ID
mcp__context7__query-docs
Library ID:
/dotnet/yarpRecommended Queries:
- "YARP configuration routes clusters transforms"
- "YARP load balancing health checks"
- "YARP request transforms path rewriting"
使用Context7获取最新YARP文档。
如何使用Context7:
- 使用搜索"yarp"
mcp__context7__resolve-library-id - 使用解析后的库ID,通过进行查询
mcp__context7__query-docs
库ID:
/dotnet/yarp推荐查询:
- "YARP configuration routes clusters transforms"
- "YARP load balancing health checks"
- "YARP request transforms path rewriting"