上篇文章(.NET Core 微服務—API網關(Ocelot) 教程 [一])介紹了Ocelot 的相關介紹。html
接下來就一塊兒來看如何使用,讓它運行起來。git
環境準備github
爲了驗證Ocelot 網關效果,咱們先建立3個webapi項目:目錄api(Api.Catalog)、訂單api(Api.Ordering)、Ocelot網關(ApiGateway.Ocelot);併爲每一個WebApi項目添加Values控制器(ValuesController),用於區分最終調用效果web
以下圖:json
Ocelot使用api
一、添加Ocelot包依賴:數組
接下來使用Nuget包管理工具爲ApiGateway.Ocelot項目添加Ocelot包引用:服務器
固然也可用使用命令方式添加Ocelot包:app
Install-Package Ocelot
二、添加Ocelot配置文件:(重點)負載均衡
向ApiGateway.Ocelot項目添加一個Ocelot.json配置文件,並修改配置文件爲以下內容:
{ "GlobalConfiguration": { }, "Routes": [ { "DownstreamPathTemplate": "/api/{everything}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5331 }, { "Host": "localhost", "Port": 5332 } ], "UpstreamPathTemplate": "/{everything}", "UpstreamHttpMethod": [ "Get", "Post" ], "LoadBalancerOptions": { "Type": "RoundRobin" } } ] }
接下來簡單介紹下相關配置節點意義。能夠看出配置文件主要包含:Routes和GlobalConfiguration。完整的配置內容能夠查看:官方文檔
GlobalConfiguration:顧名思義就是全局配置,此節點的配置容許覆蓋Routes裏面的配置
Routes:告訴Ocelot如何處理上游的請求
DownstreamPathTemplate:下游的路由模板,即真實處理請求的路徑模板
DownstreamScheme:請求的方式,如:http,https
DownstreamHostAndPorts:下游的IP以及端口,能夠有多個(若是使用負載均衡),方便實現負載均衡,固然你也可使用服務發現,實現下游服務的自動註冊與發現
UpstreamPathTemplate:上游請求的模板,即用戶真實請求的連接
UpstreamHttpMethod:上游請求的http方法,是個數組,你能夠寫多個
LoadBalancerOptions:負載均衡選項(DownstreamHostAndPorts有多個的時候才能看到效果),有三種方式
LeastConnection : 將請求發往最空閒的那個服務器
RoundRobin :輪流發送
NoLoadBalance :不啓用負載均衡,老是發往第一個請求或者服務發現的那個服務器
三、啓用Ocelot中間件:
a) 首先在ApiGateway.Ocelot項目中的Program.cs中加載ocelot.json的配置文件,以下所示:
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .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(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
b) 接下來在Startup.cs文件中註冊服務:
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOcelot();//注入Ocelot服務 services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseOcelot().Wait();//使用Ocelot中間件 app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
c) 最後:
把目錄api(Api.Catalog)、訂單api(Api.Ordering)、Ocelot網關(ApiGateway.Ocelot)分別設置啓動設置爲:http://localhost:533二、http://localhost:533一、http://localhost:5330。
到此Ocelot基本使用完成,接下來驗證下效果
效果驗證:
經過ocelot.json設置能夠獲得:
接着驗證運行效果是否是這樣:
一、打開http://localhost:5330/values 以下圖:最終獲得是: Api.Catalog 的結果
二、接着咱們刷新下當前界面:獲得以下結果:負載均衡輪詢選項生效成功
總結:
經過上面的示例,很是簡單的就成功的運行了Ocelot網關的路由效果和負載均衡的簡單效果。
接下來我就要進一步詳細瞭解Ocelot的配置內容和其餘使用方式(如:認證服務方式、服務自動發現註冊)