久热香蕉在线视频免费_自_午夜福利院中文字幕_欧美精品在线视频中文_欧美人成在线播放网站色

  • <address id="i8pvn"><var id="i8pvn"><center id="i8pvn"></center></var></address>
  • <button id="i8pvn"><acronym id="i8pvn"></acronym></button>
  • 甘肅信息港

    Net Core使用Ocelot網(wǎng)關(guān)(一) -負(fù)載,限流,熔斷,Header轉(zhuǎn)換

    分享到:
     2020-03-29 09:51:45 來源: 閱讀:-G0

    1.什么是API網(wǎng)關(guān)

    API網(wǎng)關(guān)是微服務(wù)架構(gòu)中的唯一入口,它提供一個(gè)單獨(dú)且統(tǒng)一的API入口用于訪問內(nèi)部一個(gè)或多個(gè)API。它可以具有身份驗(yàn)證,監(jiān)控,負(fù)載均衡,緩存,請求分片與管理,靜態(tài)響應(yīng)處理等。API網(wǎng)關(guān)方式的核心要點(diǎn)是,所有的客戶端和消費(fèi)端都通過統(tǒng)一的網(wǎng)關(guān)接入微服務(wù),在網(wǎng)關(guān)層處理所有的非業(yè)務(wù)功能。通常,網(wǎng)關(guān)也是提供REST/HTTP的訪問API。服務(wù)端通過API-GW注冊和管理服務(wù)。

    Ocelot介紹

    Ocelot是用.net Core實(shí)現(xiàn)的一款開源的網(wǎng)關(guān),Ocelot其實(shí)就是一組按照順序排列的.net core中間件。它接受到請求之后用request builder構(gòu)建一個(gè)HttpRequestMessage對象并發(fā)送到下游服務(wù),當(dāng)下游請求返回到Ocelot管道時(shí)再由一個(gè)中間件將HttpRequestMessage映射到HttpResponse上返回客戶端。

    開源地址點(diǎn)擊這里

    使用Ocelot傻瓜式轉(zhuǎn)發(fā)

    新建三個(gè)項(xiàng)目webApi項(xiàng)目,分別命名為ApiGateway,Api_A,Api_B


      
    設(shè)置Api_A端口為5001,Api_B端口為5002,ApiGateway為5000

    為ApiGateway項(xiàng)目安裝Ocelot,在Nuget中搜索Ocelot或者直接使用Install-Package Ocelot進(jìn)行安裝。最新版本為13.8.x支持core3.0。

    在ApiGateway新建一個(gè)configuration.json配置文件。

    {
    &#34;ReRoutes&#34;: [
    {
    //上游Api請求格式
    &#34;UpstreamPathTemplate&#34;: &#34;/Api_A/{controller}/{action}&#34;,
    //網(wǎng)關(guān)轉(zhuǎn)發(fā)到下游格式
    &#34;DownstreamPathTemplate&#34;: &#34;/api/{controller}/{action}&#34;,
    //上下游支持請求方法
    &#34;UpstreamHttpMethod&#34;: [ &#34;GET&#34;, &#34;POST&#34;, &#34;DELETE&#34;, &#34;PUT&#34; ],
    &#34;DownstreamScheme&#34;: &#34;http&#34;,
    //下游服務(wù)配置
    &#34;DownstreamHostAndPorts&#34;: [
    {
    //下游地址
    &#34;Host&#34;: &#34;localhost&#34;,
    //下游端口號
    &#34;Port&#34;: 5001
    }
    ]
    },
    {
    &#34;UpstreamPathTemplate&#34;: &#34;/Api_B/{controller}/{action}&#34;,
    &#34;DownstreamPathTemplate&#34;: &#34;/api/{controller}/{action}&#34;,
    &#34;UpstreamHttpMethod&#34;: [ &#34;GET&#34;, &#34;POST&#34;, &#34;DELETE&#34;, &#34;PUT&#34; ],
    &#34;DownstreamScheme&#34;: &#34;http&#34;,
    &#34;DownstreamHostAndPorts&#34;: [
    {
    &#34;Host&#34;: &#34;localhost&#34;,
    &#34;Port&#34;: 5002
    }
    ]
    }
    ]
    }

    在Startup.cs 的ConfigureServices和Configure中配置Ocelot

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddOcelot(new ConfigurationBuilder()
    .AddJsonFile(&#34;configuration.json&#34;)
    .Build());
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    app.UseOcelot();
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    app.UseMvc();
    }

    分別為Api_A,和Api_B分別添加一個(gè)print接口。

    [HttpGet(&#34;print&#34;)]
    public string Print()
    {
    return &#34;Api_A&#34;;
    }
    [HttpGet(&#34;print&#34;)]
    public string Print()
    {
    return &#34;Api_B&#34;;
    }

    測試網(wǎng)關(guān)

    啟動三個(gè)項(xiàng)目,使用postman來調(diào)用網(wǎng)關(guān)

    訪問Api_A服務(wù)

    訪問Api_B服務(wù)

    可以看到網(wǎng)關(guān)通過接受到的請求的Url后通過我們的配置自動轉(zhuǎn)發(fā)到了我們想要訪問的服務(wù)。

    網(wǎng)關(guān)的負(fù)載均衡

    當(dāng)下游擁有多個(gè)節(jié)點(diǎn)的時(shí)候,我們可以用DownstreamHostAndPorts來配置

    {
    &#34;UpstreamPathTemplate&#34;: &#34;/Api_A/{controller}/{action}&#34;,
    &#34;DownstreamPathTemplate&#34;: &#34;/api/{controller}/{action}&#34;,
    &#34;DownstreamScheme&#34;: &#34;https&#34;,
    &#34;LoadBalancer&#34;: &#34;LeastConnection&#34;,
    &#34;UpstreamHttpMethod&#34;: [ &#34;GET&#34;, &#34;POST&#34;, &#34;DELETE&#34;, &#34;PUT&#34; ],
    &#34;DownstreamHostAndPorts&#34;: [
    {
    &#34;Host&#34;: &#34;127.0.0.1&#34;,
    &#34;Port&#34;: 5001,
    },
    {
    &#34;Host&#34;: &#34;127.00.1&#34;,
    &#34;Port&#34;: 5002,
    }
    ],
    }

    LoadBalancer是來決定負(fù)載的算法

    • LeastConnection:將請求發(fā)往最空閑的那個(gè)服務(wù)器
    • RoundRobin:輪流轉(zhuǎn)發(fā)
    • NoLoadBalance:總是發(fā)往第一個(gè)請求或者是服務(wù)發(fā)現(xiàn)

    限流

    限流可以防止上下游服務(wù)器因?yàn)檫^載而崩潰,可以使用RateLimitOptions來配置限流

    {
    &#34;RateLimitOptions&#34;: {
    &#34;ClientWhitelist&#34;: [“127.0.0.1”],
    &#34;EnableRateLimiting&#34;: true,
    &#34;Period&#34;: &#34;5s&#34;,
    &#34;PeriodTimespan&#34;: 1,
    &#34;Limit&#34;: 10
    }
    }
    • ClientWihteList:白名單,不受限流控制。
    • EnableRateLimiting:使用啟用限流。
    • Period:限流控制的時(shí)間段 1s, 5m, 1h, 1d。
    • PeroidTimeSpan:超過限流限制的次數(shù)后,需要等待重置的時(shí)間(單位是秒)。
    • Limit:在限流控制時(shí)間段內(nèi)最大訪問數(shù)。
      對于除了請求頭中ClientId=127.0.0.1的意外所有求情啟用限流,5秒該api最多10次,如果達(dá)到10次需要從第10次請求閉合后等待一秒進(jìn)行下一次訪問。

    超過限流后會返回429狀態(tài)碼,并在在返回頭(Response Header)的Retry-After屬性中返回等待重置時(shí)間。



    限流的默認(rèn)提示,code碼,和限制標(biāo)志都是可以自己配置的

     { 
    &#34;GlobalConfiguration&#34;: {
    &#34;BaseUrl&#34;:&#34;www.baidu.com&#34;,
    &#34;RateLimitOptions&#34;: {
    &#34;DisableRateLimitHeaders&#34;: false,
    &#34;QuotaExceededMessage&#34;: &#34;接口限流!&#34;,
    &#34;HttpStatusCode&#34;: 200,
    &#34;ClientIdHeader&#34;: &#34;ClientId&#34;
    }
    }

    • DisableRateLimitHeaders:是否顯示X-Rate-Limit和Retry-After頭
    • QuotaExceededMessage:提示信息
    • HttpStatusCode:狀態(tài)碼
    • ClientIdHeader:用來設(shè)別客戶請求頭,默認(rèn)為ClientId。
    • BaseUrl 網(wǎng)關(guān)暴露的的地址。

    熔斷

    熔斷是在下游服務(wù)故障或者請求無響應(yīng)的時(shí)候停止將請求轉(zhuǎn)發(fā)到下游服務(wù)。

     { 
    &#34;QoSOptions&#34;: {
    &#34;ExceptionsAllowedBeforeBreaking&#34;: 3,
    &#34;DurationOfBreak&#34;: 20,
    &#34;TimeoutValue&#34;: 5000
    }
    }
    • ExceptionsAllowedBeforeBreaking:允許多少個(gè)異常請求。
    • DurationOfBreak:熔斷的時(shí)間(秒)。
    • TimeoutValue:下游請求的處理時(shí)間超過多少則將請求設(shè)置為超時(shí)。

    緩存

    Ocelot可以對下游請求結(jié)果進(jìn)行緩存,主要是依賴于CacheManager來實(shí)現(xiàn)的。

    {
    &#34;FileCacheOptions&#34;: {
    &#34;TtlSeconds&#34;: 60,
    &#34;Region&#34;: &#34;key&#34;
    }
    }
    • TtlSeconds:緩存時(shí)間(秒)。
    • Region:緩存分區(qū)名
      我們可以調(diào)用Ocelot的API來移除某個(gè)區(qū)下面的緩存 。

    請求頭的轉(zhuǎn)化

    Ocelot允許在請求下游服務(wù)之前和之后轉(zhuǎn)換Header.目前Ocelot只支持查找和替換.

    如果們需要轉(zhuǎn)發(fā)給下游的Header重添加一個(gè)key/value

    {
    &#34;UpstreamHeaderTransform&#34;: {
    &#34;demo&#34;: &#34;xxxxxxx&#34;
    }
    }

    如果們需要在返回給客戶端的的Header中添加一個(gè)key/value

    {
    &#34;DownstreamHeaderTransform&#34;: {
    &#34;demo&#34;: &#34;xxxxxxx&#34;
    }
    }

    如果我們需要替換Heaher中某些值

    {
    //請求
    &#34;UpstreamHeaderTransform&#34;: {
    &#34;demo&#34;: &#34;a,b&#34;
    },
    //響應(yīng)
    &#34;DownstreamHeaderTransform&#34;:
    {
    &#34;demo&#34;: &#34;a,b&#34;
    }
    }

    語法是{find},{replace}, 利用b取代a

    在Header轉(zhuǎn)換中可以使用占位符

    • {BaseUrl} - 這個(gè)是Ocelot暴露在外的url. 例如http://localhost:5000/.
    • {DownstreamBaseUrl} - 這個(gè)是下游服務(wù)的基本url 例如http://localhost:5001/. 目前這個(gè)只在DownstreamHeaderTransform中起作用.
    • {TraceId} - 這個(gè)是Butterfly的跟蹤id.目前這個(gè)也只在DownstreamHeaderTransform中起作用

    如果您想將location頭返回給客戶端,可能需要將location更改為Ocelot的地址而不是下游服務(wù)地址。 Ocelot可以使用以下配置實(shí)現(xiàn)。

    {
    &#34;DownstreamHeaderTransform&#34;: {
    &#34;Location&#34;: &#34;{DownstreamBaseUrl},{BaseUrl}&#34;
    },
    }

    給Header中添加下游地址

    響應(yīng)的Header中已經(jīng)替換成BaseUrl了

    總結(jié)

    簡單的實(shí)現(xiàn)了通過網(wǎng)關(guān)來訪問api接口。ocelot功能遠(yuǎn)不止這些,之后會實(shí)現(xiàn)與IdentityServer的認(rèn)證鑒權(quán)。服務(wù)發(fā)現(xiàn)。

    原文地址:https://www.cnblogs.com/linhuiy/p/12029652.html

    推薦閱讀:iphone7與iphone8對比

    文章評價(jià)COMMENT

    還可以輸入2000個(gè)字

    暫無網(wǎng)友的評論

    意見反饋

    ×
    J