Microsoft > .Net Core >> Ocelot API Gateway Views : 6667
Ocelot Load Balancer
Rate This Article :

Load Balancing with Ocelot API Gateway

Aim

To do a load balancing setup using Ocelot gateway.

Details

This will be a basic setup for load balancing with ocelot. I will be using the RoundRobin type for this sample. The sample contains 3 projects 

1.       Ocelot gateway

2.      Microservice 1

3.      Microservice 2 (In real-time we will have same Microservice 1 in a different port or URL)

High level steps

1.       Create 2 Microservice replicate same api

2.      Create a Gateway api and configure routes and load balancing

Detailed Steps


      1. Creating Microservice

  Add a new core web application with web api template





Add a controller and create a getMethod

[HttpGet]

        public string Get()

        {

            return "Server1";

        }

Repeat the same steps to create Microservice 2 with return value as “Server2”


      2. Create and configure Ocelot

  Add a new core web application with web api template





Install ocelot from NuGet package manager, version to be used – 16.01



Add a json file to the project and name it as ocelot.json and configure the downstream and upstream routes in the json file. For downstream hosts we will specify both the Microservice 1 & 2 like below.


Next we need to set up the Load balancer options in the json file. See the above for reference

"LoadBalancerOptions": {

        "Type": "RoundRobin"

 }

 

Types supported for Load Balancing by Ocelot

  • ·         LeastConnection - tracks which services are dealing with requests and sends new requests to service with least existing requests. The   algorythm state is not distributed across a cluster of Ocelot’s.
  • ·         RoundRobin - loops through available services and sends requests. The algorythm state is not distributed across a cluster of Ocelot’s.
  • ·         NoLoadBalancer - takes the first available service from config or service discovery.
  • ·         CookieStickySessions - uses a cookie to stick all requests to a specific server.

And also custom load balancing is also supported

 

Now configuring the startup & program.cs for Ocelot

In program.cs edit the CreateHostBuilder method to below



In startup.cs edit the ConfigureServices & Configure methods like below



Change the Configure to async method and this to last

await app.UseOcelot();