វេទិកា microservices សម្រាប់ការលក់រាយ
Spring Cloud Gateway នៅពីមុខសេវាកម្មផលិតផល និងវិក្កយបត្រ ជាមួយ JWT ពី Keycloak, circuit breaker តាម route នីមួយៗ និង Kafka សម្រាប់បង្កើតផលិតផលដោយមិនរង់ចាំ។
- ប្រភេទ
- គម្រោងផ្ទាល់ខ្លួន
- ឆ្នាំ
- 2025
- ស្ថានភាព
- ដំណើរការលើម៉ាស៊ីនផ្ទាល់
- បច្ចេកវិទ្យា
ការសិក្សាករណីលម្អិតនេះមានជាភាសាអង់គ្លេសនៅឡើយ។
បញ្ហា
A personal project for practising how a small retail back office splits into services: a product catalogue, invoicing, and one gateway that every client request passes through. It runs locally with Docker; there is no public deployment.
Give clients a single authenticated entry point while keeping the services independent, stop one slow or failing service from tying up the gateway, and accept product-creation requests quickly even when the product service is busy.
តួនាទីរបស់ខ្ញុំ
Personal project. I wrote the gateway, the product and invoice services, their database migrations and the local Docker and Kafka setup.
វិធីសាស្ត្រ
The design puts each concern in one place. Identity is checked once, at the edge. Failure isolation is a gateway rule per route, not code in every caller. Work that can wait goes onto a Kafka topic instead of a synchronous call. Each service owns its schema through its own Flyway migrations, so services can change independently.
ស្ថាបត្យកម្ម
Clients only talk to the gateway. It validates the Keycloak token, rewrites the path and forwards the request to the owning service through a circuit breaker. Writes that can be processed later go onto a Kafka topic instead of a synchronous call.
- API client
- Spring Cloud Gateway
- Keycloak
- product-service
- invoice-service
- PostgreSQL
- Kafka
លំហូរសំណើ
ការសម្រេចចិត្តបច្ចេកទេស
- Spring Cloud Gateway
- Routing, authentication and resilience rules live in gateway configuration instead of being repeated in each service.
- Keycloak
- A standard OpenID Connect provider, so services trust signed tokens rather than managing users.
- Resilience4j
- One circuit breaker per route (opens at a 50% failure rate over a 10-call window), so a failing service fails fast.
- Kafka
- Decouples product creation from the request that asks for it.
- Flyway
- Versioned SQL migrations, so each service owns and evolves its own schema.
- OpenFeign
- A declarative client for the one synchronous call between services.
ការអនុវត្ត
The gateway is configured almost entirely in YAML: each route has a path rewrite and a named circuit breaker. A global filter takes the current trace ID from Micrometer Tracing, adds it to the logging context and returns it in an X-Trace-ID response header, so an error a client reports can be matched to server logs.
Security is a resource-server setup. Every path except /actuator/** needs a valid JWT, and authentication failures go through a custom entry point that returns a consistent JSON error body.
Each service has its own Dockerfile and Spring profiles for local and container runs. One Compose file starts a three-broker Kafka cluster with ZooKeeper; another starts Keycloak with Prometheus.
បញ្ហាប្រឈម និងដំណោះស្រាយ
បញ្ហាប្រឈម
A slow or failing service can hold gateway requests open and spread the failure to every client.
ដំណោះស្រាយ
A Resilience4j circuit breaker per route opens at a 50% failure rate over a 10-call window and sends requests to a fallback endpoint instead of waiting.
បញ្ហាប្រឈម
An error a client reports is hard to match to the right lines in several services’ logs.
ដំណោះស្រាយ
A global gateway filter puts the Micrometer trace ID into the logging context and returns it in an X-Trace-ID response header.
បញ្ហាប្រឈម
Product creation should not depend on the product service being responsive at the moment of the request.
ដំណោះស្រាយ
POST /v1/async/product publishes to the new-products topic and returns 202 Accepted; product-service consumes the event and saves it in a transaction.
បញ្ហាប្រឈម
Clients need a predictable error when a token is missing or invalid.
ដំណោះស្រាយ
A custom authentication entry point returns the same JSON error body for every authentication failure.
សុវត្ថិភាព និងភាពជឿជាក់
- Tokens are verified against Keycloak’s signing keys; services never handle passwords.
- Only the health and prometheus actuator endpoints are exposed, for health checks and metrics scraping.
- Circuit breakers and a fixed-backoff retry template (three attempts, two seconds apart) limit the impact of a slow dependency.
ការធ្វើតេស្ត
Each service has a Spring Boot context-load test, which catches wiring and configuration errors but not behaviour. API flows are checked by hand through Swagger UI and Postman.
ការប្តូរយកអ្វីមួយ
ជម្រើស
Asynchronous product creation through Kafka
តម្លៃដែលត្រូវបង់
The client gets 202 before the event is processed and, without a status endpoint, cannot learn whether creation succeeded.
ជម្រើស
A single gateway as the only entry point
តម្លៃដែលត្រូវបង់
Every request takes an extra hop, and the gateway itself becomes a component that has to stay available.
ជម្រើស
A three-broker Kafka cluster for local runs
តម្លៃដែលត្រូវបង់
Closer to a real cluster’s behaviour, but heavier to start on a laptop than a single broker.
លទ្ធផល
One runnable repository that shows token validation at the edge, per-route circuit breakers, trace IDs returned to clients and an event-driven write path.
មេរៀន និងជំហានបន្ទាប់
ចំណុចខ្វះខាតដែលដឹង
- The gateway has a route to a user-service that is not part of this repository.
- There are no behavioural tests yet, and the CI workflow the README describes is not committed.
- The asynchronous endpoint answers 202 before the event is processed and offers no status endpoint, so a client cannot find out whether creation succeeded.
- Local configuration files are committed; a real deployment would load credentials from a secret store.
អ្វីដែលខ្ញុំនឹងកែប្រែបន្ទាប់
- Add a status endpoint for asynchronous writes so a client can confirm the outcome.
- Write behavioural tests for routing, fallbacks and the token check, and commit the CI workflow the README describes.
- Load credentials from the environment or a secret store instead of committed local configuration.
គម្រោងបន្ទាប់ការផ្តល់សិទ្ធិលម្អិតជាមួយ Keycloak