Ocelot仅适用于.NET Core,目前是为netstandard2.0构建的。若是Ocelot适合您,那么此文档可能会有用。html
安装NuGet包nginx
使用nuget安装Ocelot及其依赖项。您须要建立一个netstandard2.0项目并将其打包到其中。而后按照下面的“启动”和“ 配置”部分启动并运行。json
Install-Package Ocelot
全部版本均可以在这里找到。api
配置app
如下是一个很是基本的ocelot.json。它不会作任何事情,但应该让Ocelot开始。ui
{
"ReRoutes": [], "GlobalConfiguration": { "BaseUrl": "https://api.mybusiness.com" } }
这里要注意的最重要的是BaseUrl。Ocelot须要知道它正在运行的URL,以便进行Header查找和替换以及某些管理配置。设置此URL时,它应该是客户端将看到运行Ocelot的外部URL,例如,若是您正在运行容器,则Ocelot可能会在URL上运行http://123.12.1.1:6543但在其前面有相似nginx的响应在https上响应://api.mybusiness.com。在这种状况下,Ocelot基本网址应为https://api.mybusiness.com。spa
若是您正在使用容器并要求Ocelot在http://123.12.1.1:6543响应客户端, 那么您能够执行此操做,可是若是您要部署多个Ocelot,您可能但愿在某种类型的命令行上传递它脚本。但愿您使用的任何调度程序均可以经过IP。命令行
程序code
而后在您的Program.cs中,您将须要如下内容。须要注意的主要事项是AddOcelot()(添加ocelot服务),UseOcelot()。Wait()(设置全部Ocelot中间件)。htm
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();
})
.ConfigureLogging((hostingContext, logging) =>
{
//add your logging
})
.UseIISIntegration()
.Configure(app =>
{
app.UseOcelot().Wait();
})
.Build()
.Run();
}
}
**Note:** When using ASP.NET Core 2.2 and you want to use In-Process hosting, replace **.UseIISIntegration()** with **.UseIIS()**, otherwise you'll get startup errors.
安装NuGet包
使用nuget安装Ocelot及其依赖项。您须要建立一个netcoreapp1.0 + projct并将包带入其中。而后按照下面的“启动”和“ 配置”部分启动并运行。请注意,您须要从NuGet Feed中选择一个Ocelot包。
全部版本均可以在这里找到。
配置
如下是一个很是基本的ocelot.json。它不会作任何事情,但应该让Ocelot开始。
{
"ReRoutes": [], "GlobalConfiguration": {} }
程序
而后在您的Program.cs中,您将须要如下内容。
public class Program { public static void Main(string[] args) { IWebHostBuilder builder = new WebHostBuilder(); builder.ConfigureServices(s => { }); builder.UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>(); var host = builder.Build(); host.Run(); } }
启动
使用json文件进行配置的示例启动以下所示。
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("ocelot.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot(Configuration);
}
public void Configure(IApplicationBuilder app)
{
app.UseOcelot().Wait();
}
}
这几乎是你入门须要的所有。