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:9000
and create a clusterClick 'Add Cluster'
with any name e.g.payment-saga
. - Under
Cluster Zookeeper Hosts
enterzoo: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/create
with request body:
{
"userId": 1,
"productId": 1
}
- Make a GET Request to
localhost:9192/orders/all
and see the order status updated
- The Order Service application takes in an
Order
as a request, which creates and sends anOrderPurchaseEvent
to the Kafka topicorders
which is processed byOrderPurchaseEventHandler
in the payment service. OrderPurchaseEventHandler
processes the event and calculates if user has enough credit. If so, it sets the generatedPaymentEvent
status toAPPROVED
, otherwiseDECLINED
.- A
PaymentEvent
is emitted to the Kafka topicpayments
, which thePaymentEventHandler
in the Payment Service application listens for. - If the
PaymentEvent
status isAPPROVED
, it saves the transaction in theTransactionRepository
. ATransactionEvent
is emitted to thetransactions
topic. - The
TransactionEventConsumer
reads this in the order service, if successful, theOrderRepository
saves this asORDER_COMPLETED
, elseORDER_FAILED