.Net Core微服務入門全紀錄(五)——Ocelot-API網關(下)

前言

上一篇【.Net Core微服務入門全紀錄(四)——Ocelot-API網關(上)】已經完成了Ocelot網關的基本搭建,實現了服務入口的統一。固然,這只是API網關的一個最基本功能,它的進階功能還有不少不少。html

服務發現

首先須要解決的就是服務發現的問題,服務發現的優勢以前講過,就不說了。
上一篇中咱們的服務地址都是寫在ocelot.json配置文件裏,如今咱們須要結合以前的Consul來實現服務發現。git

  • 改造代碼:

首先NuGet安裝Ocelot.Provider.Consul
github

修改Startup.cs:json

public void ConfigureServices(IServiceCollection services)
        {
            //添加ocelot服務
            services.AddOcelot()
                .AddConsul();//添加consul支持
        }

修改ocelot.json配置:api

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/products",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/products",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "ProductService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    },
    {
      "DownstreamPathTemplate": "/orders",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/orders",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "OrderService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:9070",
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}

這個配置應該很好理解,就是把咱們上次的DownstreamHostAndPorts節點去掉了,而後增長了ServiceDiscoveryProvider服務發現相關配置。
注意,Ocelot除了支持Consul服務發現之外,還有Eureka也能夠,Eureka也是一個相似的註冊中心。緩存

好了,代碼修改就差很少了,下面運行程序測試一下:


客戶端正常運行。負載均衡

至此咱們就實現了服務註冊與發現和api網關的基本功能。接下來就要提到:服務治理ide

服務治理

其實服務治理也沒有一個很是明確的定義。它的做用簡單來講,就是幫助咱們更好的管理服務,提高服務的可用性。——緩存,限流,熔斷,鏈路追蹤 等等。。。都屬於經常使用的服務治理手段。
以前講的負載均衡,服務發現也能夠算是服務治理。微服務

  • 緩存:

在Ocelot中啓用緩存,須要NuGet安裝一下Ocelot.Cache.CacheManager
測試

修改Startup.cs中的ConfigureServices()方法:

//添加ocelot服務
services.AddOcelot()
    //添加consul支持
    .AddConsul()
    //添加緩存
    .AddCacheManager(x =>
    {
        x.WithDictionaryHandle();
    });

修改ocelot.json配置文件:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/products",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/products",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "ProductService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      }
    },
    {
      "DownstreamPathTemplate": "/orders",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/orders",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "OrderService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:9070",
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}

在Routes路由配置中增長了FileCacheOptions。TtlSeconds表明緩存的過時時間,Region表明緩衝區名稱,這個咱們目前用不到。

好了,代碼修改完須要編譯重啓一下網關項目,而後打開客戶端網站測試一下:

能夠看到,5秒以內的請求都是一樣的緩存數據。Ocelot也支持自定義緩存。

  • 限流:

限流就是限制客戶端必定時間內的請求次數。
繼續修改配置:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/products",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/products",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "ProductService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      },
      "RateLimitOptions": {
        "ClientWhitelist": [ "SuperClient" ],
        "EnableRateLimiting": true,
        "Period": "5s",
        "PeriodTimespan": 2,
        "Limit": 1
      }
    },
    {
      "DownstreamPathTemplate": "/orders",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/orders",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "OrderService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      },
      "RateLimitOptions": {
        "ClientWhitelist": [ "SuperClient" ],
        "EnableRateLimiting": true,
        "Period": "5s",
        "PeriodTimespan": 2,
        "Limit": 2
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:9070",
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    },
    "RateLimitOptions": {
      "DisableRateLimitHeaders": false,
      "QuotaExceededMessage": "too many requests...",
      "HttpStatusCode": 999,
      "ClientIdHeader": "Test"
    }
  }
}

在Routes路由配置中增長了RateLimitOptions。ClientWhitelist表明客戶端白名單,在白名單中的客戶端能夠不受限流的影響;EnableRateLimiting表明是否限流;Period表明限流的單位時間,例如1s,5m,1h,1d等;PeriodTimespan表明客戶端達到請求上限多少秒後能夠重試;Limit表明客戶端在定義的時間內能夠發出的最大請求數。
在GlobalConfiguration配置中也增長了RateLimitOptions。DisableRateLimitHeaders表明是否禁用X-Rate-Limit和Retry-After標頭(請求達到上限時response header中的限制數和多少秒後能重試);QuotaExceededMessage:表明請求達到上限時返回給客戶端的消息;HttpStatusCode:表明請求達到上限時返回給客戶端的HTTP狀態代碼。ClientIdHeader能夠容許自定義用於標識客戶端的標頭。默認狀況下爲「 ClientId」。
最重要的就是Period,PeriodTimespan,Limit這幾個配置。

從新編譯啓動看一下效果:

  • 超時/熔斷

超時很好理解,就是網關請求服務時可容忍的最長響應時間。熔斷的意思就是當請求某個服務的異常次數達到必定量時,那麼網關在必定時間內就再也不對這個服務發起請求了,直接熔斷。
Ocelot中啓用 超時/熔斷 須要NuGet安裝一下Ocelot.Provider.Polly

修改Startup.cs中的ConfigureServices()方法:

//添加ocelot服務
services.AddOcelot()
    //添加consul支持
    .AddConsul()
    //添加緩存
    .AddCacheManager(x =>
    {
        x.WithDictionaryHandle();
    })
    //添加Polly
    .AddPolly();

一樣的在ocelot.json路由配置中增長QoSOptions:

"QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 10000,
        "TimeoutValue": 5000
      }

ExceptionsAllowedBeforeBreaking表明發生錯誤的次數,DurationOfBreak表明熔斷時間,TimeoutValue表明超時時間。
以上的配置意思就是當服務發生3次錯誤時,那麼就熔斷10秒,期間客戶端的請求直接返回錯誤,10秒以後恢復。
這個不太好模擬,就不演示了,應該也挺好理解的。

。。。。。。

關於服務治理的學問還有不少,不繼續了。。。就到此爲止吧。
想要更深刻了解Ocelot的,請看官網:https://ocelot.readthedocs.io/en/latest/
或者看源碼:https://github.com/ThreeMammals/Ocelot

下一篇準備說一下:事件總線。

代碼放在:https://github.com/xiajingren/NetCoreMicroserviceDemo

未完待續...

相關文章
相關標籤/搜索