Ocelot网关
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進行改造?
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
?
namespace OcelotGateway
{
? ? public class Program
? ? {
? ? ? ? public static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? BuildWebHost(args).Run();
? ? ? ? }
? ? ? ? public static IWebHost BuildWebHost(string[] args)
? ? ? ? {
? ? ? ? ? ? IWebHostBuilder builder = new WebHostBuilder();
? ? ? ? ? ? //注入WebHostBuilder
? ? ? ? ? ? return builder.ConfigureServices(service =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? service.AddSingleton(builder);
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? //加載configuration配置文人年
? ? ? ? ? ? ? ? .ConfigureAppConfiguration(conbuilder =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? conbuilder.AddJsonFile("configuration.json");
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .UseKestrel()
? ? ? ? ? ? ? ? .UseUrls("http://*:5000")
? ? ? ? ? ? ? ? .UseStartup<Startup>()
? ? ? ? ? ? ? ? .Build();
? ? ? ? }
? ? }
}
同時,修改Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
?
namespace OcelotGateway
{
? ? public class Startup
? ? {
? ? ? ? public Startup(IConfiguration configuration)
? ? ? ? {
? ? ? ? ? ? Configuration = configuration;
? ? ? ? }
? ? ? ? public IConfiguration Configuration { get; } ? ??
? ? ? ? public void ConfigureServices(IServiceCollection services)
? ? ? ? { ? ? ?
? ? ? ? ? ? //注入配置文件,AddOcelot要求參數是IConfigurationRoot類型,所以要作個轉換
? ? ? ? ? ? services.AddOcelot(Configuration as ConfigurationRoot);
? ? ? ? }
? ? ? ? public ?void Configure(IApplicationBuilder app, IHostingEnvironment env)
? ? ? ? {
? ? ? ? ? ? //添加中間件
? ? ? ? ? ? app.UseOcelot().Wait();
? ? ? ? }
? ? }
}
為了測試數據好看,我們把DemoAAPI項目和DemoBAPI項目的ValuesController作一下修改: ? ?
[Route("demoaapi/[controller]")]
? ? public class ValuesController : Controller
? ? { ? ? ??
? ? ? ? [HttpGet]
? ? ? ? public IEnumerable<string> Get()
? ? ? ? {
? ? ? ? ? ? return new string[] { "DemoA服務", "請求" };
? ? ? ? }
//……
}
?
? ? [Route("demobapi/[controller]")]
? ? public class ValuesController : Controller
? ? { ? ? ??
? ? ? ? [HttpGet]
? ? ? ? public IEnumerable<string> Get()
? ? ? ? {
? ? ? ? ? ? return new string[] { "DemoB服務", "請求" };
? ? ? ? }
//……
}
最后在解決方案屬性->多個啟動項目中,把DemoAAPI,DemoBAPI,OcelotGateway都設成啟動,開始啟動解決方案,效果如下圖?
相關文章:
-
Ocelot——初識基于.Net Core的API網關
-
Ocelot API網關的實現剖析
-
微服務網關Ocelot
-
API網關Ocelot 使用Polly 處理部分失敗問題
-
談談微服務中的 API 網關(API Gateway)
原文:http://www.cnblogs.com/axzxs2001/p/8005041.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
- 上一篇: 谈谈微服务中的 API 网关(API G
- 下一篇: Ocelot统一权限验证