熟女丰满少妇精品一区二区,国产精品第一页综合在线,亚洲人成色777777精品,午夜性色福利免费视频在线播放

<object id="t794i"><abbr id="t794i"><center id="t794i"></center></abbr></object>

    <small id="t794i"><u id="t794i"></u></small>
    <address id="t794i"></address>
    <small id="t794i"><kbd id="t794i"></kbd></small>

      1. 甘肅信息港

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

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

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

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

        Ocelot介紹

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

        開源地址點擊這里

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

        新建三個項目webApi項目,分別命名為ApiGateway,Api_A,Api_B


          
        設置Api_A端口為5001,Api_B端口為5002,ApiGateway為5000

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

        在ApiGateway新建一個configuration.json配置文件。

        {
        &#34;ReRoutes&#34;: [
        {
        //上游Api請求格式
        &#34;UpstreamPathTemplate&#34;: &#34;/Api_A/{controller}/{action}&#34;,
        //網(wǎng)關轉(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;,
        //下游服務配置
        &#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分別添加一個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)關

        啟動三個項目,使用postman來調(diào)用網(wǎng)關

        訪問Api_A服務

        訪問Api_B服務

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

        網(wǎng)關的負載均衡

        當下游擁有多個節(jié)點的時候,我們可以用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是來決定負載的算法

        • LeastConnection:將請求發(fā)往最空閑的那個服務器
        • RoundRobin:輪流轉(zhuǎn)發(fā)
        • NoLoadBalance:總是發(fā)往第一個請求或者是服務發(fā)現(xià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:限流控制的時間段 1s, 5m, 1h, 1d。
        • PeroidTimeSpan:超過限流限制的次數(shù)后,需要等待重置的時間(單位是秒)。
        • Limit:在限流控制時間段內(nèi)最大訪問數(shù)。
          對于除了請求頭中ClientId=127.0.0.1的意外所有求情啟用限流,5秒該api最多10次,如果達到10次需要從第10次請求閉合后等待一秒進行下一次訪問。

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



        限流的默認提示,code碼,和限制標志都是可以自己配置的

         { 
        &#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:用來設別客戶請求頭,默認為ClientId。
        • BaseUrl 網(wǎng)關暴露的的地址。

        熔斷

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

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

        緩存

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

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

        請求頭的轉(zhuǎn)化

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

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

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

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

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

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

        {
        //請求
        &#34;UpstreamHeaderTransform&#34;: {
        &#34;demo&#34;: &#34;a,b&#34;
        },
        //響應
        &#34;DownstreamHeaderTransform&#34;:
        {
        &#34;demo&#34;: &#34;a,b&#34;
        }
        }

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

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

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

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

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

        給Header中添加下游地址

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

        總結(jié)

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

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

        推薦閱讀:iphone7與iphone8對比

        文章評價COMMENT

        還可以輸入2000個字

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

        意見反饋

        ×
        J