Ocelot是一個.net core框架下的網關的開源項目,下圖是官方給出的基礎實現圖,即把後臺的多個服務統一到網關處,前端應用:桌面端,web端,app端都只用訪問網關便可。html
Ocelot的實現原理就是把客戶端對網關的請求(Request),按照configuration.json的映射配置,轉發給對應的後端http service,而後從後端http service獲取響應(Response)後,再返回給客戶端。固然有了網關後,咱們能夠在網關這層去作統一驗證,也能夠在網關處統一做監控。前端
接下來作個Demoweb
新建三個asp.net core web aip項目:json
OcelotGateway網關項目,端口是5000後端
DemoAAPI項目A,端口是5001api
DemoBAPI項目B,端口是5002架構
(注:端口能夠在每一個項目的Properties下的launchSettings.json中修改,發佈後的端口能夠在Program.cs中用UseUrls(「http://*:5000」)來修改)app
對於OcelotGateway:框架
引用Ocelot的Nuget包:asp.net
視圖->其餘窗口->程序包管理控制檯: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 }
同時,修改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 }
爲了測試數據好看,咱們把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 }
最後在解決方案屬性->多個啓動項目中,把DemoAAPI,DemoBAPI,OcelotGateway都設成啓動,開始啓動解決方案,效果以下圖
《基於.net core微服務架構視頻》
http://edu.51cto.com/course/13342.html