Load Balancing, API Gateway, Service Discovery and Monitoring in Microservices using Ribbon, Spring Cloud gateway, Eureka and Spring Admin server

Sudha Subramaniam
5 min readJan 17, 2022

The diagram explains services used as part of this post.

Ribbon

Ribbon is mainly used for client side load balancing. When one instance of micro services is not able to handle the load then there is need to increase the instance of that service to handle more load. This is called horizontal scaling. So when a micro service have more than one instance then there is a mechanism needed to route the traffic among the instances. Ribbon does that job. That is to leverage this horizontal scaling, Load balancers are employed.

Dependency needed

Ribbon Configuration

Rule -this component is used to specify type of load balancing rule, the available rule implementations are AbstractLoadBalancerRule, AvailabilityFilteringRule, ClientConfigEnabledRoundRobinRule, RandomRule, ResponseTimeWeightedRule, RetryRule, RoundRobinRule, WeightedResponseTimeRule, ZoneAvoidanceRule.

Ping- this component is used to check the server/instance availability before sending request to a particular instance or server. The implementations are bstractLoadBalancerPing, DummyPing, NoOpPing, PingConstant,PingUrl

Serverlist- this component is used to specify the number of available instances/servers. This list can be dynamic or static. In case Eureka is used, then this configuration is not needed.

Enable the ribbon client using the annotation @RibbinClient and specify the name of the service which we want the ribbon to distribute the load as like below

application.properties of ribbon service

As we can see below 2 instance of “loadbalencingappservice” running at random port

loadbalencingappservice- Controller methods are shown below

when hitting rest endpoint “/backend” and “/get-InstanceId” using cloud gateway below are the output based on ribbon load balancing

Eureka Server

Eureka Server acts as Service Discovery which maintains the list of all the microservices as KEY-VALUE pair. Key is serviceId and value is the instance information (host, port). Also when a new instance is started with Eureka client it automatically registered with Eureka server.

Eureka helps client to invoke micro service endpoints without hardcoding host and port.

Dependency needed

@EnableEurekaServer annotation is used to make a particular service as Eureka server.

Spring Cloud Gateway

Spring cloud Gateway used to hide multiple micro services behind single façade. And its used to route requests to different microservices.

Below are components

Route- Gateway Handler mapping is used to determine which route to map for the given request. Spring Cloud Gateway matches routes using Spring WebFlux HandlerMapping infrastructure.

Predicate- used to match HTTP requests

Filter- its a normal filter pre ,post

Dependency needed

Configuration

RabbitMQ

Rabbit MQ is message broker which helps in achieving asynchronous communication between micro services. AMQP is core protocol of Rabbit MQ.

In Rabbit MQ message will get send to exchange and exchange is reasonable to send messages to a particular Queue.

Steps involved in using Rabbit MQ:

  1. Install Rabbit MQ
  2. Create exchange

2. Create Queue

3. Bind exchange and queue with routing key

Routing key- this key is used by an exchange to decide where to route the message.

To use Rabbit MQ we need to install the same.

Below command is used to install Rabbit MQ on docker

Once the installation is completed then verify the installation using below url with default username and password (we can change the default credentials )

Then create a service with below dependency to send/publish/produce message to RabbitMQ.

Dependency needed

application.properties

Below code snippet will create queue with name test.queue and exchange with name test.exchange and to bind these together test.routingkey will be used.

Queue configuration

Publish message to Queue(Sender/Publisher)

invoke producer using gateway url

After publishing message to Rabbit MQ , we can see message count as below before consuming

Micro services Monitoring with Spring Admin server

Monitoring deployed micro services is more important Spring Admin Server helps in monitoring.

Dependency needed

include annotation @EnableAdminServer to enable admin monitoring

--

--