1. 程式人生 > 實用技巧 >.netcore的微服務學習(四)--閘道器(gateway)之Ocelot+Consul+polly學習

.netcore的微服務學習(四)--閘道器(gateway)之Ocelot+Consul+polly學習

一,接著前面的程式碼,我們先引用Ocelot.Provider.Polly,然後我們的startup接著配置下,如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Ocelot.DependencyInjection; using Ocelot.Middleware; using Ocelot.Provider.Consul; using Ocelot.Provider.Polly; namespace OcelotDemo { public class Startup {
public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOcelot().AddConsul().AddPolly(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseOcelot(); } } }

二,Polly之快取設定,如下配置(快取:就是在閘道器快取請求的值,時間也是在配置中設定,本配置設定的是10S,這個適用於一般不會變化的值)

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{url}", //服務地址--url變數
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/TestOcelotConsul/{url}", //閘道器地址--url變數
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "ServiceName": "TestConsulService", //consul服務名稱
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //輪詢      LeastConnection-最少連線數的伺服器   NoLoadBalance不負載均衡
      },
      "UseServiceDiscovery": true,
      "FileCacheOptions": {
        "TtlSeconds": 10  //在第一次請求在閘道器快取10,在十秒內怎麼請求都是都閘道器的快取,不會請求例項,降低壓力,提升效能
      } //"快取"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://127.0.0.1:5003", //閘道器對外地址
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul" //由Consul提供服務發現
    }
  }
}

1》這個時候我們一直請求http://localhost:5003/TestOcelotConsul/user/get配置的閘道器地址,發現返回的都是5001介面的值,而在第10s後才會出現第二個例項的值,這個就是介面快取,這個快取是閘道器的快取,這個減少例項的壓力,提升效能

三,Polly之限流設定,如下配置(限流:就是在設定的時間內,允許請求的次數,如果達到次數,就返回設定的資訊)

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{url}", //服務地址--url變數
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/TestOcelotConsul/{url}", //閘道器地址--url變數
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "ServiceName": "TestConsulService", //consul服務名稱
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //輪詢      LeastConnection-最少連線數的伺服器   NoLoadBalance不負載均衡
      },
      "UseServiceDiscovery": true,
      "RateLimitOptions": {    //限流,限制了單位時間內的訪問量
        "ClientWhitelist": [], //白名單
        "EnableRateLimiting": true,
        "Period": "5m", //1s, 5m, 1h, 1d  
        "PeriodTimespan": 5, //多少秒之後客戶端可以重試
        "Limit": 5 //統計時間段內允許的最大請求數量
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://127.0.0.1:5003", //閘道器對外地址
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul" //由Consul提供服務發現
    },
    "RateLimitOptions": {
      "QuotaExceededMessage": "Too many requests!!!!!!!", // 當請求過載被截斷時返回的訊息,中文會出現亂碼
      "HttpStatusCode": 503 // 當請求過載被截斷時返回的http status,經測試過超過4位的狀態碼會出現異常
    }
  }
}

1》我們請求6次這個閘道器地址http://localhost:5003/TestOcelotConsul/user/get,就會出現我們配置的返回資訊,這裡設定邏輯是五分鐘內訪問五次,第六次提示限流,在第七次我們又可以訪問,這個意思不是五分鐘內一種只能訪問五次

我們注意RateLimitOptions這個設定的值,中文和狀態碼的長度

四,Polly之熔斷設定,如下設定(熔斷:單位時間內錯誤超過多少次,我們就直接停掉服務,不請求該服務)