A robust, event-driven microservices architecture leveraging Spring Boot and Apache Kafka for asynchronous communication between services.
- Architecture Overview
- Microservices
- Technology Stack
- Project Structure
- Configuration
- Installation & Setup
- API Documentation
- Kafka Integration
- Roadmap
- License
- Author
┌────────────────┐ ┌────────────────┐ ┌────────────────┐
│ │ │ │ │ │
│ Product │ │ Order │ │ Inventory │
│ Service │◄────┤ Service │◄────┤ Service │
│ │ │ │ │ │
└───────┬────────┘ └───────┬────────┘ └───────┬────────┘
│ │ │
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────┐
│ │
│ Apache Kafka │
│ │
└─────────────────────────────────────────────────────────────┘
▲ ▲ ▲
│ │ │
│ │ │
┌───────┴────────┐ ┌───────┴────────┐ ┌───────┴────────┐
│ │ │ │ │ │
│ Product │ │ Order │ │ Inventory │
│ Database │ │ Database │ │ Database │
│ │ │ │ │ │
└────────────────┘ └────────────────┘ └────────────────┘
This project implements a distributed system with three independent microservices communicating via Apache Kafka. The architecture follows modern design principles for building scalable, resilient, and maintainable applications.
Manages the product catalog with comprehensive product information:
- Product details (name, description, price)
- Category management
- Product metadata
Handles the complete order lifecycle:
- Order creation and processing
- Order status tracking
- Customer information
Maintains real-time inventory data:
- Stock levels and availability
- Warehouse locations
- Inventory adjustments
| Technology | Version | Purpose |
|---|---|---|
| Spring Boot | 2.7.0 | Microservices framework |
| Spring Web | 2.7.0 | RESTful API development |
| Spring Data JPA | 2.7.0 | ORM & database operations |
| MySQL | 8.0 | Persistent data storage |
| Apache Kafka | 3.2.0 | Event-driven messaging |
| Lombok | 1.18.24 | Boilerplate code reduction |
| ModelMapper | 3.1.0 | Object mapping |
| Maven | 3.8.6 | Dependency and build management |
microservices-kafka-app/
│
├── product-service/
│ ├── src/
│ ├── pom.xml
│ └── README.md
│
├── order-service/
│ ├── src/
│ ├── pom.xml
│ └── README.md
│
├── inventory-service/
│ ├── src/
│ ├── pom.xml
│ └── README.md
│
├── docker-compose.yml
├── README.md
└── LICENSE
Each service is a complete Spring Boot application with its own:
- Controllers, Services, Repositories
- Entity models and DTOs
- Kafka configuration
- Database configuration
Each microservice has its own application.yml with service-specific configurations:
Product Service Configuration
spring:
application:
name: product
datasource:
url: jdbc:mysql://localhost:3306/product_kafka_db
username: root
password: 1234
jpa:
hibernate:
ddl-auto: update
server:
port: 8081
kafka:
bootstrap-servers: localhost:9092
topic:
product: product-topicOrder Service Configuration
spring:
application:
name: order
datasource:
url: jdbc:mysql://localhost:3306/order_kafka_db
username: root
password: 1234
jpa:
hibernate:
ddl-auto: update
server:
port: 8082
kafka:
bootstrap-servers: localhost:9092
topic:
order: order-topicInventory Service Configuration
spring:
application:
name: inventory
datasource:
url: jdbc:mysql://localhost:3306/inventory_kafka_db
username: root
password: 1234
jpa:
hibernate:
ddl-auto: update
server:
port: 8083
kafka:
bootstrap-servers: localhost:9092
topic:
inventory: inventory-topicThree separate MySQL databases are used:
product_kafka_dborder_kafka_dbinventory_kafka_db
The following Kafka topics facilitate inter-service communication:
product-topicorder-topicinventory-topic
- JDK 17+
- MySQL 8.0+
- Apache Kafka 3.2.0+
- Maven 3.8.6+
-
Clone the repository
git clone https://github.com/your-username/microservices-kafka-app.git cd microservices-kafka-app -
Configure MySQL
# Create required databases mysql -u root -p CREATE DATABASE product_kafka_db; CREATE DATABASE order_kafka_db; CREATE DATABASE inventory_kafka_db;
-
Start Kafka
# Start Zookeeper bin/zookeeper-server-start.sh config/zookeeper.properties # Start Kafka broker bin/kafka-server-start.sh config/server.properties
-
Build and run microservices
# Product Service cd product-service mvn clean install mvn spring-boot:run # Order Service cd ../order-service mvn clean install mvn spring-boot:run # Inventory Service cd ../inventory-service mvn clean install mvn spring-boot:run
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/products |
Create product |
| GET | /api/products |
Get all products |
| GET | /api/products/{id} |
Get product by ID |
| PUT | /api/products/{id} |
Update product |
| DELETE | /api/products/{id} |
Delete product |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/orders |
Create order |
| GET | /api/orders |
Get all orders |
| GET | /api/orders/{id} |
Get order by ID |
| PUT | /api/orders/{id} |
Update order |
| DELETE | /api/orders/{id} |
Delete order |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/inventories |
Create inventory entry |
| GET | /api/inventories |
Get all inventory items |
| GET | /api/inventories/{id} |
Get inventory by ID |
| PUT | /api/inventories/{id} |
Update inventory |
| DELETE | /api/inventories/{id} |
Delete inventory |
The microservices communicate asynchronously through Kafka events:
Event Flow:
Product Service ──[product-created]──> product-topic ──> Order Service
└──> Inventory Service
Order Service ──[order-placed]──> order-topic ──> Inventory Service
└──> Product Service (for analytics)
Inventory Service ──[stock-updated]──> inventory-topic ──> Product Service
└──> Order Service
Event Flow Examples:
- When a new product is created, Product Service publishes an event to
product-topic - Order Service consumes this event to update its product catalog
- When an order is placed, Order Service publishes to
order-topic - Inventory Service consumes this event to update stock levels
- Implement comprehensive Kafka listeners for all services
- Add Circuit Breakers using Resilience4J
- Integrate OpenAPI (Swagger) documentation
- Containerise with Docker and Docker Compose
- Implement Spring Cloud Config for centralised configuration
- Add service discovery with Eureka
- Implement API Gateway with Spring Cloud Gateway
- Add distributed tracing with Sleuth and Zipkin
This project is licensed under the MIT License - see the LICENSE file for details.
Nipun
Software Engineering Student
Specialising in scalable backend systems with Spring Boot, Kafka, and Microservices architecture.