This project demonstrates a microservices architecture using Spring Boot with centralized logging using the ELK (Elasticsearch, Logstash, Kibana) stack. It includes two microservices (Order Service and Inventory Service) that communicate via Kafka events.

This project now includes three distinct logging environments following industry best practices:
- π§ Development (dev): Verbose logging for debugging and development
- π§ͺ Staging: Balanced logging for testing and monitoring
- π Production (prod): Minimal logging for performance and security
Each environment has its own:
- Spring Boot configuration
- Docker Compose setup
- Logging levels and formats
- Manages customer orders
- Publishes order events to Kafka
- RESTful API for CRUD operations
- Database:
order_db
- Manages product inventory
- Consumes order events from Kafka
- Updates stock levels automatically
- Database:
inventory_db
- PostgreSQL: Two separate databases for each service
- Kafka: Event streaming platform for inter-service communication
- Elasticsearch: Log storage and search engine
- Logstash: Log processing and transformation
- Kibana: Log visualization and analysis
- Filebeat: Log collection and forwarding
- Docker and Docker Compose
- Java 17 or higher
- Maven 3.6 or higher
# Start all services (PostgreSQL, Kafka, ELK stack)
docker-compose up -d
# Build and run the Spring Boot services
cd order && mvn clean package -DskipTests && java -jar target/order-0.0.1-SNAPSHOT.jar
cd ../inventory mvn clean package -DskipTests && java -jar target/inventory-0.0.1-SNAPSHOT.jar
# Start development infrastructure
docker compose -f docker-compose.yml -f docker-compose-dev.yml up -d
# Run services with dev profile
cd order && mvn clean package -DskipTests && java "-Dspring.profiles.active=dev" -jar "target\order-0.0.1-SNAPSHOT.jar"
cd ../inventory && mvn clean package -DskipTests && java "-Dspring.profiles.active=dev" -jar "target\inventory-0.0.1-SNAPSHOT.jar"
# Start staging infrastructure
docker compose -f docker-compose.yml -f docker-compose-staging.yml up -d
# Run services with staging profile
cd order && mvn clean package -DskipTests && java "-Dspring.profiles.active=staging" -jar "target\order-0.0.1-SNAPSHOT.jar"
cd ../inventory && mvn clean package -DskipTests && java "-Dspring.profiles.active=staging" -jar "target\inventory-0.0.1-SNAPSHOT.jar"
# Start production infrastructure
docker compose -f docker-compose.yml -f docker-compose-production.yml up -d
# Run services with production profile
cd order && mvn clean package -DskipTests && java "-Dspring.profiles.active=prod" -jar "target\order-0.0.1-SNAPSHOT.jar"
cd ../inventory && mvn clean package -DskipTests && java "-Dspring.profiles.active=prod" -jar "target\inventory-0.0.1-SNAPSHOT.jar"
GET /api/orders - Get all orders
GET /api/orders/{id} - Get order by ID
GET /api/orders/customer/{email} - Get orders by customer email
POST /api/orders - Create new order
PUT /api/orders/{id}/status - Update order status
DELETE /api/orders/{id} - Delete order
GET /api/products - Get all products
GET /api/products/{id} - Get product by ID
POST /api/products - Create new product
PUT /api/products/{id} - Update product
DELETE /api/products/{id} - Delete product
POST /api/products/{id}/stock - Update stock quantity
curl -X POST http://localhost:8081/api/products \
-H "Content-Type: application/json" \
-d '{
"name": "Test Product",
"description": "A test product",
"price": 29.99,
"stockQuantity": 100,
"category": "Test"
}'
curl -X POST http://localhost:8080/api/orders \
-H "Content-Type: application/json" \
-d '{
"customerName": "John Doe",
"customerEmail": "[email protected]",
"orderItems": [
{
"productId": 1,
"quantity": 2
}
]
}'
curl http://localhost:8081/api/products/1
- Open Kibana at the appropriate port for your environment
- Go to Discover section
- Create index pattern based on your environment:
- Development:
microservices-logs-dev-*
- Staging:
microservices-logs-staging-*
- Production:
microservices-logs-prod-*
- Development:
- View and search logs from both services
Logs include:
- Service name and operation
- Request/response details
- Kafka event publishing/consumption
- Database operations
- Error details with stack traces
- Log Level: DEBUG, TRACE, INFO, WARN, ERROR
- SQL Logging: Enabled with parameter binding
- Console Output: Colored, detailed formatting
- Performance: Detailed timing and metrics
- Log Level: INFO, WARN, ERROR
- SQL Logging: Disabled for performance
- Console Output: Standard formatting
- Performance: Balanced timing
- Log Level: ERROR, WARN only
- SQL Logging: Completely disabled
- Console Output: Minimal formatting
- Performance: Optimized for speed
Each environment has its own infrastructure configuration with different ports:
- PostgreSQL: Port 5432
- PG Admin: Port 5050
- PostgreSQL: Port 5433
- PG Admin: Port 5051
- PostgreSQL: Port 5434
- PG Admin: Port 5052
Note: Each environment connects to its own database instance, ensuring complete isolation between environments.
# View all logs for specific environment
index:microservices-logs-dev-*
index:microservices-logs-staging-*
index:microservices-logs-prod-*
# Filter by service
fields.service:order-service
fields.service:inventory-service
# Filter by log level
level:ERROR
level:WARN
level:INFO
level:DEBUG
# Search for specific operations
message:*order created*
message:*SQL*
message:*Performance Metric*
elk_springboot/
βββ order/ # Order Service
β βββ src/main/java/com/learn/order/
β β βββ controller/ # REST controllers
β β βββ service/ # Business logic
β β βββ entity/ # JPA entities
β β βββ repository/ # Data access
β β βββ dto/ # Data transfer objects
β β βββ event/ # Kafka events
β β βββ config/ # Configuration
β βββ src/main/resources/
β β βββ application.properties # Default config
β β βββ application-dev.properties # Development config
β β βββ application-staging.properties # Staging config
β β βββ application-prod.properties # Production config
β βββ pom.xml
βββ inventory/ # Inventory Service
β βββ src/main/java/com/learn/inventory/
β β βββ controller/ # REST controllers
β β βββ service/ # Business logic
β β βββ entity/ # JPA entities
β β βββ repository/ # Data access
β β βββ kafka/ # Kafka consumers
β β βββ config/ # Configuration
β β βββ service/LoggingService.java # Environment-aware logging
β βββ src/main/resources/
β β βββ application.properties # Default config
β β βββ application-dev.properties # Development config
β β βββ application-staging.properties # Staging config
β β βββ application-prod.properties # Production config
β βββ pom.xml
βββ docker-compose.yml # Default infrastructure
βββ init-db.sql # Database initialization
βββ logstash/ # Logstash configuration
β βββ pipeline/
β β βββ logstash.conf # Default pipeline
β βββ config/
βββ filebeat/ # Filebeat configuration
β βββ filebeat.yml # Default config
βββ logs/ # Application logs
βββ README.md # This file
- Order Service:
jdbc:postgresql://localhost:5432/order_db
- Inventory Service:
jdbc:postgresql://localhost:5432/inventory_db
- Credentials: postgres/password
- Bootstrap Servers: localhost:9092
- Topic:
order-created
- Consumer Group:
inventory-group
- Log Files:
logs/{service-name}-{environment}.log
- Log Level: Environment-specific (see Environment-Specific Logging section)
- Format: Structured logging with timestamps and correlation IDs
- Services won't start: Check if PostgreSQL and Kafka are running
- Database connection failed: Verify database is created and accessible
- Kafka connection failed: Ensure Kafka are running
- Logs not appearing in Kibana: Check Filebeat and Logstash status
- Wrong environment configuration: Ensure correct profile is active
# Check service status for specific environment
docker-compose -f docker-compose-dev.yml ps
docker-compose -f docker-compose-staging.yml ps
docker-compose -f docker-compose-prod.yml ps
# View service logs for specific environment
docker-compose -f docker-compose-dev.yml logs -f
docker-compose -f docker-compose-staging.yml logs -f
docker-compose -f docker-compose-prod.yml logs -f
# Restart specific service
docker-compose -f docker-compose-dev.yml restart [service-name]
# Stop all services for specific environment
docker-compose -f docker-compose-dev.yml down
docker-compose -f docker-compose-staging.yml down
docker-compose -f docker-compose-prod.yml down
# Clean up volumes for specific environment
docker-compose -f docker-compose-dev.yml down -v
docker-compose -f docker-compose-staging.yml down -v
docker-compose -f docker-compose-prod.yml down -v
This project demonstrates:
- Microservices Architecture: Service separation and communication
- Event-Driven Communication: Using Kafka for asynchronous messaging
- Centralized Logging: ELK stack integration
- Environment-Specific Configuration: Different logging levels for different environments
- Database Design: Separate databases per service
- RESTful APIs: CRUD operations implementation
- Docker Orchestration: Multi-service deployment
- Log Aggregation: Collecting logs from multiple services
- Real-time Monitoring: Log analysis and visualization
Feel free to submit issues and enhancement requests!
This project is for educational purposes.