Product Version

Load Balance Microservices

Learning objectives

  • What are and how to create an API connection.

  • Add Round Robin load balance to an API connection.

  • Create a Route that uses the Load Balance API connection.

  • Test the Route to validate the Load Balance API connection accessing the microservices.

Prerequisites

  • Docker installed and the daemon is running on your computer.

  • Curl installed on your computer.

1. Run the demo Microservices

demo diagram

For this guide we are going to scale the movie microservice to three instances with a custom language configuration: English, Spanish and French.

Each client request to obtain the microservice Language Configuration is going to be Load balanced by the Tribestream API Gateway.

In order to run our demo microservices we need to open a terminal and execute the following command:

docker run -e LANG=ENGLISH -d -p 3333:9090 --name movie-api-en  tomitribedev/movie-api
docker run -e LANG=SPANISH -d -p 4444:9090 --name movie-api-es  tomitribedev/movie-api
docker run -e LANG=FRENCH -d -p 5555:9090 --name movie-api-fr  tomitribedev/movie-api

We can validate that our microservices are up and running by executing the following commands. Notice that each microservice returns in the payload it’s own Language Configuration.

curl  http://localhost:3333/movie-api/api/language
"ENGLISH"
curl  http://localhost:4444/movie-api/api/language
"SPANISH"
curl  http://localhost:5555/movie-api/api/language
"FRENCH"

2. Run Tribestream API Gateway (TAG)

We can reuse the TAG docker container we created from preivous guides by executing the following command:

docker start tag
  • If this is the first time you run TAG, open a terminal execute the following command according to your operating system:

    • For linux: docker run --net="host" -de LICENSE=accept --name tag tomitribe/tribestream-api-gateway tomitribe/tribestream-api-gateway

    • For OS X and Windows: docker run -de LICENSE=accept --name tag -p 8080:8080 tomitribe/tribestream-api-gateway

To see the TAG starting process log you can execute the following command:

docker logs -f tag

TAG is ready when you see the following message on the TAG log:

INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-bio-8080"]
INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-bio-8009"]
INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 18348 ms

To exit the log view, you need to type crtl+c on the terminal.

3. Login into TAG

Open a browser and navigate to: http://localhost:8080/tag

Login into the TAG dashboard using the following credentials: username: admin, password: admin. If this is not the first time you are login into TAG, you may be redirected straight to the Dashboard page.

login

4. Load Balance your microservices

4.1 Create an API Connection
api connection diagram

Connections is the term Tribestream API Gateway uses to describe and configure external resources like Databases, Ldap servers and API’s. The following instructions will provide you the steps need it to create and API connection to describe and configure the access to the three microservices we previously started.

From the TAG Dashboard page click on the Connections option to navigate to the Connections page, click the + button on the upper right side and select API Connection.

open new connections

Create a new API Connection with the following data according to your Operating System and then click the Save button.

4.2 Add Load Balancing to an API Connection

In the Load Balance API Connection detail page, click the button and select Add Load Balancing.

The Load Balancing section appears. To add the remaining two microservice endpoints location, click the the Host + button and add the two locations, then click the save button located at the top right corner of the screen:

4.3 Create a Route to reference the Load Balanced API Connection
route lb diagram

The mechanism used by the Tribestream API Gateway to orchestrate and secure the traffic to and from API endpoints is called Route.

For the purpose of this guide, you need to create a Route in order to use the Load Balancer created in the previous step via the API Connection.

From the Dashboard page, click the Route link and then from the Routes page click the + button on the upper right side and select` Mod_Rewrite Route`.

open new route

Create a new Route with the following data and click Save:

  • Name: Load Balancer Route

  • Mod_Rewrite: RewriteRule "^/?test-load-balancer$" "%{API:load balance api connection}/movie-api/api/language" [P,NE]

create route

5. Test the Load Balancer Route

test lb diagram

To successfully test the load balance microservice we can send a couple of request to the test-load-balancer endpoint that TAG now provides by the route we created. Notice that TAG is going to apply the mod_rewrite rule and will proxy the requests to the microservice /language endpoint.

To successfully test the load balance microservice you can execute the following command three times to see how the microservices are effectively balanced:

curl http://localhost:8080/test-load-balancer
"ENGLISH"
curl http://localhost:8080/test-load-balancer
"SPANISH"
curl http://localhost:8080/test-load-balancer
"FRENCH"

You can now shutdown one of the microservice by execute the following command:

docker stop movie-api-en

Run again three times and see how the microservices are effectively balanced:

curl http://localhost:8080/test-load-balancer
"SPANISH"
curl http://localhost:8080/test-load-balancer
"FRENCH"
curl http://localhost:8080/test-load-balancer
"SPANISH"

You can start again the movie-api-en microservice:

docker start movie-api-en

6. Disabling a specific microservice via TAG Load Balancer UI

You can also deactivate one of the three microservices using the TAG UI. From the TAG Dashboard page click on the Connections option and click the load balance api connection we previously created.

disable one endpoint

Just like in the previous section, now you can run again three times the curl command making a request to http://localhost:8080/test-load-balancer and see how the two microservices are effectively balanced now that one of the three was deactivated via the TAG UI.

7. Stop Tribestream API Gateway

Since both the TAG and the microservices were created with a specific container name, you can now stop the containers from the command line with the following command.

Stopping TAG

docker stop tag

Stopping the microservices

docker stop movie-api-en
docker stop movie-api-es
docker stop movie-api-fr

Summary

Congratulations! in this module you learned about:

  • What are and how to create an API connection

  • Add Round Robin load balance to an API connection

  • Create a Route that uses the Load Balance API connection

  • Test the Route to validate the Load Balance API connection accessing the microservices.