【译】Introducing YARP Preview 1

1 YARP

    YARP是一个项目,用于创建反向代理服务器。它开始于我们注意到来自微软内部团队的一系列问题。他们要么为其服务构建反向代理,要么询问 API 和用于构建 API 的技术。因此我们决定让他们聚在一起开发一个通用解决方案,该解决方案形成了YARP。

    YARP是一个反向代理工具包,用于使用 ASP.NET 和 .NET 中的基础设施在 .NET 中构建代理服务器。YARP 的主要区别是,它被设计为易于自定义和调整,以满足不同方案的特定需求。YARP 插入ASP.NET管道以处理传入请求,然后它拥有自己的子管道,用于执行将请求代理到后端服务器的步骤。客户可以添加其他module,或根据需要更换常备module。

    随着其开发已基本到位,我们制作了 YARP 的第一个正式版本(Preview 1),以便更好地协作并获得反馈。

2 Preview 1 是什么

    • 核心代理的基础结构
    • 基于配置的路由定义
    • 扩展性的管道模型
    • Forwarded标头(硬编码)
    • 目标 .NET Core 3.1 和 .NET Core 5

3 Preview 1 不包括

    • 会话亲和性(又称会话保持)
    • Forwarded标头(可配置)
    • 基于代码的路由定义和预请求路由
    • 指标和日志
    • 性能调整
    • 连接筛选

4 快速开始

Step 01 下载.net framework

    YARP 适用于 .NET Core 3.1 或 .NET 5 Preview 4(或更高版本)。

Step 02 创建一个ASP.NET Core项目

Step 03 打开项目,添加引用,确保其包含

<PropertyGroup>
    <TargetFramework>netcoreapp5.0</TargetFramework>
</PropertyGroup>

  和

<ItemGroup>
    <PackageReference Include="Microsoft.ReverseProxy" Version="1.0.0-preview.1.*" />
</ItemGroup>

Step 04 Startup.cs

  YARP 当前使用配置文件来定义代理的路由和终结点。在ConfigureServices方法中加载。

public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
    services.AddReverseProxy()
        .LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}

  Configure方法定义ASP.NET的请求处理管道。反向代理插入到ASP.NET的终结点路由,然后具有其自己的代理子管道。在这里,可以添加代理管道模块(如负载均衡)来自定义请求的处理。

/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
public void Configure(IApplicationBuilder app)
{
    app.UseHttpsRedirection();

    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapReverseProxy(proxyPipeline =>
        {
            proxyPipeline.UseProxyLoadBalancing();
        });
    });
}

Step 05 配置

  YARP 的配置定义在appsettings.json中:

"ReverseProxy": {
    "Routes": [
      {
        "RouteId": "app1",
        "BackendId": "backend1",
        "Match": {
          "Methods": [ "GET", "POST" ],
          "Host": "localhost",
          "Path": "/app1/"
        }
      },
      {
        "RouteId": "route2",
        "BackendId": "backend2",
        "Match": {
          "Host": "localhost"
        }
      }
    ],
    "Backends": {
      "backend1": {
        "LoadBalancing": {
          "Mode": "Random"
        },
        "Destinations": {
          "backend1_destination1": {
            "Address": "https://example.com:10000/"
          },
          "backend1_destination2": {
            "Address": "http://example.com:10001/"
          }
        }
      },
      "backend2": {
        "Destinations": {
          "backend2_destination1": {
            "Address": "https://example.com:10002/"
          }
        }
      }
    }
  }
    • Backends:请求可以路由到的服务器群集。
    • Destinations:是用于指标、日志记录和会话保持的标识符。
    • Address:URL前缀(基地址)
    • Routes:根据请求的各个方面(如主机名、路径、方法、请求标头等)将传入请求映射到后端群集。路由是有序的,因此,需要首先定义 app1 路由,因为 route2 将作为尚未匹配的所有路径的 catchall。

  好啦,先介绍到这里。

原文链接

  https://devblogs.microsoft.com/dotnet/introducing-yarp-preview-1/?utm_source=vs_developer_news&utm_medium=referral

 

posted @ 2020-06-12 00:33  MeteorSeed  阅读(327)  评论(0编辑  收藏  举报