当前位置:网站首页>(4) ASP.NET Core 3.1 Ocelot load balancing

(4) ASP.NET Core 3.1 Ocelot load balancing

2020-11-10 12:47:32 Dark jejunum

1. Load balancing

Ocelot Load balancing can be achieved in the available downstream services of each route , This allows us to select downstream services to process requests more efficiently . Load balancing type :
●LeastConnection: Decide which service to process the new request based on the number of requests being processed by the service , Sending a new request to a service with the least number of existing requests to process . The algorithm state is not distributed in Ocelot In the cluster .
●RoundRobin: Traverse available services and send requests . The algorithm state is not distributed in Ocelot In the cluster .
●NoLoadBalancer: Get the first available service from configuration or service discovery to process new requests .
●CookieStickySessions: By using Cookie, Ensure that specific requests can be assigned to specific services for processing .
stay Ocelot Load balancing project Example , We're routing LoadBalancerOptions Configuration options select load balancing type :

{
  "Routes": [
    {
      // Downstream routing service address 
      "DownstreamPathTemplate": "/api/values",
      // Downstream service address access protocol type http perhaps https
      "DownstreamScheme": "http",
      // Host and port of downstream services 
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9001
        },
        {
          "Host": "localhost",
          "Port": 9002
        }
      ],
      // Upstream service address , That is, the real access address of downstream services 
      "UpstreamPathTemplate": "/",
      // Load balancing type : polling 
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      // Upstream services HTTP Request mode , for example Get、Post
      "UpstreamHttpMethod": [ "Get" ]
    }
  ]
}

When a new request passes through the gateway ,Ocelot Will be based on LoadBalancerOptions Configure load balancing types to distribute to specific downstream Services .

2. Service discovery

The following shows how to use service discovery to set up routing :

{
    "DownstreamPathTemplate": "/api/posts/{postId}",
    "DownstreamScheme": "https",
    "UpstreamPathTemplate": "/posts/{postId}",
    "UpstreamHttpMethod": [ "Put" ],
    "ServiceName": "product",
    "LoadBalancerOptions": {
        "Type": "LeastConnection"
    }
}

After setting this option ,Ocelot Downstream hosts and ports will be found from the Service Discovery Provider , Load balancing requests are made in all available services . If you discover a provider from a service ( Consul ) Add and remove services from ,Ocelot Will stop calling the deleted service , And start calling the added service . When the follow-up learning service finds this knowledge point, it will explain it again .

3. Project presentations

3.1APIGateway project

The project passed LoadBalancerOptions Configuration options define service load balancing request mechanism , The type of load balancing used by the case project is RoundRobin, stay Program add to Ocelot The support code is as follows :

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
            //.UseStartup<Startup>()
            .UseUrls("http://*:9000")
            .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                // add to Ocelot The configuration file 
.AddJsonFile("configuration.json")
                .AddEnvironmentVariables();
        })
        .ConfigureServices(s =>
        {
            // add to Ocelot service ;
            s.AddOcelot();
        })
        .Configure(a =>
        {
    // Use Ocelot
            a.UseOcelot().Wait();
        });

3.2APIServicesA and APIServicesB Downstream service projects

APIServicesA and APIServicesB Two new projects will be built GET Request method , The codes are as follows :

//APIServicesA
[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public string Get()
    {
        return "From APIServiceA";
    }
}
//APIServicesB
[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public string Get()
    {
        return "From APIServiceB";
    }        
}

3.3 Project operation

● adopt dotnet run Command to start APIGateway project ( Gateway layer )

dotnet run --project APIGateway Project path \APIGateway.csproj


● adopt dotnet run Command to start APIServicesA project

dotnet run --project APIServicesA Project path \APIServicesA.csproj


● adopt dotnet run Command to start APIServicesB project

dotnet run --project APIServicesB Project path \APIServicesB.csproj


View the results returned by polling distribution to downstream services through the browser :


Load balancing polling distribution of downstream services succeeded .

4. Custom load balancing

Ocelot Support custom load balancing methods . Custom load balancing classes need to inherit ILoadBalancer Interface class , Let's define a simple load balancing loop to output downstream services :

public class CustomLoadBalancer : ILoadBalancer
{
    private readonly Func<Task<List<Service>>> _services;
    private readonly object _lock = new object();
    private int _last;

    public CustomLoadBalancer(Func<Task<List<Service>>> services)
    {
        _services = services;
    }
    public async Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
    {
        var services = await _services();
        lock (_lock)
        {
            if (_last >= services.Count)
            {
                _last = 0;
            }
            var next = services[_last];
            _last++;
            return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
        }
    }
    public void Release(ServiceHostAndPort hostAndPort)
    {
    }
}

stay Ocelot Register this class in :

Func<IServiceProvider, DownstreamRoute, IServiceDiscoveryProvider, CustomLoadBalancer> loadBalancerFactoryFunc =
(serviceProvider, Route, serviceDiscoveryProvider) => new CustomLoadBalancer(serviceDiscoveryProvider.Get);
s.AddOcelot().AddCustomLoadBalancer(loadBalancerFactoryFunc);

Finally, in routing LoadBalancerOptions Change the configuration option to CustomLoadBalancer Custom load balancing class name :

"LoadBalancerOptions": {
  "Type": "CustomLoadBalancer"
}

Run the project debug to see the results :
The first request was distributed to APIServicesA project .

The second request was distributed to APIServicesB project .

From the above results , Custom load balancing succeeded .

reference :
Ocelot Official website

版权声明
本文为[Dark jejunum]所创,转载请带上原文链接,感谢