[译]Ocelot - Getting Started

时间:2023-03-09 23:09:18
[译]Ocelot - Getting Started

原文

Ocelot专为.NET Core而设计。

.NET Core 2.1

安装

首先需要创建一个netstandard2.0项目,然后再通过nuget安装。

Install-Package Ocelot

Configuration

下面是个最基本的最简单的ocelot.json文件的内容。

{
"ReRoutes": [],
"GlobalConfiguration": {
"BaseUrl": "https://api.mybusiness.com"
}
}

这里我们要注意下BaseUrl。这个URL地址应该是对外的,客户可见的地址,而不应该是一个内部的地址。

Program

public class Program
{
public static void Main(string[] args)
{
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddJsonFile("ocelot.json")
.AddEnvironmentVariables();
})
.ConfigureServices(s => {
s.AddOcelot(); // 添加Ocelot services
})
.ConfigureLogging((hostingContext, logging) =>
{
//add your logging
})
.UseIISIntegration()
.Configure(app =>
{
app.UseOcelot().Wait(); // 使用ocelot中间件
})
.Build()
.Run();
}
}