Posted on: March 8, 2025 Posted by: rahulgite Comments: 0

Microservices architecture requires efficient communication and traffic management. API Gateways, Service Discovery, and Load Balancers play key roles in managing traffic, ensuring scalability, and maintaining service availability.


1. API Gateway

What is an API Gateway?

An API Gateway acts as a single entry point for client requests, managing authentication, routing, load balancing, caching, and monitoring. It simplifies microservices communication by providing a unified interface for multiple services.

Features of API Gateway:

  • Authentication & Authorization (JWT, OAuth2, API Keys)
  • Request Routing (Forward requests to appropriate microservices)
  • Rate Limiting (Prevent abuse by limiting requests per second)
  • Load Balancing (Distribute traffic evenly across instances)
  • Caching (Reduce redundant requests by caching responses)
  • Monitoring & Logging (Track API calls and errors)

Setting Up API Gateway with Spring Cloud Gateway

Step 1: Add Dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

Step 2: Configure Routes in application.yml

spring:
  cloud:
    gateway:
      routes:
        - id: order-service
          uri: http://localhost:8081/
          predicates:
            - Path=/orders/**
        - id: payment-service
          uri: http://localhost:8082/
          predicates:
            - Path=/payments/**

Step 3: Enable Gateway in Main Class

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

2. Service Discovery

What is Service Discovery?

Service Discovery enables microservices to dynamically register themselves and find other services without manual configuration.

Types of Service Discovery:

  • Client-Side Discovery (Eureka, Consul, Zookeeper)
  • Server-Side Discovery (Kubernetes Service Discovery, AWS ALB, Nginx)

Setting Up Eureka for Service Discovery

Step 1: Add Dependencies to Eureka Server

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Step 2: Configure Eureka Server in application.yml

server:
  port: 8761

spring:
  application:
    name: eureka-server

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

Step 3: Enable Eureka Server

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Step 4: Register Microservices with Eureka

Add the following dependency to each microservice:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Step 5: Configure Microservice in application.yml

spring:
  application:
    name: order-service

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

Step 6: Enable Discovery Client in Microservices

@EnableDiscoveryClient
@SpringBootApplication
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
}

3. Load Balancer

What is a Load Balancer?

A Load Balancer distributes incoming network traffic across multiple instances of a service to ensure high availability and reliability.

Types of Load Balancers:

  • Software Load Balancers (Spring Cloud LoadBalancer, Ribbon, Nginx, HAProxy)
  • Hardware Load Balancers (F5, Citrix)
  • Cloud-Based Load Balancers (AWS Elastic Load Balancer, Google Cloud Load Balancer)

Setting Up Spring Cloud Load Balancer

Step 1: Add Dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

Step 2: Use @LoadBalanced with RestTemplate

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Step 3: Call Service Using Load Balanced URL

@Autowired
private RestTemplate restTemplate;

public Order getOrder(Long id) {
    return restTemplate.getForObject("http://order-service/orders/" + id, Order.class);
}

4. Circuit Breaker

What is a Circuit Breaker?

A Circuit Breaker prevents system failure by detecting failures and temporarily blocking requests to failing services. It helps improve system resilience by allowing services to recover before resuming requests.

Setting Up Resilience4J Circuit Breaker in Spring Boot

Step 1: Add Dependencies

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
</dependency>

Step 2: Configure Circuit Breaker in application.yml

resilience4j:
  circuitbreaker:
    instances:
      orderService:
        failureRateThreshold: 50
        waitDurationInOpenState: 5000ms

Step 3: Apply Circuit Breaker in Service Layer

@Service
public class OrderService {
    @Autowired
    private RestTemplate restTemplate;

    @CircuitBreaker(name = "orderService", fallbackMethod = "fallbackOrder")
    public Order getOrder(Long id) {
        return restTemplate.getForObject("http://order-service/orders/" + id, Order.class);
    }

    public Order fallbackOrder(Long id, Throwable t) {
        return new Order(id, "Fallback Order", 0);
    }
}

Summary of Key Components

ComponentPurpose
API GatewayManages traffic, authentication, and routing
Service DiscoveryDynamically registers and discovers services
Load BalancerDistributes traffic across multiple instances
Circuit BreakerPrevents cascading failures in microservices

Annotations Summary

AnnotationPurpose
@EnableEurekaServerEnables Eureka server for service registration
@EnableDiscoveryClientEnables microservice to register with Eureka
@EnableCircuitBreakerEnables circuit breaker for resilience
@LoadBalancedEnables client-side load balancing
@CircuitBreakerProtects against failures and provides fallback

This guide provides an overview and setup instructions for API Gateway, Service Discovery, Load Balancer, and Circuit Breaker in a microservices architecture.

Leave a Comment