Gateway Ocelot

 

 

 

Ocelot as a gateway, there are many functions: load balancing, current limiting, fuse, caching ....

As users we just need to configure them in configuration.json, the configuration file on the official website has, is standing on the shoulders of giants Zhendi comfortable.

Use the following a few simple recording function

Step1: Nuget the download package Ocelot

 

 Step2: Add configuration.json files, configuration in the official website or their own online search

 

 

 Step3:services.AddOcelot();      app.UseOcelot();     

 Function 1: routing, gateway after setting request .good / {goodId} will turn and services / api / good / goodId

 

{
     " DownstreamPathTemplate " : " / API / Good / goodId {} " , // downstream routing template 
    " DownstreamScheme " : " HTTP " , // manner downstream of the routing request 
    " DownstreamHostAndPorts " : [ // downstream port and routing Host 
            {
                 " the Host " : " localhost " ,
                 " Port " : 1001 , 
            } 
        ], 
    "UpstreamPathTemplate " : " / Good / goodId {} " , // template upstream route request 
    " UpstreamHttpMethod " : [ " of Put " , " the Delete " ] // manner upstream of the routing request 
}

Function two: + Polly current-limiting fuse

 

  services.AddOcelot().AddPolly();

Limiting

 " RateLimitOptions " : {
           " ClientWhitelist " : [], // white list 
          " EnableRateLimiting " : to true , // whether to enable current limiting 
          " Period " : " 5m " , // 1S, 5m, 1H, 1D 
          " PeriodTimespan " : 20 , // after how many seconds a client can retry 
          " Limit " : 5 , // allow statistical period of maximum number of requests 

          // we can also make the following configuration in GlobalConfiguration 
          "DisableRateLimitHeaders " : to false , // the Http head X-Rate-Limit, and whether to disable the Retry-After 
          " QuotaExceededMessage " : " Too MANY !!! " , // message when the request is truncated returned overload 
          " the HttpStatusCode " : 666 , / / when the request is truncated overload Status returned HTTP 
          " ClientIdHeader " : " the Test "  // is used to identify the client request header, the default is the ClientId 
        }

Fuse

" QoSOptions " : {
         " ExceptionsAllowedBeforeBreaking " : 0 , // allow the number of malformed request 
        " DurationOfBreak " : 0 , // fusing time, in seconds 
        " TimeoutValue " : 0  // if the processing time exceeds the number of seconds downstream from the request rotatably request to time-out 
      }

Three Functions: + Consul load balancing

 

  services.AddOcelot().AddConsul();

 " Reroutes " : [ 
    { 
      // universal template: "/ {url}"; the lowest priority universal template, as long as there are other routes template, template other routes will be honored 
      " UpstreamPathTemplate " : " / T / {url } " , // upstream request address template 
      " UpstreamHttpMethod " : [ @ upstream request mode 
        " the Get " ,
         " Post " 
      ], 
      " DownstreamPathTemplate " : " /} {URL " , // downstream jump address templates; user's request / post / 1 forwarded to localhost / API / post / 1 
      "DownstreamScheme": "http",
      "UseServiceDiscovery": true,
      "ServiceName": "shenqing",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
}]
 "GlobalConfiguration": {
    "BaseUrl": "http://127.0.0.1:6299",
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  }

 

Guess you like

Origin www.cnblogs.com/xingzhu-nan/p/12594369.html