The purpose of this blog post is to explain, how we can achieve event-driven microservices with spring boot and kubernetes.
The original source of this blog post is below youtube channel.
- Please find the source code for this post in here:
https://github.com/dhanuka84/kbe-sb-microservices/tree/fixes
What is the difference
https://github.com/dhanuka84/kbe-sb-microservices/pull/1/commits
1. Fixed unit test failure due to SQL script not compatible with H2 DB
2. Images can be build locally and use same docker images with minikube.
3. MySQL datasource username configuration fix.
4. Simple and Short explanation.
How to build and test
1. Build java project & run unit tests
mvn clean package
2. Start minikube
start minikube
# this is important otherwise k8s cluster can't pull the docker image
eval $(minikube docker-env)
3. Build docker images
mvn spring-boot:build-image
Deploy containers in kubernetes
minikube tunnel
kubectl apply -f ./kbe-sb-microservices/k8s-scripts
Explaining the flow and components
1. Gateway : this will route API path to the correct URI
2. Order-Service Micro-Service: placing an order
- Let's take order service micro-service and it's placeOrder() method/API.
- Controller will call the BeerOrder service and then it's business logic
- Then BeerOrderManager will call state machine to start the spring boot event bus flow.
- According to state machine configuration, next event is BeerOrderEventEnum.VALIDATE_ORDER and next action is validateBeerOrder .
- Now this will trigger the validateBeerOrder action and it will send ValidateBeerOrderRequest to VALIDATE_ORDER_QUEUE JMS queue.
- Now our second micro-service called Beer-Service will listen to this VALIDATE_ORDER_QUEUE, and validate the beer order and send the result to another JMS queue called VALIDATE_ORDER_RESULT_QUEUE
Then again, Order-Service micro-service will listen to validation result queue and process the order and update the status of the order.
Monitoring
- Filebeat will collect all the docker logs and ingest them into Elastisearch.
- So then we can monitor application logs using Kibana.
Key takeaways
- Microservices communicated with each other in an event-driven manner using JMS queue.
- Spring Boot state machine (event bus) has been used to simplifying the business flow of the application.
- Using above two techniques, events and states has been managed both in standalone and distributed way.
- Centralized logging can be used to trouble shoot and monitor distributed applications.