Chapter 4: API Gateway with Spring Cloud Gateway
π― Chapter Objectivesβ
- Understand the role of an API Gateway
- Configure Spring Cloud Gateway
- Define routes and predicates
- Implement filters (pre/post)
- Configure rate limiting
- Integrate with Eureka for load balancing
1. Why an API Gateway?β
1.1 Without Gatewayβ
Problems:
- Client must know all services
- CORS to configure on each service
- Authentication duplicated everywhere
- No centralized rate limiting
- Dispersed logging
- SSL to manage everywhere
1.2 With Gatewayβ
Advantages:
- Single entry point
- Centralized authentication
- Rate limiting
- Logging/Monitoring
- SSL termination
- Load balancing
- Request transformation
2. Spring Cloud Gateway vs Zuulβ
| Criterion | Spring Cloud Gateway | Zuul 1.x |
|---|---|---|
| Architecture | Reactive (WebFlux) | Blocking (Servlet) |
| Performance | Excellent | Good |
| Programming | Functional | Imperative |
| Maintenance | Active | Deprecated |
| Spring Boot 3 | β Supported | β Not supported |
Recommendation: Use Spring Cloud Gateway for new projects.
3. Key Conceptsβ
3.1 Routeβ
A route is the basic building block: it defines where to send a request.
spring:
cloud:
gateway:
routes:
- id: user-service-route
uri: http://localhost:8081
predicates:
- Path=/api/users/**
3.2 Predicateβ
A predicate is a condition to match a request.
predicates:
- Path=/api/users/** # By path
- Method=GET,POST # By HTTP method
- Header=X-Request-Id, \d+ # By header
- Query=name # By query param
- Host=**.example.com # By hostname
- After=2024-01-01T00:00:00 # By date
3.3 Filterβ
A filter modifies the request or the response.
filters:
- AddRequestHeader=X-Gateway, true
- AddResponseHeader=X-Response-Time, 123
- RewritePath=/api/(?<segment>.*), /$\{segment}
- CircuitBreaker=name=myCircuitBreaker
3.4 Processing Flowβ
4. Gateway Configurationβ
4.1 Project Structureβ
api-gateway/
βββ pom.xml
βββ src/main/
βββ java/com/example/gateway/
β βββ ApiGatewayApplication.java
β βββ config/
β β βββ GatewayConfig.java
β β βββ SecurityConfig.java
β βββ filter/
β βββ LoggingFilter.java
βββ resources/
βββ application.yml
4.2 Maven Dependenciesβ
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>api-gateway</artifactId>
<version>1.0.0</version>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.version>
</properties>
<dependencies>
<!-- Spring Cloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Eureka Client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Circuit Breaker -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
</dependency>
<!-- Rate Limiter (Redis) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.3 Main Applicationβ
package com.example.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
5. YAML Route Configurationβ
5.1 Basic Routesβ
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
# Route to User Service
- id: user-service
uri: http://localhost:8081
predicates:
- Path=/api/users/**
# Route to Product Service
- id: product-service
uri: http://localhost:8082
predicates:
- Path=/api/products/**
# Route to Order Service
- id: order-service
uri: http://localhost:8083
predicates:
- Path=/api/orders/**
# Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
5.2 Routes with Eureka (Load Balancing)β
spring:
cloud:
gateway:
routes:
- id: user-service
# lb:// = Load Balanced via Eureka
uri: lb://user-service
predicates:
- Path=/api/users/**
- id: product-service
uri: lb://product-service
predicates:
- Path=/api/products/**
- id: order-service
uri: lb://order-service
predicates:
- Path=/api/orders/**
# Automatic service discovery
discovery:
locator:
enabled: true
lower-case-service-id: true
5.3 Routes with Filtersβ
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/api/users/**
filters:
# Add headers
- AddRequestHeader=X-Gateway-Source, api-gateway
- AddResponseHeader=X-Response-Time, ${timestamp}
# Rewrite path
- RewritePath=/api/users/(?<segment>.*), /users/$\{segment}
# Strip prefix
- StripPrefix=1
# Circuit Breaker
- name: CircuitBreaker
args:
name: userServiceCircuitBreaker
fallbackUri: forward:/fallback/users
6. Detailed Predicatesβ
6.1 Path Predicateβ
predicates:
# Exact path
- Path=/api/users
# With single wildcard (one segment)
- Path=/api/users/*
# With multiple wildcards (multiple segments)
- Path=/api/users/**
# Multiple paths
- Path=/api/users/**,/api/accounts/**
6.2 Method Predicateβ
predicates:
- Path=/api/users/**
- Method=GET,POST,PUT,DELETE
6.3 Header Predicateβ
predicates:
- Path=/api/users/**
# Header present with regex value
- Header=X-Api-Key, ^[a-zA-Z0-9]{32}$
# Header present (without checking value)
- Header=Authorization
6.4 Query Predicateβ
predicates:
- Path=/api/search
# Query param present
- Query=q
# Query param with regex value
- Query=category, ^(electronics|books|clothing)$
6.5 Host Predicateβ
predicates:
- Host=**.example.com
- Host=api.example.com,admin.example.com
6.6 DateTime Predicatesβ
predicates:
# Active after this date
- After=2024-01-01T00:00:00+00:00[UTC]
# Active before this date
- Before=2025-01-01T00:00:00+00:00[UTC]
# Active between two dates
- Between=2024-01-01T00:00:00+00:00[UTC], 2024-12-31T23:59:59+00:00[UTC]
6.7 Weight Predicate (A/B Testing)β
routes:
- id: user-service-v1
uri: lb://user-service-v1
predicates:
- Path=/api/users/**
- Weight=group1, 8 # 80% of traffic
- id: user-service-v2
uri: lb://user-service-v2
predicates:
- Path=/api/users/**
- Weight=group1, 2 # 20% of traffic
7. Detailed Filtersβ
7.1 AddRequestHeader / AddResponseHeaderβ
filters:
- AddRequestHeader=X-Request-Source, gateway
- AddResponseHeader=X-Gateway-Version, 1.0
7.2 RemoveRequestHeader / RemoveResponseHeaderβ
filters:
- RemoveRequestHeader=Cookie
- RemoveResponseHeader=X-Powered-By
7.3 RewritePathβ
filters:
# /api/users/123 β /users/123
- RewritePath=/api/(?<segment>.*), /$\{segment}
# /old-api/users β /api/v2/users
- RewritePath=/old-api/(?<segment>.*), /api/v2/$\{segment}
7.4 StripPrefix / PrefixPathβ
filters:
# /api/v1/users β /users (removes 2 segments)
- StripPrefix=2
# /users β /api/users (adds prefix)
- PrefixPath=/api
7.5 SetStatusβ
filters:
# Force a return code
- SetStatus=401
7.6 Retryβ
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY,SERVICE_UNAVAILABLE
methods: GET
backoff:
firstBackoff: 100ms
maxBackoff: 500ms
factor: 2
7.7 RequestRateLimiterβ
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
redis-rate-limiter.requestedTokens: 1
key-resolver: "#{@userKeyResolver}"
@Bean
public KeyResolver userKeyResolver() {
return exchange -> Mono.just(
exchange.getRequest()
.getHeaders()
.getFirst("X-User-Id") != null
? exchange.getRequest().getHeaders().getFirst("X-User-Id")
: exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()
);
}
8. Java Configuration (Programmatic)β
8.1 RouteLocator Beanβ
package com.example.gateway.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
// Route User Service
.route("user-service", r -> r
.path("/api/users/**")
.filters(f -> f
.stripPrefix(1)
.addRequestHeader("X-Gateway", "true")
.circuitBreaker(c -> c
.setName("userServiceCB")
.setFallbackUri("forward:/fallback/users")
)
)
.uri("lb://user-service")
)
// Route Product Service
.route("product-service", r -> r
.path("/api/products/**")
.and()
.method("GET", "POST", "PUT", "DELETE")
.filters(f -> f
.rewritePath("/api/products/(?<segment>.*)", "/products/${segment}")
.retry(config -> config
.setRetries(3)
.setStatuses(HttpStatus.BAD_GATEWAY)
)
)
.uri("lb://product-service")
)
// Route with Rate Limiting
.route("order-service", r -> r
.path("/api/orders/**")
.filters(f -> f
.requestRateLimiter(config -> config
.setRateLimiter(redisRateLimiter())
.setKeyResolver(userKeyResolver())
)
)
.uri("lb://order-service")
)
.build();
}
}
9. Custom Filtersβ
9.1 Global Filter (All Routes)β
package com.example.gateway.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.UUID;
@Component
public class LoggingFilter implements GlobalFilter, Ordered {
private static final Logger log = LoggerFactory.getLogger(LoggingFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// PRE-FILTER: Before sending to the service
String requestId = UUID.randomUUID().toString();
long startTime = System.currentTimeMillis();
ServerHttpRequest request = exchange.getRequest().mutate()
.header("X-Request-Id", requestId)
.build();
log.info(">>> Request: {} {} | RequestId: {}",
request.getMethod(),
request.getURI().getPath(),
requestId
);
// Modify the exchange with the new request
ServerWebExchange modifiedExchange = exchange.mutate()
.request(request)
.build();
return chain.filter(modifiedExchange)
.then(Mono.fromRunnable(() -> {
// POST-FILTER: After the response
long duration = System.currentTimeMillis() - startTime;
log.info("<<< Response: {} | Duration: {}ms | RequestId: {}",
exchange.getResponse().getStatusCode(),
duration,
requestId
);
}));
}
@Override
public int getOrder() {
return -1; // Executed first
}
}
9.2 GatewayFilter (Specific Route)β
package com.example.gateway.filter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class AuthenticationFilter extends AbstractGatewayFilterFactory<AuthenticationFilter.Config> {
public AuthenticationFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
String authHeader = exchange.getRequest()
.getHeaders()
.getFirst("Authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
String token = authHeader.substring(7);
// Validate the token (simplified)
if (!isValidToken(token)) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
};
}
private boolean isValidToken(String token) {
// Implement your JWT validation logic
return token != null && token.length() > 10;
}
public static class Config {
// Filter configuration if needed
}
}
Usage in YAML:
filters:
- Authentication
10. Fallback and Circuit Breakerβ
10.1 Fallback Configurationβ
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/api/users/**
filters:
- name: CircuitBreaker
args:
name: userServiceCB
fallbackUri: forward:/fallback/users
10.2 Fallback Controllerβ
package com.example.gateway.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/fallback")
public class FallbackController {
@GetMapping("/users")
public ResponseEntity<Map<String, String>> userServiceFallback() {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(Map.of(
"message", "User Service is currently unavailable",
"status", "FALLBACK"
));
}
@GetMapping("/products")
public ResponseEntity<Map<String, String>> productServiceFallback() {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(Map.of(
"message", "Product Service is currently unavailable",
"status", "FALLBACK"
));
}
@GetMapping("/orders")
public ResponseEntity<Map<String, String>> orderServiceFallback() {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(Map.of(
"message", "Order Service is currently unavailable",
"status", "FALLBACK"
));
}
}
10.3 Resilience4j Configurationβ
resilience4j:
circuitbreaker:
configs:
default:
slidingWindowSize: 10
failureRateThreshold: 50
waitDurationInOpenState: 10000
permittedNumberOfCallsInHalfOpenState: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
instances:
userServiceCB:
baseConfig: default
productServiceCB:
baseConfig: default
failureRateThreshold: 30
11. CORS Configurationβ
package com.example.gateway.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
import java.util.List;
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
CorsConfiguration corsConfig = new CorsConfiguration();
corsConfig.setAllowedOrigins(List.of(
"http://localhost:3000",
"https://myapp.com"
));
corsConfig.setAllowedMethods(Arrays.asList(
"GET", "POST", "PUT", "DELETE", "OPTIONS"
));
corsConfig.setAllowedHeaders(List.of("*"));
corsConfig.setAllowCredentials(true);
corsConfig.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfig);
return new CorsWebFilter(source);
}
}
Or via YAML:
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "http://localhost:3000"
allowedMethods:
- GET
- POST
- PUT
- DELETE
allowedHeaders: "*"
allowCredentials: true
12. Monitoring and Actuatorβ
management:
endpoints:
web:
exposure:
include: health,info,gateway
endpoint:
gateway:
enabled: true
Useful Endpointsβ
# List of routes
curl http://localhost:8080/actuator/gateway/routes
# Refresh routes
curl -X POST http://localhost:8080/actuator/gateway/refresh
# Global filters
curl http://localhost:8080/actuator/gateway/globalfilters
# Route filters
curl http://localhost:8080/actuator/gateway/routefilters
13. Summaryβ
Key Pointsβ
β
API Gateway = Single entry point
β
Routes = Define where to send requests
β
Predicates = Matching conditions
β
Filters = Modify request/response
β
lb:// = Load Balancing via Eureka
β
CircuitBreaker = Protection against failures
β
RateLimiter = Protection against abuse
Production Type Configurationβ
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/api/users/**
filters:
- StripPrefix=1
- name: CircuitBreaker
args:
name: userServiceCB
fallbackUri: forward:/fallback/users
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 100
redis-rate-limiter.burstCapacity: 200