A production-ready starter template for building scalable Spring Boot applications using a multi-module Maven architecture.
- Multi-module Maven project with clean separation of concerns
- Core modules providing shared infrastructure
- A service (
user-service
) demonstrating best practices - Complete integration between all components
- Spring Boot 3.2.0 - Application framework
- Maven Multi-Module - Build and dependency management
- PostgreSQL - Primary database
- Flyway - Database migrations
- Apache Kafka - Event streaming
- HashiCorp Vault - Secrets management
- Testcontainers - Integration testing
- Spring JDBC - Direct database access
- Docker - Containerization ready
spring-microservices-starter/
βββ pom.xml # Parent POM with dependency management
β
βββ core/ # Shared infrastructure modules
β βββ core-database/ # PostgreSQL + Flyway + JDBC utilities
β βββ core-messaging/ # Kafka producers/consumers
β βββ core-vault/ # Vault secret management
β βββ core-testing/ # Testcontainers setup
β
βββ services/ # Deployable applications
β βββ user-service/ # Sample microservice
β
βββ docker-compose.yml # Local development environment
- Java 17+
- Docker & Docker Compose
- Maven 3.6+
docker compose up -d
This starts PostgreSQL, Kafka, Zookeeper, and Vault.
# Unseal Vault and create secrets
curl -X POST http://localhost:8200/v1/sys/init \
-d '{"secret_shares": 1, "secret_threshold": 1}'
# Use the root token from response to authenticate
export VAULT_TOKEN="your-root-token"
# Build all modules
./mvnw clean install
# Run the user service
./mvnw spring-boot:run -pl user-service
# Create a user
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "[email protected]"}'
# Get all users
curl http://localhost:8080/api/users
- PostgreSQL connection management with HikariCP
- Flyway migrations auto-applied on startup
- JDBC utilities for direct database access
- Health checks and monitoring
Key Features:
- Connection pooling and optimization
- Database migration versioning
- Transaction management
- Query utilities and row mappers
- Kafka integration with Spring Kafka
- Producer/Consumer abstractions for easy usage
- Serialization handling (JSON by default)
- Error handling and retry logic
Key Features:
- Topic auto-creation for development
- Dead letter queue patterns
- Message tracing and monitoring
- Async processing support
- HashiCorp Vault integration for secrets
- Dynamic secret management with auto-refresh
- Database credential rotation support
- Configuration property injection
Key Features:
- Automatic token renewal
- KV secrets engine support
- Database secrets engine integration
- Environment-based configuration
- Testcontainers setup for integration tests
- Test data management utilities
- Mock configurations for unit tests
- Test profiles for different scenarios
Key Features:
- PostgreSQL test containers
- Kafka test containers
- Vault test setup
- Data cleanup utilities
A complete REST API demonstrating:
- CRUD operations with proper HTTP methods
- Database integration using core-database
- Event publishing using core-messaging
- Secret management using core-vault
- Comprehensive testing using core-testing
API Endpoints:
GET /api/users
- List all usersGET /api/users/{id}
- Get user by IDPOST /api/users
- Create new userPUT /api/users/{id}
- Update userDELETE /api/users/{id}
- Delete user
# Database
POSTGRES_URL=jdbc:postgresql://localhost:5432/userdb
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=postgres
# Kafka
KAFKA_BOOTSTRAP_SERVERS=localhost:9092
# Vault
VAULT_URI=http://localhost:8200
VAULT_TOKEN=your-vault-token
Each service has its own application.yml
with:
- Database configuration
- Kafka settings
- Vault integration
- Actuator endpoints
- Logging configuration
- Service layer testing with mocks
- Repository testing with H2
- Controller testing with WebMvcTest
- Full application context with Testcontainers
- Database migrations applied automatically
- Kafka message testing with embedded broker
- End-to-end API testing
Run Tests:
# All tests
./mvnw test
# Integration tests only
./mvnw test -Dtest="*IT"
# Specific service tests
./mvnw test -pl user-service
Each service includes a Dockerfile
:
cd services/user-service
docker build -t user-service:latest .
docker run -p 8080:8080 user-service:latest
Ready for Kubernetes deployment with:
- ConfigMaps for configuration
- Secrets for sensitive data
- Services for internal communication
- Ingress for external access
mkdir -p services/order-service/src/main/java
cd services/order-service
# Copy and modify user-service structure
<modules>
<module>services/user-service</module>
<module>services/order-service</module>
</modules>
Reference only the core modules you need:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>core-database</artifactId>
</dependency>
<!-- Add other core modules as needed -->
</dependencies>
- Clean separation between core and services
- Provided scope for core dependencies
- Interface-based design for flexibility
- Event-driven communication between services
- Vault integration for secret management
- Connection pooling with security best practices
- Input validation and sanitization
- Proper error handling without information leakage
- Health checks for all components
- Metrics via Spring Actuator
- Structured logging with correlation IDs
- Distributed tracing ready
- Test pyramid with unit, integration, and E2E tests
- Testcontainers for realistic testing
- Test data builders for maintainable tests
- Separate test configurations
- Each service is stateless and can be scaled independently
- Database connection pooling handles multiple instances
- Kafka consumer groups distribute message processing
- JVM tuning parameters in Dockerfile
- Connection pool sizing based on load
- Cache configuration for performance
/actuator/health
- Overall application health/actuator/health/db
- Database connectivity/actuator/health/kafka
- Kafka connectivity/actuator/health/vault
- Vault connectivity
- JVM metrics, database pool metrics
- Kafka consumer/producer metrics
- Custom business metrics
- Request/response metrics
- Vault unsealed: Ensure Vault is initialized and unsealed
- Database migrations: Check Flyway migration status
- Kafka connectivity: Verify broker accessibility
- Port conflicts: Ensure ports 8080, 5432, 9092, 8200 are available
# Enable debug logging
export LOGGING_LEVEL_COM_EXAMPLE=DEBUG
# View container logs
docker compose logs -f postgres
docker compose logs -f kafka
- Add more services following the established patterns
- Implement API Gateway for centralized routing
- Add service discovery (Consul/Eureka)
- Implement circuit breakers (Resilience4j)
- Add distributed tracing (Zipkin/Jaeger)
- Set up CI/CD pipeline with automated testing
This starter template provides a solid foundation for enterprise Spring Boot applications. Feel free to extend it based on your specific requirements!
Happy coding! π