Overview
The API Gateway serves as the single entry point for all external requests to the StreamLine Logistics platform. Built with Spring Cloud Gateway, it provides request routing, load balancing, security, and cross-cutting concerns for the microservices architecture.Development Status: The API Gateway module exists in the codebase but is not yet included in the docker-compose.yml deployment. Currently, microservices can be accessed directly on their respective ports.
The API Gateway is designed to run on port 8080 and route all incoming traffic to the appropriate backend microservices.
Architecture Role
The Gateway sits between external clients and internal microservices:Key Responsibilities
- Unified Entry Point: Single address for all client requests
- Service Routing: Forward requests to appropriate microservices
- Load Balancing: Distribute traffic across service instances
- Security: Centralized authentication and authorization (future)
- Rate Limiting: Protect backend services from overload (future)
- Request/Response Transformation: Modify headers, paths, and payloads as needed
Configuration
Application Properties
The Gateway uses minimal configuration inapplication.properties:
The Gateway relies on Spring Cloud Gateway’s auto-configuration and Eureka integration for routing. Routes can be defined programmatically or via configuration files.
Default Port
The API Gateway runs on port 8080 (Spring Boot default). This can be overridden:Spring Boot Application
The Gateway application is a standard Spring Boot application:@EnableGateway annotation is required.
Dependencies
Frompom.xml:
Key Dependencies Explained
| Dependency | Purpose |
|---|---|
spring-cloud-starter-gateway-server-webmvc | WebMVC-based Gateway implementation |
spring-cloud-starter-netflix-eureka-client | Service discovery integration |
spring-cloud-starter-config | Config Server client support |
spring-boot-starter-actuator | Health checks and metrics |
This project uses
spring-cloud-starter-gateway-server-webmvc, which is the servlet-based Gateway implementation. For reactive applications, use spring-cloud-starter-gateway instead.Eureka Integration
The Gateway integrates with Eureka Server for dynamic service discovery:- Gateway automatically discovers available service instances
- Routes can reference services by their registered names
- Load balancing happens automatically across instances
- Failed instances are automatically removed from routing
Routing Configuration
Automatic Routing with Eureka
With Eureka enabled, the Gateway can automatically route requests based on service names:Custom Route Configuration
Routes can be defined programmatically for more control:lb:// prefix enables client-side load balancing via Spring Cloud LoadBalancer.
Route Configuration via YAML
Alternatively, define routes inapplication.yml:
Docker Configuration
The API Gateway is deployed as a Docker container:The Gateway must start after Eureka Server to properly register and discover backend services. The
depends_on directive ensures correct startup order.Request Flow
Example: Creating an Order
-
Client Request
-
Gateway Processing
- Receives request on port 8080
- Matches route predicate
/api/orders/** - Queries Eureka for
msvc-orderinstances - Selects instance via load balancing
-
Forward to Service
- Forwards request to Order Service (e.g.,
http://order-service:8090/api/orders) - Preserves headers, body, and HTTP method
- Forwards request to Order Service (e.g.,
-
Response
- Order Service processes request
- Response flows back through Gateway
- Client receives response
Filters and Transformations
Global Filters
Apply cross-cutting concerns to all routes:Request/Response Modification
Modify headers, paths, or bodies:Config Server Integration
The Gateway can load configuration from Config Server:Monitoring and Observability
Actuator Endpoints
The Gateway exposes Spring Boot Actuator endpoints:/actuator/health- Gateway health status/actuator/gateway/routes- View configured routes/actuator/gateway/refresh- Refresh route configuration/actuator/gateway/globalfilters- List global filters
Metrics
Gateway automatically collects metrics:- Request count per route
- Request duration
- Error rates
- Active connections
Security
Future Authentication/Authorization
The Gateway is the ideal place to implement security:Centralized authentication at the Gateway prevents unauthorized requests from reaching backend services, reducing security overhead in individual microservices.
Performance and Scaling
Load Balancing
Thelb:// URI scheme enables client-side load balancing:
- Round-robin distribution (default)
- Configurable load balancing strategies
- Automatic health-based routing
- Sticky sessions (if configured)
Rate Limiting
Protect backend services with rate limiting:Circuit Breaking
Implement resilience with Resilience4j:Troubleshooting
503 Service Unavailable
Causes:- Backend service not registered in Eureka
- All service instances are down
- Network connectivity issues
- Check Eureka dashboard for registered services
- Verify backend service health
- Review Gateway logs for routing errors
- Test direct connectivity to backend service
Route Not Found (404)
Causes:- No matching route predicate
- Incorrect path in request
- Route configuration error
- Review route configuration
- Check
/actuator/gateway/routesendpoint - Verify request path matches predicate
- Enable debug logging:
logging.level.org.springframework.cloud.gateway=DEBUG
Timeout Errors
Causes:- Backend service slow response
- Network latency
- Default timeout too short
- Increase timeout configuration:
- Optimize backend service performance
- Implement caching strategies
Best Practices
- Keep Gateway Stateless: Don’t store session state in the Gateway
- Centralize Cross-Cutting Concerns: Authentication, logging, rate limiting
- Use Descriptive Route IDs: Makes debugging easier
- Monitor Gateway Metrics: Track request rates, latencies, and errors
- Implement Circuit Breakers: Prevent cascading failures
- Version Your APIs: Use path prefixes like
/v1/,/v2/
Related Services
- Eureka Server - Service discovery for routing
- Config Server - Centralized configuration management