https://github.com/youssefgamalmohamed/spring-cloud-api-gateway
API Gateway with Spring Cloud API Gateway and Contains Most of Patterns into API Gateway as ( Security, Rate-Limiting, Routing , Retry Pattern, Circuit-Breakers, ... etc )
https://github.com/youssefgamalmohamed/spring-cloud-api-gateway
api-gateway authentication authorization circuit-breaker microservices proxy rate-limiting retry-pattern routing spring-boot spring-cloud-gateway
Last synced: 6 months ago
JSON representation
API Gateway with Spring Cloud API Gateway and Contains Most of Patterns into API Gateway as ( Security, Rate-Limiting, Routing , Retry Pattern, Circuit-Breakers, ... etc )
- Host: GitHub
- URL: https://github.com/youssefgamalmohamed/spring-cloud-api-gateway
- Owner: youssefGamalMohamed
- Created: 2025-07-02T21:37:05.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-07-04T23:10:09.000Z (7 months ago)
- Last Synced: 2025-07-05T00:23:30.758Z (7 months ago)
- Topics: api-gateway, authentication, authorization, circuit-breaker, microservices, proxy, rate-limiting, retry-pattern, routing, spring-boot, spring-cloud-gateway
- Language: Java
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring Cloud API Gateway
[](https://spring.io/projects/spring-boot)
[](https://spring.io/projects/spring-cloud)
[](https://adoptopenjdk.net/)
[](https://redis.io/)
[](https://zipkin.io/)
[](https://www.docker.com/)
[](https://projectreactor.io/)
[](https://www.keycloak.org/)
[](https://github.com/rakyll/hey)
---
## Overview
A robust **API Gateway** built with [Spring Cloud Gateway](https://spring.io/projects/spring-cloud-gateway), designed to demonstrate and implement common API Gateway patterns such as **security**, **rate-limiting**, **routing**, **resilience**, and **observability**.
This project is ready for extension and integration with microservices architectures.
---
## โจ Features
- **Dynamic Routing**: Easily route requests to backend services.
- **Rate Limiting**: Protect your APIs using Redis-backed rate limiting.
- **Resilience Patterns**: Circuit breaker, retry, and fallback using Resilience4j.
- **Distributed Tracing**: Integrated with Zipkin and Micrometer Tracing.
- **Metrics & Monitoring**: Expose actuator endpoints for health and metrics.
- **Custom Filters**: Add custom logic (e.g., request ID injection) via global filters.
- **Docker Compose**: Spin up dependencies (Redis, Zipkin, Keycloak) with a single command.
- **Reactive & Non-blocking**: Built on Spring WebFlux for high concurrency.
- **Authentication & Authorization**: Secured with Keycloak for OAuth2 and JWT-based authentication.
---
## ๐ Getting Started
### Prerequisites
- [Java 17+](https://adoptopenjdk.net/)
- [Maven](https://maven.apache.org/)
- [Docker](https://www.docker.com/) (for running dependencies)
- [Hey](https://github.com/rakyll/hey) (for stress testing)
### Clone the Repository
```bash
git clone https://github.com/your-username/spring-cloud-api-gateway.git
cd spring-cloud-api-gateway
```
### Start Dependencies
```bash
docker compose up -d
```
### Configure Keycloak
After starting the Docker Compose services, configure Keycloak:
1. Access the Keycloak Admin Console at [http://localhost:8080](http://localhost:8080).
2. Log in with the credentials:
- Username: `admin`
- Password: `admin`
3. Import the realm configuration:
- Navigate to the Keycloak Admin Console.
- Click on the realm dropdown (top-left) and select **"Add realm"**.
- Choose the `realm-settings.json` file located in the `imports/keykloak/` folder of the project.
- Click **Create** to import the `json-placeholder-realm` configuration.
### Run the Application
```bash
./mvnw spring-boot:run
```
---
## ๐ ๏ธ Tools & Technologies
| Tool/Tech | Description |
|---------------------|------------------------------------|
|  | Application framework |
|  | API Gateway |
|  | Programming language |
|  | Rate limiting backend |
|  | Distributed tracing |
|  | Containerization |
|  | Metrics & tracing |
|  | Resilience patterns |
|  | Authentication & authorization |
|  | Stress testing tool |
---
## ๐ฆ Project Structure
```
src/
main/
java/
com.youssef.gamal.microservices.spring_cloud_api_gateway/
SpringCloudApiGatewayApplication.java
configs/
filters/
resources/
application.yaml
test/
java/
...
compose.yml
README.md
pom.xml
imports/
keykloak/
realm-settings.json
```
---
## โ๏ธ Configuration
- **Routes** and **filters** are defined in [`application.yaml`](src/main/resources/application.yaml).
- **Rate limiting** and **custom filters** are configured in the `configs` and `filters` packages.
- **Tracing** is enabled and configured for Zipkin.
- **Authentication** is configured with Keycloak in `application.yaml` and `realm-settings.json`.
---
## ๐ Observability
- **Zipkin UI**: [http://localhost:9411](http://localhost:9411)
- **Actuator Endpoints**: [http://localhost:8080/actuator](http://localhost:8080/actuator)
---
## ๐งช Testing
Run all unit tests:
```bash
./mvnw test
```
### Stress Testing
To perform stress testing on the API Gateway, we use the `hey` tool to simulate load. Below is an example command to test the `GET /posts/{id}` endpoint:
```bash
hey -n 100 -c 100 -m GET \
-H "Authorization: Bearer $TOKEN_VALUE" \
http://localhost:9090/posts/1
```
Replace `$TOKEN_VALUE` with a valid JWT token obtained from Keycloak.
**Example Stress Test Result**:
```plaintext
Summary:
Total: 4.1552 secs
Slowest: 4.1550 secs
Fastest: 0.4334 secs
Average: 3.2293 secs
Requests/sec: 24.0665
Response time histogram:
0.433 [1] |โ
0.806 [5] |โ โ โ โ
1.178 [6] |โ โ โ โ โ
1.550 [3] |โ โ โ
1.922 [0] |
2.294 [0] |
2.666 [0] |
3.038 [0] |
3.411 [14] |โ โ โ โ โ โ โ โ โ โ โ โ
3.783 [45] |โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
4.155 [26] |โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
Latency distribution:
10% in 1.0938 secs
25% in 3.3998 secs
50% in 3.5556 secs
75% in 3.8002 secs
90% in 3.9024 secs
95% in 3.9245 secs
99% in 4.1550 secs
Details (average, fastest, slowest):
DNS+dialup: 0.0090 secs, 0.4334 secs, 4.1550 secs
DNS-lookup: 0.0022 secs, 0.0000 secs, 0.0108 secs
req write: 0.0013 secs, 0.0000 secs, 0.0058 secs
resp wait: 3.2180 secs, 0.4223 secs, 4.1318 secs
resp read: 0.0008 secs, 0.0000 secs, 0.0228 secs
Status code distribution:
[200] 5 responses
[429] 95 responses
```
The high number of `[429]` responses indicates that the rate limiter (configured in `application.yaml`) is effectively throttling requests.
---
## ๐ค Contributing
Contributions are welcome! Please open issues or submit pull requests for improvements.
---
## ๐ค Author
[Youssef Gamal](https://github.com/your-username)
---
## ๐ฌ Contact
For questions or support, please open an issue or contact me via [GitHub](https://github.com/your-username).
---