Chapter 10.3☕ 16 min read

API Gateway & Routing

If you have 10 microservices, should your Angular frontend call 10 different IP addresses? No. That is a security nightmare and causes CORS issues. You need a <strong>API Gateway</strong> — a single front door for all clients.

01The Concept: Reverse Proxy & Routing

The HITEC City Main Security Gate Analogy:

Imagine an IT park with 10 different company buildings. Visitors don't sneak through the back door of Building 4. Everyone must enter through the Main Security Gate of the IT park. The guard checks your ID (Authentication), and says, "You want to go to Company A? Take the first left."

Spring Cloud Gateway is that main security gate. It is a single entry point (e.g., localhost:8080). It routes requests to the correct microservice, handles CORS, and can validate JWT tokens before the request even reaches the microservice.

02Technical Explanation
  1. Spring Cloud Gateway: The modern, reactive replacement for the dead Netflix Zuul.
  2. Route: A rule that says "If URL starts with /api/users, send it to user-service".
  3. Predicate: The condition (e.g., Path=/api/users/**).
  4. Filter: Things you do to the request before forwarding (e.g., add a header, check JWT).
  5. DiscoveryClient: The Gateway can ask Eureka for IPs automatically, so you don't hardcode them.
03Full Working Code: The Gateway Configuration

1. pom.xml

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

2. application.yml (The Magic Routing Rules)

server:
port: 8080 # The single front door

spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: user-service-route
uri: lb://USER-SERVICE # 'lb' means use LoadBalancer with Eureka
predicates:
- Path=/api/users/** # If URL starts with /api/users
filters:
- RewritePath=/api/users/(?<segment>.*), /${segment} # Strip /api/users before sending

- id: payment-service-route
uri: lb://PAYMENT-SERVICE
predicates:
- Path=/api/payments/**

If a browser calls http://localhost:8080/api/users/1, the Gateway sees the path, looks up USER-SERVICE in Eureka, and forwards the request to that microservice.

04Why It Matters

Interview Question: "What is the difference between Spring Cloud Gateway and Netflix Zuul?"

Answer: Zuul 1 was built on Servlet (blocking I/O) and is completely dead. Spring Cloud Gateway is built on Spring WebFlux (non-blocking, reactive). It can handle thousands of concurrent connections with very few threads, making it much faster.

05Interview Note

Enterprise Note: The API Gateway is the perfect place to implement Rate Limiting (preventing a user from making more than 100 API calls per minute) using Redis. You can also validate JWTs here, so individual microservices don't need to know about security.

Key Takeaways

  • ✅ Spring Cloud Gateway is the modern, reactive replacement for Netflix Zuul
  • ✅ Routes pair predicates (conditions) with URI targets (where to forward)
  • ✅ The lb:// prefix tells Gateway to discover service IPs dynamically via Eureka
  • ✅ API Gateway is ideal for cross-cutting concerns like rate limiting and JWT validation