Service Routing
The API Gateway manages service routing by registering backend services and their routes with Kong. The GatewayManagementService maintains an in-memory cache of service and route configurations and synchronizes them with the Kong Admin API. Routes map incoming request paths, methods, and headers to specific backend services.
Service Configuration
A service represents a backend microservice that the gateway proxies requests to. Each service is defined with connection parameters and timeout settings.
ServiceConfig Properties
| Property | Type | Default | Description |
|---|---|---|---|
name | String | required | Unique service identifier |
url | String | required | Backend service URL (e.g., http://ai-service:8000) |
connectTimeout | int | 60000 | Connection timeout in milliseconds |
writeTimeout | int | 60000 | Write timeout in milliseconds |
readTimeout | int | 60000 | Read timeout in milliseconds |
retries | int | 3 | Number of retry attempts on failure |
tags | List | null | Tags for categorization and filtering |
Register a Service
Endpoint: POST /api/v1/gateway/services
curl -X POST http://localhost:8080/api/v1/gateway/services \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{
"name": "ai-service",
"url": "http://ai-service.tenant-acme:8000",
"connectTimeout": 60000,
"readTimeout": 120000,
"retries": 3,
"tags": ["data-plane", "ai"]
}'List All Services
Endpoint: GET /api/v1/gateway/services
curl http://localhost:8080/api/v1/gateway/services \
-H "Authorization: Bearer ${TOKEN}"Delete a Service
Endpoint: DELETE /api/v1/gateway/services/:serviceName
curl -X DELETE http://localhost:8080/api/v1/gateway/services/ai-service \
-H "Authorization: Bearer ${TOKEN}"Route Configuration
Routes define how incoming requests match to backend services. Routes are attached to services and can match on paths, methods, hosts, and headers.
RouteConfig Properties
| Property | Type | Default | Description |
|---|---|---|---|
name | String | required | Unique route identifier |
paths | List | null | URL path prefixes to match (e.g., /api/v1/ai) |
methods | List | null | HTTP methods to match (e.g., GET, POST) |
hosts | List | null | Hostnames to match |
headers | Map | null | Headers to match (key to list of values) |
stripPath | boolean | false | Whether to strip the matched path prefix |
preserveHost | boolean | true | Whether to preserve the original host header |
tags | List | null | Tags for categorization |
Create a Route
Endpoint: POST /api/v1/gateway/services/:serviceName/routes
curl -X POST http://localhost:8080/api/v1/gateway/services/ai-service/routes \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{
"name": "ai-service-route",
"paths": ["/api/v1/ai", "/api/v1/conversations"],
"methods": ["GET", "POST", "PUT", "DELETE"],
"stripPath": false,
"preserveHost": true,
"tags": ["data-plane"]
}'Delete a Route
Endpoint: DELETE /api/v1/gateway/routes/:routeName
curl -X DELETE http://localhost:8080/api/v1/gateway/routes/ai-service-route \
-H "Authorization: Bearer ${TOKEN}"Routing Patterns
Path-Based Routing
Route by URL path prefix to direct requests to the correct microservice:
| Path Prefix | Backend Service |
|---|---|
/api/v1/ai/* | ai-service |
/api/v1/billing/* | billing-service |
/api/v1/audit/* | audit-service |
/api/v1/tenants/* | tenant-service |
/api/v1/iam/* | iam-service |
Host-Based Routing
Route by hostname for multi-tenant isolation:
{
"name": "tenant-acme-route",
"hosts": ["acme.matih.ai"],
"paths": ["/api/v1"],
"preserveHost": true
}Header-Based Routing
Route by custom headers for service versioning or tenant routing:
{
"name": "v2-api-route",
"paths": ["/api"],
"headers": {
"X-API-Version": ["v2"]
}
}Caching Behavior
The GatewayManagementService maintains an in-memory ConcurrentHashMap cache for both services and routes. When a service or route is created or updated via the API, the cache is updated alongside the Kong Admin API call. On service deletion, the cache entry is removed.
The cache serves as a fast lookup for management operations. Actual traffic routing is handled by Kong directly and does not depend on this cache.