Introduction
In the previous articles of this series, we discussed how to build the API Gateway in ASP.NET Core.
And in this article, we will discuss the Load Balancing module of Ocelot.
If you want to look at the previous articles of this series, please visit the links given below.
What is Load Balancing?
Load balancing improves the distribution of workloads across multiple computing resources, such as computers, a computer cluster, network links, central processing units, or disk drives. Load balancing aims to optimize resource use, maximize throughput, minimize response time, and avoid overload of any single resource.
For our API services, Ocelot's load balancer is a software program that is listening on the port where external clients connect to access services.
I will use version 5.0.0 of Ocelot to show you this feature.
Preparation
We need to create three projects and ensure that they can run well.
Here I use two API services as one API service to show, which may make you understand more clearly.
In APIServicesA project, it will return From APIServiceA.
- [HttpGet]
- public string Get()
- {
- return "From APIServiceA";
- }
In APIServicesB project, it will return From APIServiceB.
- [HttpGet]
- public string Get()
- {
- return "From APIServiceB";
- }
After running them up, we will add configuration of Load Balancing to them.
Configure Load Balancing
We need to pay attention to node DownstreamHostAndPorts and node LoadBalancer.
DownstreamHostAndPorts is an array that contains all services' host and port. In this sample, it will contain both APIServiceA and APIServiceB.
The value of LoadBalancer node is a string which specifies the scheduling algorithms.
At this time, Ocelot support two scheduling algorithms, one is RoundRobin, the other one is LeastConnection.
Here is a sample shows you how to configure.
- {
- "DownstreamPathTemplate": "/api/values",
- "DownstreamScheme": "http",
- "DownstreamHostAndPorts": [
- {
- "Host": "localhost",
- "Port": 9001
- },
- {
- "Host": "localhost",
- "Port": 9002
- }
- ],
- "UpstreamPathTemplate": "/",
- "LoadBalancer": "RoundRobin",
-
- "UpstreamHttpMethod": [ "Get" ]
- }
-
The above configuration means that when we visit http://localhost:9000/, we will get the result from http://localhost:9001/api/values or http://localhost:9002/api/values, which is based on the RoundRobin load balancer.
Let's take a look at the result:
As you can see, there is an alternation of
From APIServiceB and
From APIServiceA! It means that load balancing is working now, Ocelot forwards the requests to the downstream services.
Here is the source code you can find on my GitHub page.
Summary
This article introduced the basic usage of load balancing in Ocelot. It also can be used with service discovery. I will show you more about this later.
I hope this helps you!