API Management 4 minutes

Create an API with load balanced endpoints

Rob Blaauboer
Integration Consultant & WSO2 Trainer
Blog - API load balance
Scroll

WSO2 API Manager has the capabilities to work with failover endpoints as well as load balanced endpoints. In this blog, we are going to create an API with the load balance endpoint from within the API Manager. We will use the WSO2 API Manager 3.2.0, which is the latest version of the WSO2 API Manager at the moment of writing this article. 

Installation of WSO2 API Manager

In one of our other articles, you will find information about the installation of WSO2 API Manager. The article takes you through the principal steps you need to take when installing the product. In case you are using a different product, or a different version number of the product, the steps described in this article are still relevant for you. I am sure you can pull it off.

In short: It is necessary for you to install Java first if you do not have this already. One of the supported versions is Java 8 / 121, or open JDK 11. After setting JAVA_HOME you can download the zip file with the API Manager and unpack it. This will work on both Windows as Linux / Mac OS. There are actually multiple options to install WSO2 products including installers, but these fall under the software license. The open source version can be downloaded as a zip file from Github 

WireMock to mock the APIs

To mock the APIs that we need for the failover, I am using WireMock. It deploys mock APIs on port 8080 by default. For your setup, you can have it running locally or on a server. You can find instructions on how to run WireMock in this article by my colleague Dusan Devic. It will also explain the mechanism of WireMock and where to place the json files.

Loadbalanced 1

This is the API1 (api1.json) definition for WireMock that responds with a simple message:

{
    "request": {
        "method": "GET",
        "url": "/api_1"
    },
    "response": {
        "status": 200,
        "body": "This is API 1",
        "fixedDelayMilliseconds": 0,
		"headers": {
            "Content-Type": "application/json"
        }
    }
}

Copy this API-definition to api2.json and api3.json and change the body to respectively show “This is API 2” and “This is API 3”. Do not forget to change the url (/api_1) as well to /api_2 and /api_3.

Create the API within the API Publisher

We are going to create an API with the load balancer endpoint from within the API Publisher UI. Start the API Manager Publisher as you normally would (I am assuming an out of the box install) with https://localhost:9443/publisher. Log in using the userid admin and password admin.

Loadbalanced 2

Let us start by creating a new API called “Loadbalanced”. Select the “Design New REST API” option from the menu in the API publisher. 

Loadbalanced 3

Fill in the details as shown in this screenshot with regards to the API definition: 

Loadbalanced 4

Press “Create” to create the API and you will be presented with this overview screen after creation.  

Loadbalanced 5

There are some more details we must provide. Let us open the “Design configurations” and fill in those details too. 

I have created a logo and uploaded it to the API on this page. You can use the default logo (a simple background and symbol) that the API Manager suggests or create your own. Fill in the other fields as shown in the image below. Of course, you can change them but changes to visibility and access control will impact the API. 

Loadbalanced 6

Press SAVE to save the changes.

Go to “Resources” and remove all the wildcards resources that were created by default apart from the resource with the GET HTTP verb and use URI pattern “/*”.

Loadbalanced 7

Press SAVE to save the changes. 

On the overview screen click on the endpoints and make the following changes: 

Add two load balance endpoints by copying the original endpoint and changing the suffix ‘1’ (api_1) to the numbers 2 and 3 as shown in the screenshot. 

Loadbalanced 8

Save again to store the changes. 

Loadbalanced 9

Publish the API in the lifecycle definition and go to the Devportal to see the API. There will be a link in the API Publisher.  

Loadbalanced 10

Subscribe to the API using the default application. 

Loadbalanced 11
Loadbalanced 12
Loadbalanced 13

Select PROD KEYS and generate Keys. 

Loadbalanced 14
Loadbalanced 15
Loadbalanced 16
Loadbalanced 17

Copy the token with the icon to the right (the paper icon). This is now in memory. Close the window.

We will use SoapUI to try out the API. 

Start SoapUI and create a new rest project. Use the http://localhost:8280/load/1.0 URI. 

Loadbalanced 18

When the project is created, click on the Auth button.

Loadbalanced 19

Select Oauth 2.0 from the dropdown list as the authorization.

Loadbalanced 20

Copy the token into the field Access Token.

Loadbalanced 21

Click on the green arrow.

Loadbalanced 22

Repeat this a number of times. See that the load balanced endpoint will round robin the calls.

Loadbalanced 23
Loadbalanced 24
Loadbalanced 25

Conclusion

If you want to have a load balanced setup you can do that from the API definition itself. The algorithm is a simple round robin setup that will cycle through the endpoints.