ASP.NET Core3.1 uses Ocelot open source gateway

1. Build downstream services
Add 2 ASP.NET Core projects, namely Order.Api and User.Api
The Order.Api port is set to 5001, and the User.Api port is set to 5002, both using the HTTP protocol

UsersController code:
[ApiController]
[Route(“v1”)]
public class UsersController : ControllerBase
{
private static List_users = new List
{
new UserItem(“Mack”),
new UserItem(“Jack”)
};
[HttpGet(“users”)]
public IActionResult GetAll()
{
return Ok(users);
}
[HttpGet(“users/{id}”)]
public IActionResult Get(int id)
{
return Ok(users. FirstOrDefault(=>
.Id==id));
}
}
public class UserItem
{
private static int _id=1001;
public int Id { get; set; }
public string Name { get; set; }
public UserItem(string name)
{
Id= _id++;
Name = name;
}
}
OrdersController code:
[ApiController]
[Route(“v1”)]
public class OrdersController : ControllerBase
{
private static List_orders = new List()
{
new OrderItem(1001,99),
new OrderItem(1001,199),
new OrderItem(1002,59)
};
[HttpGet(“orders”)]
public IActionResult GetAll()
{
return Ok(_orders);
}
[HttpGet(“orders/{id}”)]
public IActionResult Get(int id)
{
return Ok(orders. FirstOrDefault( => _.Id == id));
}
[HttpGet(“users/{userId}/orders”)]
public IActionResult GetByUser(int userId)
{
return Ok(orders. FirstOrDefault( => _.UserId == userId));
}
}
public class OrderItem
{
private static int _id=10001;
public int Id { get; set; }
public DateTime CreationTime { get; set; }
public int UserId { get; set; }
public decimal Money { get; set; }
public OrderItem(int userId,decimal money)
{
Id = _id++;
CreationTime = DateTime. Now;
UserId = userId;
Money=money;
}
}

add gateway
Create a new project and set the port to 5000

Install Nuget package: Ocelot, Core3.1 uses version 15.0.7 of Ocelot
Add ocelot.json file
{
“ReRoutes”: [
{
“DownstreamPathTemplate”: “/{url}”,
“DownstreamScheme”: “http”,
“DownstreamHostAndPorts”: [
{
“Host”: “localhost”,
“Port”: 5001
}
],
“UpstreamHttpMethod”: [ “Get”, “Post”, “Delete”, “Put”, “Head” ],
“UpstreamPathTemplate”: “/order-api/{url}”,
// Set the priority, the bigger the priority
“Priority”: 0
},
{
“DownstreamPathTemplate”: “/{url}”,
“DownstreamScheme”: “http”,
“DownstreamHostAndPorts”: [
{
“Host”: “localhost”,
“Port”: 5002
}
],
“UpstreamHttpMethod”: [ “Get”, “Post”, “Delete”, “Put”, “Head” ],
“UpstreamPathTemplate”: “/user-api/{url}”
},
{
“DownstreamPathTemplate”: “/v1/orders”,
“DownstreamScheme”: “http”,
“DownstreamHostAndPorts”: [
{
“Host”: “localhost”,
“Port”: 5001
}
],
“UpstreamHttpMethod”: [ “Get” ],
“UpstreamPathTemplate”: “/order-api/v1/orders”,
“RateLimitOptions”: {
“EnableRateLimiting”: true,
“Period”: “2s”,
“Period Timespan”: 1,
“Limit”: 1
},
“Priority”: 1,
“Key”: “All-Order”
},
// aggregate order of order and user
{
“DownstreamScheme”: “http”,
“DownstreamPathTemplate”: “/v1/users/{userId}/orders”,
“DownstreamHostAndPorts”: [
{
“Host”: “localhost”,
“Port”: 5001
}
],
“UpstreamHttpMethod”: [ “Get” ],
“UpstreamPathTemplate”: “/order-api/v1/users/{userId}/orders”,
“Key”: “Overview-Order”
},
{
“DownstreamHostAndPorts”: [
{
“Host”: “localhost”,
“Port”: 5002
}
],
“DownstreamPathTemplate”: “/v1/users/{userId}”,
“DownstreamScheme”: “http”,
“UpstreamHttpMethod”: [ “Get” ],
“UpstreamPathTemplate”: “/user-api/v1/users/{userId}”,
“Key”: “Overview-User”
},
{
“DownstreamHostAndPorts”: [
{
“Host”: “localhost”,
“Port”: 5002
}
],
“DownstreamPathTemplate”: “/v1/users”,
“DownstreamScheme”: “http”,
“UpstreamHttpMethod”: [ “Get” ],
“UpstreamPathTemplate”: “/user-api/v1/users”,
“Key”: “All-User”
}

],
// aggregate API
“Aggregates”: [
{
“ReRouteKeys”: [
“Overview-User”,
“Overview-Order”
],
“UpstreamPathTemplate”: “/user-overview/{userId}”
},
{
“ReRouteKeys”: [
“All-Users”,
“All-Order”
],
“UpstreamPathTemplate”: “/all”
}
],
“GlobalConfiguration”: {
“BaseUrl”: “http://localhost:5000”
}
}
Program code:

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder. ConfigureAppConfiguration((context, config) =>
                {
                    config.AddJsonFile("ocelot.json", false, true);
                });
                webBuilder. UseStartup<Startup>();
            });
}

Startup code:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

 public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services. AddOcelot();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseOcelot().Wait();
    }
}
</code><img class="look-more-preCode contentImg-no-view" src="//i2.wp.com/csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack.png" alt ="" title="">

run service
Just start the three projects