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 }
為了測試資料好看,我們把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
****歡迎關注我的asp.net core系統課程****《asp.net core精要講解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core項目實戰》 https://ke.qq.com/course/291868
《基于.net core微服務》 https://ke.qq.com/course/299524