Ocelot gateway using the .Net Core

 

1. Project Environment

  .Net Core 2.2      Ocelot 13.5.2

 

2. Basic use

* Nuget installation Ocelot, Ocelot.Provider.Polly

* Modify Program.cs 

WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostingContext,builder) => {
      builder.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath).AddJsonFile("Ocelot.json");
}).UseStartup<Startup>();

* Modify Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOcelot().AddPolly(); 
        }

        
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }  

            app.UseOcelot().Wait(); 
        }

* New Ocelot.json, reads as follows

{
   " Reroutes " : [ 
    { 
      // - upstream Service Configuration 
      " UpstreamPathTemplate " : " /} {URL " ,
       " UpstreamHttpMethod " : [ " the Get " , " Post " , " of Put " , " the Delete " ], 

      // - downstream service configuration 
      " DownstreamPathTemplate " : " /} {URL " ,
       "DownstreamScheme": " HTTP " ,
       " DownstreamHostAndPorts " : [ 
        { 
          " Host " : " localhost " ,
           " Port " : 4001 
        }, 
        { 
          " Host " : " localhost " ,
           " Port " : 4002 
        } 
      ], 

      // - LoadBalancer will determine load balancing algorithms, are three values
       // the RoundRobin: take turns sending
       //LeastConnection: sends the requests to the least busy server
       // NoLoadBalance: always destined for a first service discovery request or 
      " LoadBalancerOptions " : {
         " the Type " : " of the RoundRobin " 
      }, 


      // ClientWhiteList: a string array, contained in the request header ClientId = xxx unrestricted flow control requests, wherein the key may be modified ClientId, will be introduced behind, XXX represents the whitelist configuration.
      // "EnableRateLimiting: Boolean value, whether to enable the limit, only to true, the configuration takes effect." "Period: limiting the time period control, you can enter",
       // "1S (1 Miao), 1m (1 minute) , (1 hour), 1D (1 day) IH similar values, and so on "." PeriodTimespan:. after limiting the number exceeds the current limit, it takes time to wait for reset (in seconds) ",
       // " limit : the largest number in the period of time that can be accessed "." 

      Upper sentence describe configuration: In addition to the request header contains the request ClientId = myclient enable restrictor, which is accessed at most twice api 1 minute, if the secondary has reached the end of the 2nd starting request 30 a visit to the latter seconds. ", "RateLimitOptions " : {
         " ClientWhitelist " : [],
         " EnableRateLimiting " : to false ,
         " Period " : " 1M " ,
         " PeriodTimespan " : 10 ,
         " Limit " : . 3 
      }, 


      // blown" ExceptionsAllowedBeforeBreaking: Allow abnormal frequency when Ocelot this is forwarded to a downstream continuous service when the number reaches the number of abnormal, Ocelot fuse will automatically no longer forwarded to the downstream service request for a time "
       // " DurationOfBreak: fusing time in ms (millisecond), for how long to this time is not forwarded downstream service request ",
      //"TimeoutValue: quality of service configuration item, the timeout time in ms (milliseconds), when the request is forwarded downstream Ocelot how long the service, the request is timed out automatically considered": null, 

      " QoSOptions " : {
         " ExceptionsAllowedBeforeBreaking " : . 5 ,
         " DurationOfBreak " : 10000 ,
         " TimeoutValue " : 3000 
      } 

    } 
  ], 
  " GlobalConfiguration " : {
     " BaseUrl " : " https://api.mybusiness.com " 
  } 
}

   

* Start the gateway and API site

 

Guess you like

Origin www.cnblogs.com/myshowtime/p/11653232.html