Microsoft > .Net Core >> Ocelot API Gateway Views : 5863
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();



Now to run the application

 

      1. Set all web project as start up



       2. Run the application and Access the URL from a browser - https://localhost:44378/gateway/WeatherForecast

 

You will get the response from 1st Microservice


Make the second request from another tab with same url, now the request will be routed to second server( 2nd microservice)



Note: Sample code is available to download, Its created in .net core 3.1



About Author
ezhilarasan.j
Total Posts 3
-
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
Ram
hi, I have two application servers where the REST API Web services are deployed. These webservices would be accessed via the Ocelot API Gateway. There is a load balancer already which is a hardware load balancer F5. WE would be deploying the Web Services in IIS on both the physical app server. We would like to know if the Ocelot has to be installed on both the App Servers where the web services are deployed in IIS on both server. How is this going to work. Where do i deploy the Ocelot API Gateway and the subsequent configuration changes. Need your inputs
  Privacy   Terms Of Use   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.