API網關是微服務架構中的惟一入口,它提供一個單獨且統一的API入口用於訪問內部一個或多個API。它能夠具備身份驗證,監控,負載均衡,緩存,請求分片與管理,靜態響應處理等。API網關方式的核心要點是,全部的客戶端和消費端都經過統一的網關接入微服務,在網關層處理全部的非業務功能。一般,網關也是提供REST/HTTP的訪問API。服務端經過API-GW註冊和管理服務。git
Ocelot是用.net Core實現的一款開源的網關,Ocelot其實就是一組按照順序排列的.net core中間件。它接受到請求以後用request builder構建一個HttpRequestMessage對象併發送到下游服務,當下遊請求返回到Ocelot管道時再由一箇中間件將HttpRequestMessage映射到HttpResponse上返回客戶端。github
開源地址點擊這裏web
新建三個項目webApi項目,分別命名爲ApiGateway,Api_A,Api_B算法
設置Api_A端口爲5001,Api_B端口爲5002,ApiGateway爲5000json
爲ApiGateway項目安裝Ocelot,在Nuget中搜索Ocelot或者直接使用Install-Package Ocelot
進行安裝。最新版本爲13.8.x支持core3.0。api
在ApiGateway新建一個configuration.json
配置文件。緩存
{ "ReRoutes": [ { //上游Api請求格式 "UpstreamPathTemplate": "/Api_A/{controller}/{action}", //網關轉發到下游格式 "DownstreamPathTemplate": "/api/{controller}/{action}", //上下游支持請求方法 "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ], "DownstreamScheme": "http", //下游服務配置 "DownstreamHostAndPorts": [ { //下游地址 "Host": "localhost", //下游端口號 "Port": 5001 } ] }, { "UpstreamPathTemplate": "/Api_B/{controller}/{action}", "DownstreamPathTemplate": "/api/{controller}/{action}", "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ], "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 } ] } ] }
在Startup.cs
的ConfigureServices
和Configure
中配置Ocelot服務器
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddOcelot(new ConfigurationBuilder() .AddJsonFile("configuration.json") .Build()); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseOcelot(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); }
分別爲Api_A,和Api_B分別添加一個print接口。架構
[HttpGet("print")] public string Print() { return "Api_A"; }
[HttpGet("print")] public string Print() { return "Api_B"; }
啓動三個項目,使用postman來調用網關併發
訪問Api_A服務
訪問Api_B服務
能夠看到網關經過接受到的請求的Url後經過咱們的配置自動轉發到了咱們想要訪問的服務。
當下遊擁有多個節點的時候,咱們能夠用DownstreamHostAndPorts來配置
{ "UpstreamPathTemplate": "/Api_A/{controller}/{action}", "DownstreamPathTemplate": "/api/{controller}/{action}", "DownstreamScheme": "https", "LoadBalancer": "LeastConnection", "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ], "DownstreamHostAndPorts": [ { "Host": "127.0.0.1", "Port": 5001, }, { "Host": "127.00.1", "Port": 5002, } ], }
限流能夠防止上下游服務器由於過載而崩潰,可使用RateLimitOptions來配置限流
{ "RateLimitOptions": { "ClientWhitelist": [「127.0.0.1」], "EnableRateLimiting": true, "Period": "5s", "PeriodTimespan": 1, "Limit": 10 } }
超過限流後會返回429狀態碼,並在在返回頭(Response Header)的Retry-After屬性中返回等待重置時間。
限流的默認提示,code碼,和限制標誌都是能夠本身配置的
{ "GlobalConfiguration": { "BaseUrl":"www.baidu.com", "RateLimitOptions": { "DisableRateLimitHeaders": false, "QuotaExceededMessage": "接口限流!", "HttpStatusCode": 200, "ClientIdHeader": "ClientId" } }
熔斷是在下游服務故障或者請求無響應的時候中止將請求轉發到下游服務。
{ "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 3, "DurationOfBreak": 20, "TimeoutValue": 5000 } }
Ocelot能夠對下游請求結果進行緩存,主要是依賴於CacheManager來實現的。
{ "FileCacheOptions": { "TtlSeconds": 60, "Region": "key" } }
Ocelot容許在請求下游服務以前和以後轉換Header.目前Ocelot只支持查找和替換.
若是們須要轉發給下游的Header重添加一個key/value
{ "UpstreamHeaderTransform": { "demo": "xxxxxxx" } }
若是們須要在返回給客戶端的的Header中添加一個key/value
{ "DownstreamHeaderTransform": { "demo": "xxxxxxx" } }
若是咱們須要替換Heaher中某些值
{ //請求 "UpstreamHeaderTransform": { "demo": "a,b" }, //響應 "DownstreamHeaderTransform": { "demo": "a,b" } }
語法是{find},{replace}, 利用b取代a
在Header轉換中可使用佔位符
若是您想將location頭返回給客戶端,可能須要將location更改成Ocelot的地址而不是下游服務地址。 Ocelot可使用如下配置實現。
{ "DownstreamHeaderTransform": { "Location": "{DownstreamBaseUrl},{BaseUrl}" }, }
給Header中添加下游地址
響應的Header中已經替換成BaseUrl了
簡單的實現了經過網關來訪問api接口。ocelot功能遠不止這些,以後會實現與IdentityServer的認證鑑權。服務發現。