YARP is an open source reverse proxy project of Microsoft. Its English name is YT another reverse proxy. The most famous reverse proxy is nginx. Yes, YARP can also be used to complete most functions of nginx, such as proxy to different back-end services according to different domain names. Since it can be used as a reverse proxy, it can actually be used as a service gateway, similar to Ocelot. Of course, it lacks some functions, such as current limiting and degradation. Anyway, let's experience it today.
Run YARP
YARP is very simple to use. It is just a class library. Let's create a new ASP Net core. Then use nuget to install YARP ReverseProxy .
Install-Package Yarp.ReverseProxy -Version 1.0.0
Modify program The CS file contains the following:
var builder = WebApplication.CreateBuilder(args); builder.Services.AddReverseProxy() .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")); var app = builder.Build(); app.MapReverseProxy(); app.Run();
At Appsettings Add the ReverseProxy node to the JSON file:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ReverseProxy": { "Routes": { "route1" : { "ClusterId": "cluster1", "Match": { "Path": "{**catch-all}" }, } }, "Clusters": { "cluster1": { "Destinations": { "destination1": { "Address": "https://www.baidu.com/" } } } } } }
We configure a cluster cluster1 with the address of Baidu website; Configure a route route1 to match all URLs. In this way, when we visit this website, all requests will be represented to Baidu.
Let's run it and try:
You can see that the content of Baidu is rendered when we visit the address of localhost:5085.
Integrating AgileConfig to hot update configuration
Through the above, we simply demonstrated the use of YARP. Our demo configuration file is fairly simple, but the essence of YARP is actually in the configuration file. If your agent policy is very complex, the configuration of YARP will be very complex. Use local Appsettings JSON is obviously too cumbersome to update the configuration file every time, and it needs to restart the YARP gateway. Below, we demonstrate how YARP integrates AgileConfig so that the configuration can be edited online and supports hot update.
Agileconfig is a lightweight configuration center that can be easily configured for NET project provides the function of configuration center. Project address: https://github.com/dotnetcore/AgileConfig .
Run AgileConfig server
AgileConfig server can be easily deployed through docker.
sudo docker run \ --name agile_config \ -e TZ=Asia/Shanghai \ -e adminConsole=true \ -e db:provider=sqlite \ -e db:conn="Data Source=agile_config.db" \ -p 5000:5000 \ #-v /your_host_dir:/app/db \ -d kklldog/agile_config:latest
After successful deployment, we will create a new application in the application interface: Yarp_test
Click "Edit JSON" in the configuration item editing interface to pop up the JSON editing view, and the original Appsettings Copy the contents of the ReverseProxy node in the JSON file. Click Save > publish.
Integrating agileconfig Client
After AgileConfig is successfully configured, we need to integrate AgileConfig on YARP Gateway project Client to read the configuration of the configuration center.
Use nuget to install agileconfig Client :
Install-Package AgileConfig.Client -Version 1.2.1.5
At Appsettings Add the AgileConfig node in JSON to configure the information of related applications, and delete the original ReverseProxy node because it is no longer needed.
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "AgileConfig": { "appId": "yarp_test", "secret": "", "nodes": "http://localhost:5000/" } }
Modify program CS file is as follows:
var builder = WebApplication.CreateBuilder(args); //add agileconfig configuration provider builder.Host.ConfigureAppConfiguration((_, bd) => { bd.AddAgileConfig(); }); builder.Services.AddReverseProxy() .LoadFromConfig(builder.Configuration); var app = builder.Build(); app.MapReverseProxy(); app.Run();
After the modification, we run the YARP Gateway project. If you can display Baidu's page by visiting localhost:5085, it indicates that the configuration of the configuration center has been correctly read.
Hot renewal
If the above steps are successful, we can try the hot update of the configuration. Open the server of Agileconfig and continue to edit the above configuration. We change the cluster address to www.qq.com COM, changed to Tencent's network management. Click Save > publish.
Without restarting the YARP Gateway project, we directly refresh the page localhost:5085, and you can see that Tencent's web page is displayed now.
Demo project address: https://github.com/kklldog/YarpDemo