Ocelot是一個.net core框架下的網關的開源項目,下圖是官方給出的基礎實現圖,即把後臺的多個服務統一到網關處,前端應用:桌面端,web端,app端都只用訪問網關即可。 ...
Ocelot是一個.net core框架下的網關的開源項目,下圖是官方給出的基礎實現圖,即把後臺的多個服務統一到網關處,前端應用:桌面端,web端,app端都只用訪問網關即可。
Ocelot的實現原理就是把客戶端對網關的請求(Request),按照configuration.json的映射配置,轉發給對應的後端http service,然後從後端http service獲取響應(Response)後,再返回給客戶端。當然有了網關後,我們可以在網關這層去做統一驗證,也可以在網關處統一作監控。
接下來做個Demo
新建三個asp.net core web aip項目:
OcelotGateway網關項目,埠是5000
DemoAAPI項目A,埠是5001
DemoBAPI項目B,埠是5002
(註:埠可以在每個項目的Properties下的launchSettings.json中修改,發佈後的埠可以在Program.cs中用UseUrls(“http://*:5000”)來修改)
對於OcelotGateway:
引用Ocelot的Nuget包:
視圖->其他視窗->程式包管理控制台:Install-Package Ocelot
或項目右鍵“管理Nuget程式包”,在瀏覽里查找Ocelot進行安裝
在OcelotGateway項目中添加一個configuration.json文件,關把它的屬性“複製到輸出目錄”,設成“始終複製”,內容如下:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/demoaapi/values",
"DownstreamScheme": "http",
"DownstreamPort": 5001,
"DownstreamHost": "localhost",
"UpstreamPathTemplate": "/demoaapi/values",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3,
"DurationOfBreak": 10,
"TimeoutValue": 5000
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false
},
"AuthenticationOptions": {
"AuthenticationProviderKey": "",
"AllowedScopes": []
}
},
{
"DownstreamPathTemplate": "/demobapi/values",
"DownstreamScheme": "http",
"DownstreamPort": 5002,
"DownstreamHost": "localhost",
"UpstreamPathTemplate": "/demobapi/values",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3,
"DurationOfBreak": 10,
"TimeoutValue": 5000
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false
},
"AuthenticationOptions": {
"AuthenticationProviderKey": "",
"AllowedScopes": []
}
}
]
}
接下來對OcelotGateway的Program.cs進行改造
1 using Microsoft.AspNetCore.Hosting; 2 3 using Microsoft.Extensions.Configuration; 4 5 using Microsoft.Extensions.DependencyInjection; 6 7 8 9 namespace OcelotGateway 10 11 { 12 13 public class Program 14 15 { 16 17 public static void Main(string[] args) 18 19 { 20 21 BuildWebHost(args).Run(); 22 23 } 24 25 public static IWebHost BuildWebHost(string[] args) 26 27 { 28 29 IWebHostBuilder builder = new WebHostBuilder(); 30 31 //註入WebHostBuilder 32 33 return builder.ConfigureServices(service => 34 35 { 36 37 service.AddSingleton(builder); 38 39 }) 40 41 //載入configuration配置文人年 42 43 .ConfigureAppConfiguration(conbuilder => 44 45 { 46 47 conbuilder.AddJsonFile("configuration.json"); 48 49 }) 50 51 .UseKestrel() 52 53 .UseUrls("http://*:5000") 54 55 .UseStartup<Startup>() 56 57 .Build(); 58 59 } 60 61 } 62 63 }View Code
同時,修改Startup.cs
1 using Microsoft.AspNetCore.Builder; 2 3 using Microsoft.AspNetCore.Hosting; 4 5 using Microsoft.Extensions.Configuration; 6 7 using Microsoft.Extensions.DependencyInjection; 8 9 using Ocelot.DependencyInjection; 10 11 using Ocelot.Middleware; 12 13 14 15 namespace OcelotGateway 16 17 { 18 19 public class Startup 20 21 { 22 23 public Startup(IConfiguration configuration) 24 25 { 26 27 Configuration = configuration; 28 29 } 30 31 public IConfiguration Configuration { get; } 32 33 public void ConfigureServices(IServiceCollection services) 34 35 { 36 37 //註入配置文件,AddOcelot要求參數是IConfigurationRoot類型,所以要作個轉換 38 39 services.AddOcelot(Configuration as ConfigurationRoot); 40 41 } 42 43 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 44 45 { 46 47 //添加中間件 48 49 app.UseOcelot().Wait(); 50 51 } 52 53 } 54 55 }View Code
為了測試數據好看,我們把DemoAAPI項目和DemoBAPI項目的ValuesController作一下修改:
1 [Route("demoaapi/[controller]")] 2 3 public class ValuesController : Controller 4 5 { 6 7 [HttpGet] 8 9 public IEnumerable<string> Get() 10 11 { 12 13 return new string[] { "DemoA服務", "請求" }; 14 15 } 16 17 //…… 18 19 } 20 21 22 23 [Route("demobapi/[controller]")] 24 25 public class ValuesController : Controller 26 27 { 28 29 [HttpGet] 30 31 public IEnumerable<string> Get() 32 33 { 34 35 return new string[] { "DemoB服務", "請求" }; 36 37 } 38 39 //…… 40 41 }View Code
最後在解決方案屬性->多個啟動項目中,把DemoAAPI,DemoBAPI,OcelotGateway都設成啟動,開始啟動解決方案,效果如下圖