用途:YARP 是 .NET 反向代理,在 Sorcha 中承担 API 网关路由,将外部请求转发到内部微服务,同时处理路径转换、安全头与 CORS。关键模式:网关专属端点必须在 MapReverseProxy() 之前执行,MapReverseProxy() 必须最后调用。
主要能力:支持基于配置的路由/集群/转换;路径前缀剥离;健康检查端点映射;X-Forwarded 头设置以传递原始客户端信息。
使用方式:在 Program.cs 中通过 builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")); 加载配置,随后 app.MapReverseProxy(); 且必须最后调用。配置包含 Routes(ClusterId、Match.Path、Transforms.PathPattern)与 Clusters(Destinations.Address)。示例:Match.Path 为 /api/blueprint/{**catch-all},PathPattern 为 /api/{**catch-all},地址如 http://blueprint-service:8080。
概念:Route 映射外部路径到集群;Cluster 为后端服务目标;Transform 在转发前重写路径;Catch-all 捕获剩余路径段({**catch-all} 或 {*any})。
适用场景:统一健康检查、服务前缀剥离、后端需要原始客户端信息等网关场景。相关主题:服务发现集成参见 aspire 技能,网关专属端点参见 minimal-apis,认证透传参见 jwt,容器网络参见 docker。
限制与补充:资料未提供许可证、版本、性能与部署细节;需查阅最新 YARP 文档,可用 Context7:mcp__context7__resolve-library-id 搜索 "yarp",库 ID 为 /dotnet/yarp,再用 mcp__context7__query-docs 查询。