Ocelot僅適用於.NET Core,目前是根據netstandard2.0構建的,若是Ocelot適合您,這個文檔可能會有用。nginx
安裝NuGet包json
使用nuget安裝Ocelot及其依賴項。 您須要建立一個netstandard2.0項目並將其打包到其中。 而後按照下面的「啓動」和「配置」部分啓動並運行。api
Install-Package Ocelotapp
全部版本均可以在這裏找到。ui
配置spa
如下是一個很是基本的ocelot.json。 它不會作任何事情,但應該讓Ocelot開始。命令行
{ "ReRoutes": [], "GlobalConfiguration": { "BaseUrl": "https://api.mybusiness.com" } }
這裏要注意的最重要的是BaseUrl。 Ocelot須要知道它正在運行的URL,以便執行Header查找和替換以及某些管理配置。 設置此URL時,它應該是客戶端將看到運行Ocelot的外部URL,例如 若是你正在運行容器,Ocelot可能會在網址http://123.12.1.1:6543上運行,但在https://api.mybusiness.com上響應它以前有相似nginx的東西。 在這種狀況下,Ocelot基本網址應爲https://api.mybusiness.com。 code
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(); } }
.NET Core 1.0中間件
安裝NuGet包blog
使用nuget安裝Ocelot及其依賴。 您須要建立一個netcoreapp1.0 + projct並將包帶入其中。 而後按照下面的「啓動」和「配置」部分啓動並運行。 請注意,您須要從NuGet Feed中選擇一個Ocelot包。
{ { "ReRoutes": [], "GlobalConfiguration": {} }
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(); } } Startup.cs文件 使用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(); } }
以上這些就是Ocelot基本入門內容。喜歡就收藏此文。版權全部,禁止未經受權的複製轉載。詳細的中文文檔查閱 http://nopapp.com/Blog/Article/Ocelot-Basic