Introducing YARP Preview 1

栏目: IT技术 · 发布时间: 4年前

内容简介:May 21st, 2020YARP is a reverse proxy toolkit for building fast proxy servers in .NET using the infrastructure from ASP.NET and .NET. The key differentiator for YARP is that it is being designed to be easily customized and tweaked to match the specific nee

Sam

May 21st, 2020

Introducing YARP Preview 1

YARP is a project to create a reverse proxy server. It started when we noticed a pattern of questions from internal teams at Microsoft who were either building a reverse proxy for their service or had been asking about APIs and technology for building one, so we decided to get them all together to work on a common solution, which has become YARP.

YARP is a reverse proxy toolkit for building fast proxy servers in .NET using the infrastructure from ASP.NET and .NET. The key differentiator for YARP is that it is being designed to be easily customized and tweaked to match the specific needs of each deployment scenario. YARP plugs into the ASP.NET pipeline for handling incoming requests, and then has its own sub-pipeline for performing the steps to proxy the requests to backend servers. Customers can add additional modules, or replace stock modules as needed.

With the basic infrastructure of the proxy now in place, we’ve produced the first official build of YARP (Preview 1) in order to better collaborate and get feedback.

What is in Preview 1

  • Core proxy infrastructure
  • Config-based route definitions
  • Pipeline model for extensibility
  • Forwarded Headers (hard coded)
  • Targeting both .NET Core 3.1 and .NET Core 5

What is not in Preview 1

  • Session Affinity #45
  • Forwarded Headers (configurable) #60
  • Code-based route definition & per-request routing #8 , #46
  • Metrics & logging #61
  • Performance tweaks #51
  • Connection filtering #53
  • And much more – see the project board for more details

Getting started with YARP Preview 1

YARP is designed as a library that provides the core proxy functionality, that you can then customize by adding or replacing modules. For preview 1, YARP is being supplied as a nuget package and code snippets. We plan on having a project template, and pre-built exe in future previews.

  1. YARP works with either .NET Core 3.1 or .NET 5 preview 4 (or later). Download the preview 4 (or greater) of .NET 5 SDK from https://dotnet.microsoft.com/download/dotnet/5.0
  2. Create an “Empty” ASP.NET Core application using
    • dotnet new web -n MyProxy -f netcoreapp5.0 (or netcoreapp3.1 for .NET Core 3.1)
      Or
    • Create a new ASP.NET Core web application in Visual Studio, and choose “Empty” for the project template
  3. Open the Project and make sure it includes:

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

    And

    <ItemGroup> 
      <PackageReference Include="Microsoft.ReverseProxy" Version="1.0.0-preview.1.*" /> 
    </ItemGroup>
  4. Startup.cs
    YARP is implemented as a ASP.NET Core component, and so the majority of the sample code is in Startup.cs.
    YARP currently uses configuration files to define the routes and endpoints for the proxy. That is loaded in the ConfigureServices

    method.

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

    The configure method defines the ASP.NET pipeline for processing requests. The reverse proxy is plugged in to ASP.NET endpoint routing, and then has its own sub pipeline for the proxy. Here proxy pipeline modules, such as load balancing can be added to customize the handling of the request.

    /// <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();
            });
        });
    }
  5. Configuration

    The configuration for YARP is defined in JSON in the appsettings.json file. It defines a set of:

    • Backends – which are the clusters of servers that requests can be routed to
      • The destination name is an identifier that is used in metrics, logging and for session affinity.
      • Address is the URI prefix that will have the original request path appended to it
    • Routes – which map incoming requests to the backend clusters based on aspects of the request such as host name, path, method, request headers etc.
      Routes are ordered, so the app1 route needs to be defined first as route2 will act as a catchall for all paths that have not already been matched.

      The following needs to be a peer to the Logging and AllowedHosts sections.

      "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/"
                }
              }
            }
          }
        }

Survey

We are interested to see how you are currently using a reverse proxy and what problems we need to solve with this project. We’d appreciate your input here .

Code Feedback

If you have feature suggestions, changes, bugs etc, please provide feedback using the issue templates in the project repo. We welcome contributions, please see contibuting.md for more details.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

思想的未来

思想的未来

(美)劳伦斯﹒莱斯格 / 李旭 / 中信出版社 / 2004-10 / 29.00元

因特网革命已到来,一些人说它已经过去,革命缘何而来?又缘何而去呢? 劳伦斯·莱斯格对因特网革命中为何会出现一种反革命的破坏性力量及后果做出了解释。创作之所以繁荣,是因为因特网保护了创新的公共资源。是因为因特网保护了创新的公共资源。因特网的独特设计营造出一个中立的平台。最广大范围的作者们可在此平台上进行试验。围绕此平台的法律架构对这一自由空间给予了保护,以使文化和信息——我们这个时代的......一起来看看 《思想的未来》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具