A Spring Cloud Gateway project that supports both YAML-based configuration (traditional) and web-based dynamic configuration using Thymeleaf.
✅ Dual Configuration Methods:
- Traditional YAML configuration in
application.yml - Dynamic web-based configuration through Thymeleaf UI
✅ Dynamic Route Management:
- Create, update, delete, and view routes at runtime
- No restart required for route changes
✅ Modern Web Interface:
- Beautiful, responsive UI built with Thymeleaf
- Easy-to-use forms for route configuration
- Real-time route management
✅ REST API:
- Full REST API for programmatic route management
- Endpoints for CRUD operations on routes
- Spring Boot 3.5.6
- Spring Cloud Gateway 2024.0.0
- Spring Cloud LoadBalancer
- Thymeleaf (for web UI)
- Spring Web MVC (for Thymeleaf controllers)
- Lombok (for cleaner code)
- Maven (build tool)
- Java 17 or higher
- Maven 3.6+
- Clone the repository
- Navigate to the project directory
- Build the project:
mvn clean install- Run the application:
mvn spring-boot:runThe application will start on http://localhost:8087
You can set the admin username and password in application.yml:
spring:
security:
user:
name: admin
password: adminAccess the web-based route management interface at:
http://localhost:8087/admin/routes
From here you can:
- View all routes: See all configured routes with their details
- Create new routes: Add routes dynamically through a form
- Edit routes: Modify existing route configurations
- Delete routes: Remove routes that are no longer needed
- View route details: See complete information about any route
You can also configure routes in application.yml:
spring:
cloud:
gateway:
routes:
- id: example-route
uri: https://httpbin.org
predicates:
- Path=/httpbin/**
filters:
- StripPrefix=1The application provides REST endpoints for programmatic access:
GET http://localhost:8087/api/routesGET http://localhost:8087/api/routes/{routeId}POST http://localhost:8087/api/routes
Content-Type: application/json
{
"id": "my-service-route",
"uri": "http://localhost:8081",
"predicates": [
{
"name": "Path",
"args": {
"_genkey_0": "/api/myservice/**"
}
}
],
"filters": [
{
"name": "StripPrefix",
"args": {
"_genkey_0": "2"
}
}
],
"order": 0,
"description": "Route to my service"
}PUT http://localhost:8087/api/routes/{routeId}
Content-Type: application/json
{
"id": "my-service-route",
"uri": "http://localhost:8082",
...
}DELETE http://localhost:8087/api/routes/{routeId}- Path: Match by request path
- Example:
Path=/api/users/**
- Example:
- Method: Match by HTTP method
- Example:
Method=GET,POST
- Example:
- Header: Match by request header
- Example:
Header=X-Request-Id, \d+
- Example:
- Query: Match by query parameter
- Example:
Query=foo, ba.
- Example:
- Host: Match by host header
- Example:
Host=**.example.com
- Example:
- StripPrefix: Remove path segments
- Example:
StripPrefix=1(removes first segment)
- Example:
- AddRequestHeader: Add header to request
- Example:
AddRequestHeader=X-Request-Foo, Bar
- Example:
- AddResponseHeader: Add header to response
- Example:
AddResponseHeader=X-Response-Foo, Bar
- Example:
- RewritePath: Rewrite request path
- Example:
RewritePath=/api/(?<segment>.*), /${segment}
- Example:
- CircuitBreaker: Add circuit breaker
- Example:
CircuitBreaker=myCircuitBreaker
- Example:
Key configuration in application.yml:
server:
port: 8087
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
# Static routes configured here
thymeleaf:
cache: false
management:
endpoints:
web:
exposure:
include: gateway,health,infogateway/
├── .git/ # Git version control
├── .mvn/ # Maven wrapper
├── src/
│ ├── main/
│ │ ├── java/com/doors/gateway/
│ │ │ ├── config/ # Configuration classes (GatewayConfig, SecurityConfig, etc.)
│ │ │ ├── controller/ # Web & REST controllers (LoginController, GatewayWebController, etc.)
│ │ │ ├── model/ # Domain models (RouteDefinition, PredicateDefinition, etc.)
│ │ │ ├── service/ # Services (DynamicRouteService)
│ │ │ ├── util/ # Utility classes (MapConverter, etc.)
│ │ │ └── GatewayApplication.java # Main Spring Boot application
│ │ └── resources/
│ │ ├── static/ # Static assets
│ │ │ ├── css/ # Stylesheets (login.css, routes.css, etc.)
│ │ │ └── js/ # JavaScript (routes-form.js, advanced-form.js)
│ │ ├── templates/ # Thymeleaf templates
│ │ │ ├── login.html
│ │ │ └── routes/
│ │ │ ├── index.html
│ │ │ ├── simple-route-form.html
│ │ │ ├── advance-route-form.html
│ │ │ └── route-details.html
│ │ └── application.yml # Main configuration
│ └── test/java/com/doors/gateway/
│ └── GatewayApplicationTests.java
├── target/ # Build output (compiled classes, JARs)
├── pom.xml # Maven build configuration
├── mvnw, mvnw.cmd # Maven wrapper scripts
├── README.md
├── HELP.md
├── QUICKSTART.md
└── EXAMPLES.md
- Static Routes: Configured in
application.yml, loaded at startup - Dynamic Routes: Created through web UI or REST API, stored in memory
- Route Refresh: Automatic refresh when routes are added/updated/deleted
- Dual Stack: Uses WebFlux for Gateway and Web MVC for Thymeleaf UI
Access Spring Boot Actuator endpoints:
- Gateway routes:
http://localhost:8087/actuator/gateway/routes - Health:
http://localhost:8087/actuator/health - Info:
http://localhost:8087/actuator/info
- Load Balancing: Use
lb://service-nameas URI for load-balanced services - Order Matters: Lower order numbers have higher priority
- Path Predicates: Use
/**for wildcard matching - Testing: Use the example route to test with httpbin.org
- Check the route configuration in the web UI
- Verify the target service is running
- Check logs for any errors
- Ensure the application is running on port 8087
- Check if another application is using the port
- Routes should refresh automatically
- Check the logs for refresh events
This project is open source and available under the MIT License.
Created with ❤️ using Spring Cloud Gateway and Thymeleaf