Hackforge Academy

Category: spring_boot

API Gateway in microservices

Published on 22 Aug 2026

Explanation

Routing is the main responsibility of an API Gateway. The gateway receives a client request, checks the request path, and forwards it to the correct microservice.

Code:

React Frontend
      │
      │ GET /api/users/10
      ▼
API Gateway :8080
      │
      │ Route
      ▼
User Service :8081

Explanation

Routes are configured using a route ID, destination URI, and predicate. The Path predicate tells the gateway which requests should be sent to a particular service.

Code:

spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: http://localhost:8081
          predicates:
            - Path=/api/users/**

Explanation

When the frontend calls /api/users/10, the gateway matches the /api/users/** pattern and forwards the request to the User Service.

Code:

Frontend Request

GET http://localhost:8080/api/users/10
                 │
                 ▼
          API Gateway
                 │
                 ▼
http://localhost:8081/api/users/10
                 │
                 ▼
           User Service

Explanation

A gateway can contain multiple routes for different microservices. Each route uses a different path to identify the target service.

Code:

API Gateway :8080
       │
       ├── /api/users/**
       │       ↓
       │   User Service :8081
       │
       ├── /api/orders/**
       │       ↓
       │   Order Service :8082
       │
       └── /api/payments/**
               ↓
           Payment Service :8083

Explanation

Spring Cloud Gateway can remove or rewrite parts of the request path before forwarding it to the microservice. This is useful when the external API URL and internal service URL are different.

Code:

Client:
GET /api/users/10

Gateway:
Rewrite /api/users/10
        ↓
/users/10

User Service:
GET /users/10

Explanation

Instead of hardcoding localhost URLs, the gateway can use Eureka Service Discovery. The gateway discovers the service using its registered name and can route requests to available instances.

Code:

React
  ↓
API Gateway
  ↓
Eureka Server
  ↓
USER-SERVICE
  ↓
User Service Instance

uri: lb://USER-SERVICE

🚀 Learn Spring Boot with real-world projects

💡 Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
⚛️

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community