Choreography Saga Pattern - Payment Microservices
Medium Article - Explanation In Depth
- Spring Cloud Stream with Apache Kafka Binder
- Spring WebFlux
- Leveraging Netty's non-blocking server model
Operations persising to the database have a dedicated, well-tuned thread pool as it can isolate blocking IO calls separately from the application.
cd docker
docker-compose up
Kafka Binary Distribution Download.
- Once the Kafka Docker containers are running, go onto
localhost:9000and create a clusterClick 'Add Cluster'with any name e.g.payment-saga. - Under
Cluster Zookeeper Hostsenterzoo:2181
- There are 3 topics which the Order and Payment Services use; these must be created before starting both applications.
- orders
- payments
- transactions
- Run the Order Service and the Payment Service application
- Make a POST Request to
localhost:9192/orders/createwith request body:
{
"userId": 1,
"productId": 1
}
- Make a GET Request to
localhost:9192/orders/alland see the order status updated
- The Order Service application takes in an
Orderas a request, which creates and sends anOrderPurchaseEventto the Kafka topicorderswhich is processed byOrderPurchaseEventHandlerin the payment service. OrderPurchaseEventHandlerprocesses the event and calculates if user has enough credit. If so, it sets the generatedPaymentEventstatus toAPPROVED, otherwiseDECLINED.- A
PaymentEventis emitted to the Kafka topicpayments, which thePaymentEventHandlerin the Payment Service application listens for. - If the
PaymentEventstatus isAPPROVED, it saves the transaction in theTransactionRepository. ATransactionEventis emitted to thetransactionstopic. - The
TransactionEventConsumerreads this in the order service, if successful, theOrderRepositorysaves this asORDER_COMPLETED, elseORDER_FAILED
