Ocelot簡易教程(七)之配置文件數據庫存儲插件源碼解析
Ocelot是爲.net core量身定作的,目前是基於 netstandard2.0進行構建的。html
使用nuget安裝Ocelot及其依賴項。您須要建立一個netstandard2.0項目並將其Package安裝到項目中。而後按照下面的「啓動」和「 配置」節點啓動並運行。
安裝命令 Install-Package Ocelot
你能夠經過下面的連接查看Ocelot的歷史版本https://www.nuget.org/packages/Ocelot/ 目前最新版是10.0.4。最新版最近正在進行重構,更新比較頻繁。nginx
如下配置是一個很是基礎的Ocelot.json配置,他不會作任何事情,但卻可讓ocelot正常運行。數據庫
{ "ReRoutes": [], "GlobalConfiguration": { "BaseUrl": "https://api.yilezhu.cn" } }
這個配置裏面最重要的是BaseUrl。Ocelot須要知道它正在運行的URL,以便執行Header查找和替換以及某些管理配置。設置此URL時,它應該是客戶端將看到Ocelot運行的外部URL,例如,若是您正在運行容器,則Ocelot可能會在URL上運行http://123.12.1.1:6543但在其前面有相似nginx的響應在https://api.yilezhu.cn。在這種狀況下,Ocelot基本網址應爲https://api.yilezhu.cn。json
若是因爲某種緣由你正在使用容器而且但願Ocelot在http://123.12.1.1:6543上響應客戶端的請求, 那麼你能夠這樣作可是若是要部署多個Ocelot,你可能但願在命令行中傳遞它某種腳本。但願您使用的任何調度程序均可以傳遞IP。c#
特別須要注意的是,這裏的Ocelot.json配置文件須要在VS中右鍵修改成「始終複製」屬性。api
官方文檔是按照下面進行配置的。不過我的仍是習慣在Sartup.cs文件中進行相關的配置。博主就先貼出官方文檔給出的配置方法。
而後在你的Program.cs你將按照如何代碼進行配置。這裏最主要的是AddOcelot() 添加 ocelot 服務), UseOcelot().Wait() (使用 Ocelot中間件).app
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(); }
我我的也比較習慣在Startup.cs中進行配置,不習慣在Program.cs中配置。下面是我配置的一種方式,固然你也能夠自由發揮。async
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddOcelot(new ConfigurationBuilder() .AddJsonFile("ocelot.json") .Build()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public async void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } await app.UseOcelot(); app.UseMvc(); }
今天只是給你們介紹Ocelot的很是很是簡單地使用,能夠說零配置,並介紹了官方的使用方法以及我平時的使用方式,只爲了快速開始Ocelot,讓項目可以跑起來。接下來咱們會詳細的介紹Ocelot的配置。優化