yarp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

YARP 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
MapReverseProxy()
which must be called last.
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

核心概念

ConceptUsageExample
RouteMaps external path to cluster
"Path": "/api/blueprint/{**catch-all}"
ClusterBackend service destination(s)
"Address": "http://service:8080"
TransformRewrites path before forwarding
"PathPattern": "/api/{**catch-all}"
Catch-allCaptures remaining path segments
{**catch-all}
or
{*any}
概念用途示例
路由将外部路径映射到集群
"Path": "/api/blueprint/{**catch-all}"
集群后端服务目标地址
"Address": "http://service:8080"
转换转发前重写路径
"PathPattern": "/api/{**catch-all}"
通配捕获捕获剩余路径段
{**catch-all}
{*any}

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:
GET /api/blueprint/blueprints
→ Backend:
GET /api/blueprints
场景: 外部API包含服务前缀,但后端服务不包含
json
{
  "Match": { "Path": "/api/blueprint/{**catch-all}" },
  "Transforms": [{ "PathPattern": "/api/{**catch-all}" }]
}
请求:
GET /api/blueprint/blueprints
→ 后端:
GET /api/blueprints

Health 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:
  1. Use
    mcp__context7__resolve-library-id
    to search for "yarp"
  2. Query with
    mcp__context7__query-docs
    using the resolved library ID
Library ID:
/dotnet/yarp
Recommended Queries:
  • "YARP configuration routes clusters transforms"
  • "YARP load balancing health checks"
  • "YARP request transforms path rewriting"
使用Context7获取最新YARP文档。
如何使用Context7:
  1. 使用
    mcp__context7__resolve-library-id
    搜索"yarp"
  2. 使用解析后的库ID,通过
    mcp__context7__query-docs
    进行查询
库ID:
/dotnet/yarp
推荐查询:
  • "YARP configuration routes clusters transforms"
  • "YARP load balancing health checks"
  • "YARP request transforms path rewriting"