Spring Cloud Gateway Setup
What is Spring Cloud Gateway?
Spring Cloud Gateway is a modern API Gateway built on Spring Boot and Spring WebFlux.
It acts as a single entry point for client requests and forwards them to the appropriate backend services.
Client
|
v
API Gateway (Port 8080)
|
+----> User Service (8081)
|
+----> Order Service (8082)
Common responsibilities:
- Request routing
- Authentication & Authorization
- Rate Limiting
- Logging & Monitoring
- Header manipulation
- Request/Response transformation
- Resilience (Retry, Circuit Breaker)
Is there an @EnableGateway annotation?
No.
Spring Cloud Gateway uses Spring Boot Auto Configuration.
Once the dependency is added, Spring Boot automatically creates all required Gateway components.
Unlike older Netflix Zuul: @EnableZuulProxy
Spring Cloud Gateway requires no explicit enable annotation.
@SpringBootApplication
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
Create the Project
Maven Dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway-server-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Main Application
package com.example.apigateway;
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);
}
}
Configure First Route
application.yml
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: user-service
uri: http://localhost:8081
predicates:
- Path=/users/**
Route Components
Every route contains:
id:
Unique route identifier
uri:
Destination service URL
predicates:
Conditions that must match before routing
Example:
- Path=/users/**
means:
/users/1
/users/abc
/users/profile
will be routed.
Backend Service Example
Run a sample service on port 8081.
package com.example.userservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/users/hello")
public String hello() {
return "Hello from User Service";
}
}
Test Request
Call:
GET http://localhost:8080/users/hello
Flow:
Client
|
v
Gateway (8080)
|
| Path matches /users/**
v
User Service (8081)
Response:
Hello from User Service
What Happens Internally?
Request arrives
|
v
Route Predicate Matching
|
v
Route Selected
|
v
Gateway Filter Chain
|
v
Proxy Request To Target Service
|
v
Receive Response
|
v
Return To Client
How To Verify Gateway Is Active
Enable debug logging:
logging:
level:
org.springframework.cloud.gateway: DEBUG
Startup logs:
Loaded RoutePredicateFactory [Path]
Loaded RoutePredicateFactory [Method]
Loaded GatewayFilterFactory [AddRequestHeader]
This confirms Gateway infrastructure is initialized.
Minimum Requirements For A Gateway
- Add Gateway Dependency
spring-cloud-starter-gateway-server-webflux
- Define At Least One Route
spring:
cloud:
gateway:
routes:
- id: user-service
uri: http://localhost:8081
predicates:
- Path=/users/**
Without routes, the application starts successfully but does not forward any requests.
Key Takeaways
- Spring Cloud Gateway is built on Spring Boot + WebFlux.
- No @EnableGateway annotation is required.
- Gateway functionality is enabled through Auto Configuration.
-
A Route consists of:
- id
- uri
- predicates
- filters (optional)
- Requests are matched using predicates and then forwarded to backend services.
- Gateway acts as the single entry point in a microservices architecture.