A comprehensive demonstration of microservices architecture using Apache Kafka as a message broker, built with .NET 9 and Docker.
This project demonstrates a real-world microservices architecture with the following components:
βββββββββββββββββββ ββββββββββββββββ βββββββββββββββββββββββ
β Order Service βββββΆβ Kafka βββββΆβ Inventory Service β
β (Producer) β β (Message β β (Consumer) β
β β β Broker) β β β
βββββββββββββββββββ β β βββββββββββββββββββββββ
β β
β β βββββββββββββββββββββββ
β βββββΆβ Notification Serviceβ
β β β (Consumer) β
ββββββββββββββββ βββββββββββββββββββββββ
-
Order Service (Port 5001)
- Handles order creation
- Produces
OrderCreatedEventto Kafka - REST API for order management
-
Inventory Service (Port 5002)
- Consumes
OrderCreatedEvent - Manages product inventory
- Reserves inventory for orders
- Produces
InventoryReservedEvent
- Consumes
-
Notification Service (Port 5003)
- Consumes multiple events
- Sends notifications to customers
- Handles order confirmations and inventory updates
-
Kafka Infrastructure
- Kafka Broker (Port 9092)
- Zookeeper (Port 2181)
- Kafka UI (Port 8080) - Web interface for monitoring
- Docker Desktop
- .NET 9 SDK (for local development)
- PowerShell or Bash terminal
-
Clone and navigate to the project:
cd d:\entertainment\kafka
-
Start the entire system:
docker-compose up --build
This will start:
- Zookeeper
- Kafka broker
- Kafka UI (web interface)
- All three microservices
-
Verify services are running:
- Kafka UI: http://localhost:8080
- Order Service API: http://localhost:5001
- Inventory Service API: http://localhost:5002
- Notification Service API: http://localhost:5003
-
Access Swagger Documentation:
- Order Service: http://localhost:5001/swagger
- Inventory Service: http://localhost:5002/swagger
- Notification Service: http://localhost:5003/swagger
-
Start Kafka infrastructure only:
docker-compose up -d zookeeper kafka kafka-ui
-
Run services locally:
# Terminal 1 - Order Service cd src/KafkaMicroservices.OrderService dotnet run # Terminal 2 - Inventory Service cd src/KafkaMicroservices.InventoryService dotnet run # Terminal 3 - Notification Service cd src/KafkaMicroservices.NotificationService dotnet run
-
Create a new order:
$body = @{ CustomerId = "CUST001" Items = @( @{ ProductId = "PROD001" ProductName = "Laptop" Quantity = 2 Price = 999.99 }, @{ ProductId = "PROD002" ProductName = "Mouse" Quantity = 1 Price = 29.99 } ) } | ConvertTo-Json -Depth 3 Invoke-RestMethod -Uri "http://localhost:5001/api/orders" -Method POST -Body $body -ContentType "application/json"
-
Check the logs to see the event flow:
- Order Service logs: Order created and event published
- Inventory Service logs: Inventory reservation
- Notification Service logs: Customer notification sent
# Get all inventory
Invoke-RestMethod -Uri "http://localhost:5002/api/inventory" -Method GET
# Get specific product inventory
Invoke-RestMethod -Uri "http://localhost:5002/api/inventory/PROD001" -Method GET# Get notifications for a customer
Invoke-RestMethod -Uri "http://localhost:5003/api/notifications/CUST001" -Method GETd:\entertainment\kafka\
βββ src\
β βββ KafkaMicroservices.Shared\ # Shared models and contracts
β βββ KafkaMicroservices.OrderService\ # Order management service
β βββ KafkaMicroservices.InventoryService\ # Inventory management service
β βββ KafkaMicroservices.NotificationService\ # Notification service
βββ docker\ # Docker configurations
βββ docs\ # Documentation
βββ scripts\ # Demo and setup scripts
βββ docker-compose.yml # Docker orchestration
βββ README.md # This file
- .NET 9: Latest version of .NET
- ASP.NET Core: Web API framework
- Swagger/OpenAPI: API documentation and testing
- Apache Kafka: Message broker
- Docker: Containerization
- Confluent.Kafka: .NET Kafka client library
-
Order Creation:
HTTP POST β Order Service β OrderCreatedEvent β Kafka Topic: order-created -
Inventory Processing:
Kafka Topic: order-created β Inventory Service β InventoryReservedEvent β Kafka Topic: inventory-reserved -
Notification Processing:
Kafka Topic: order-created β Notification Service β Customer Notification Kafka Topic: inventory-reserved β Notification Service β Inventory Update Notification
Symptom: Services can't connect to Kafka
Error: Connection to broker failed
Solutions:
- Ensure Kafka is running:
docker-compose ps - Check Kafka health:
docker-compose logs kafka - Verify port 9092 is accessible:
netstat -an | findstr 9092
Symptom: Service fails to start
Error: Unable to bind to port
Solutions:
- Check if ports are in use:
netstat -an | findstr 5001 - Kill processes using the ports
- Change ports in
appsettings.jsonordocker-compose.yml
Symptom: Docker build fails
Error: Unable to restore packages
Solutions:
- Clear Docker cache:
docker system prune -a - Rebuild without cache:
docker-compose build --no-cache - Check internet connection for package downloads
- URL: http://localhost:8080
- View topics, messages, consumer groups
- Monitor message flow in real-time
# View logs for specific service
docker-compose logs order-service
docker-compose logs inventory-service
docker-compose logs notification-service
# Follow logs in real-time
docker-compose logs -f kafka# Check Order Service
Invoke-RestMethod -Uri "http://localhost:5001/api/orders" -Method GET
# Check Inventory Service
Invoke-RestMethod -Uri "http://localhost:5002/api/inventory" -Method GET
# Check Notification Service
Invoke-RestMethod -Uri "http://localhost:5003/api/notifications/test" -Method GET# List topics
docker exec kafka kafka-topics --bootstrap-server localhost:9092 --list
# Consume messages from a topic
docker exec kafka kafka-console-consumer --bootstrap-server localhost:9092 --topic order-created --from-beginning
# Produce test message
docker exec kafka kafka-console-producer --bootstrap-server localhost:9092 --topic order-created- Monitor service logs for performance indicators
- Use Application Insights or Prometheus for production
- Use Kafka UI for basic monitoring
- Monitor consumer lag
- Check message throughput
β Before Starting:
- Docker Desktop is running
- No other services using ports 5001-5003, 8080, 9092, 2181
- Sufficient disk space (>2GB for Docker images)
β If Issues Occur:
- Check Docker container status:
docker-compose ps - Review service logs:
docker-compose logs [service-name] - Verify Kafka topics exist in Kafka UI
- Test individual service endpoints
- Check network connectivity between containers
β For Development:
- .NET 8 SDK installed
- NuGet packages restored
- Environment variables set correctly
- Local Kafka instance accessible
{
"eventId": "guid",
"timestamp": "datetime",
"eventType": "OrderCreatedEvent",
"order": {
"id": "guid",
"customerId": "string",
"items": [
{
"productId": "string",
"productName": "string",
"quantity": "number",
"price": "decimal"
}
],
"totalAmount": "decimal",
"createdAt": "datetime",
"status": "string"
}
}{
"eventId": "guid",
"timestamp": "datetime",
"eventType": "InventoryReservedEvent",
"orderId": "guid",
"reservedItems": [
{
"productId": "string",
"quantityReserved": "number"
}
]
}- Add Kafka Packages: Install
Confluent.KafkaNuGet package to enable actual Kafka communication - Database Integration: Add Entity Framework for persistent storage
- Authentication: Implement JWT authentication across services
- API Gateway: Add an API Gateway for unified access
- Monitoring: Integrate Application Insights or ELK stack
- Circuit Breaker: Implement resilience patterns
- Event Sourcing: Add event store for full audit trail
- Apache Kafka Documentation
- Confluent.Kafka .NET Client
- Microservices Patterns
- .NET Microservices Guide
Happy Coding! π