Efficient Load Balancing and Metrics Monitoring in Spring Cloud Microservices

In Spring Microservices, services are often decentralized and distributed across multiple nodes. This architecture can enhance resilience and scalability but also introduces challenges such as efficient request distributions and performance monitoring. Load balancing and metrics monitoring are essential for the practices that address these challenges. It can ensure that the requests are evenly distributed among available service instances and that the health and performance of the services are constantly overserved.

Spring Cloud LoadBalancer can provide a simple yet powerful way to implement client-side load balancing in the microservices. It can replace the traditional Netflix Ribbon.

  • Load Balancing: It is a method of distributing network traffic across multiple servers. This ensures that no single server is carrying too much demand. By the spreading the load and it can enhances the responsiveness and availability of the applications.
  • Metrics Monitoring: It can involve tracking application metrics like the request count, response times, error times, and system health which are vital for diagnosing the issues and ensuring that the microservices are performing optimally.

Tools in Spring Cloud:

  • Spring Cloud LoadBalancer: The client side load balancer that can provides the control over the HTTP and TCP traffic between the client and server.
  • Spring Boot Actuator: It can provides the built in the endpoints for the monitoring and interacting with the microservices.
  • Spring Cloud Sleuth: It can used to adds the context to logs and making it easier to the trace requests through the microservices architecture.
  • Micrometer: It can be used to facilitates the metrics collection and is integrated with the external monitoring systems like Prometheus and Grafana.

Key Features:

  • Integration with the Spring Cloud Discovery Client.
  • Support for the Round Ribbon and weighted algorithms.
  • Health checks and server stats.

Key Terminologies:

  • Microservices: This architecture breaks down the monolithic application into the smaller, independent parts that communicate the over well defined APIs. Each service is the responsible for the specific business function and can be developed, deployed and scaled independently.
  • Spring Boot: The convention over configuration solution from the Spring ecosystem, aimed at the simplifying the development of the new Spring application through the various operational and development features such as the embedded servers.
  • Spring Cloud: It can be used to building some of the common patterns in the distributed systems like configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy and control bus.

Implementation of Load Balancing and Metrics Monitoring in Spring Cloud Microservices

Below are the implementation steps of Load Balancing and Metrics Monitoring in Spring Cloud Microservices.

Step 1: Create a spring project using spring initializer, on creating the project add the below dependencies.

Dependencies:

  • Spring Web
  • Spring Dev Tools
  • Lombok
  • Spring Eureka Client
  • Spring Cloud Routing

After creating the Spring project, the file structure will be like the image below.


Step 2: Open the application.properties file and rename it to application.yml. Then, add the following YAML code for configuring the server port, Eureka client, and API routing:

server:
servlet:
context-path: /
port: 9056

spring:
application:
name: API-GATEWAY
profiles:
active:
- default
cloud:
gateway:
default-filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
routes:
- id: SERVICE-1
uri: lb://service-1
predicates:
- Path= /**
- id: SERVICE-2
uri: lb://service-2
predicates:
- Path= /**


eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:9099/eureka
instance:
prefer-ip-address: true


Step 3: In the main class, include @EnableEurekaClient annotation to activate the Eureka server functionality.

Java
package org.example.apigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }

}


Step 4: Run the application

Once the Spring project is completed and successfully runs as a Spring application, it will start at port 9056.


Set up the Eureka-server Configuration

Step 1: Create a Spring project using Spring Initializr, and include the following dependencies:

Dependencies:

  • Spring Web
  • Eureka Server
  • Spring Dev Tools
  • Lombok

After creating the Spring project, the file structure will be like the image below.


Step 2: Open the application.properties file and add the following code to configure the server port and Eureka server settings for the project.

spring.application.name=EurekaServerService
server.port=9099
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone =http://${eureka.instance.hostname}:${server.port}/eureka


Step 3: In the main class, include @EnableEurekaServer annotation to activate the Eureka server functionality.

Java
package org.example.eurekaserverservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerServiceApplication.class, args);
    }

}


Step 4: Once the Spring project is completed and run as a Spring application successfully, it will start at port 9099.


Create the Service-1 microservice

Step 1: Create the Spring project using Spring Initializr. When creating the project, add the following dependencies:

Dependencies:

  • Spring Web
  • Eureka Server Client
  • Spring Dev Tools
  • Lombok
  • Spring Actuator

After creating the Spring project, the file structure will look like the image below.


Step 2: Open the application.properties file and insert the following code to configure the server port and Eureka client settings for the project.

spring.application.name=service-1
server.servlet.context-path=/service1
server.port= 8081
management.endpoints.web.exposure.include=health,info,metrics
eureka.instance.prefer-ip-address=true
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone= http://localhost:9099/eureka


Step 3: Open the main class and create the new REST API endpoint using the @RestController of the project.

Go to src > org.example.service1 > Service1Application and put the below code.

Java
package org.example.service1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Service1Application {

    @GetMapping("/message")
    public String getMessage() {
        return "Hello from Service A";
    }

    public static void main(String[] args) {
        SpringApplication.run(Service1Application.class, args);
    }
}


Step 4: Once the Spring project is completed, run it as a Spring application and it will start at port 8081.


Create the Service-2 Microservice

Step 1: Create the Spring project using Spring Initializr. When creating the project, add the following dependencies:

Dependencies:

  • Spring Web
  • Eureka Server Client
  • Spring Dev Tools
  • Lombok
  • Spring Actuator

After creating the Spring project, the file structure will be like the image below.


Step 2: Open the application.properties file and insert the following code to configure the server port and Eureka client settings for the project.

spring.application.name=service-2

server.port= 8082
server.servlet.context-path=/service2
management.endpoints.web.exposure.include=health,info,metrics
eureka.instance.prefer-ip-address=true
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone= http://localhost:9099/eureka


Step 3: Open the main class and create the new REST API endpoint using the @RestController of the project.

Go to src > org.example.service2 > Service2Application and put the below code.

Java
package org.example.service2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Service2Application {

    @GetMapping("/message")
    public String getMessage() {
        return "Hello from Service B";
    }


    public static void main(String[] args) {
        SpringApplication.run(Service2Application.class, args);
    }

}


Step 4: Once the Spring project is completed, run it as a Spring application and it will start at port 8082.


Eureka-Server Dashboard:

Open the below URL into the browser

http://localhost:9099

Output:


API Load balancing Service-1 Endpoint:

http://localhost:9056/service1/message

Output:


API Load balancing Service-2 Endpoint:

http://localhost:9056/service2/message

Output:


Service-1 microservice health endpoint:

http://localhost:8081/actuator/health


Output:


Service-2 microservice health endpoint:

http://localhost:8082/actuator/health

Output:


Service-1 microservice Metric Endpoint:

http://localhost:8081/actuator/metrics

Output:


Service-2 microservice Metric Endpoint:

http://localhost:8082/actuator/metrics

Output

Conclusion

Using the Spring Cloud for load balancing and metrics monitoring in the microservices architecture ensure that the applications are robust, responsive and maintainable. By the following the above steps, we can implement the scalable microservices application with the effective load management and detailed observability.