一步一步地在 .NET Core 中使用 Ocelot 作为 API 网关。
API 网关是 API 的前端服务器,负责处理传入的 API 请求并将其路由到相应的后端服务。它通过提供系统的单一入口点,在微服务架构中发挥着至关重要的作用。API
网关的一些主要功能包括:
- 路由
- 验证
- 授权
- 请求组成
- 缓存
- 负载均衡
- 容错
- 服务发现
在基于.NET Core 的微服务中,API 网关有很多流行的选择,例如:
- 山猫
- YARP 等。这篇博文解释了 Ocelot 如何成为 .NET Core API 中的 API 网关。
设置 API
有两个 API。第一个 API 有一个 ProductController,它有两个 EndPoint。
[Route("api/products")]
[ApiController]
public class ProductController : ControllerBase
{
static List<Product> Products = new List<Product>()
{
new Product { Id = 1, Name = "Product 1", Price = 10.0m },
new Product { Id = 2, Name = "Product 2", Price = 20.0m },
new Product { Id = 3, Name = "Product 3", Price = 30.0m }
};
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> Get()
{
var products = await GetProductsAsync();
await Task.Delay(500);
return Ok(products);
}
[HttpPost]
public async Task<ActionResult<Product>> Post(Product product)
{
Products.Add(product);
await Task.Delay(500);
// Return the product along with a 201 Created status code
return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
}
private Task<List<Product>> GetProductsAsync()
{
return Task.FromResult(Products);
}
}
这些 EndPoint 可通过http://localhost:5047/api/products访问,用于 GET 和 POST 操作。
第二个 API 的 InvoiceController 只有一个 EndPoint。
[Route("api/invoice")]
[ApiController]
public class InvoiceController : ControllerBase
{
[HttpGet]
public async Task<ActionResult<IEnumerable<string>>> Get()
{
await Task.Delay(100);
return new string[] { "Dhananjay", "Nidhish", "Vijay","Nazim","Alpesh" };
}
}
可通过http://localhost:5162/api/invoice访问 EndPoint来执行 GET 操作。
设置 API 网关
我们将使用 Ocelot 设置 API 网关。为此,请按照以下步骤操作:
- 创建一个 API 项目。
- 不要向其中添加任何控制器。
首先,将 Nuget 中的 Ocelot 包添加到项目中。
添加Ocelot包后,在API Gateway项目中添加一个名为ocelot.json的文件。
{
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5001"
},
"Routes": [
{
"UpstreamPathTemplate": "/gateway/products",
"UpstreamHttpMethod": [ "GET" ],
"DownstreamPathTemplate": "/api/products",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5047
}
]
},
{
"UpstreamPathTemplate": "/gateway/invoice",
"UpstreamHttpMethod": [ "GET" ],
"DownstreamPathTemplate": "/api/invoice",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5162
}
]
}
]
}
让我们探索一下上述文件中的每个配置。
- 在 GlobalConfiguration 部分中,BaseUrl 是 API 网关的 URL。API 客户端将与此 URL 进行交互。运行 API 网关项目时,它应该在此基本 URL 上运行
- 路线部分包含数组中的各种路线
- 该路线分为上游和下游部分。
- UpStream 部分代表 API 网关
- DownStream 部分代表 API。
上述配置如下图所示:
通过上述配置,当您点击 EndPoint http://localhost:5001/gateway/products时,它将被重定向到 API EndPoint http://localhost:5047/api/Products
接下来在 Program.cs 中,在 App Gateway 启动时,添加以下配置。
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);
app.UseOcelot();
现在运行 API 网关应用程序,您应该能够导航私有 API。除了 GET 之外,Ocelot 还支持其他 HTTP 动词。您可以像下面这样添加 POST 操作的路由。
{
"UpstreamPathTemplate": "/gateway/products",
"UpstreamHttpMethod": [ "POST" ],
"DownstreamPathTemplate": "/api/products",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5047
}
]
},
使用基本配置,您应该能够在私有 API 中读取 HttpContext 对象、标头和请求对象。
希望这篇博文对您有所帮助。感谢您的阅读。