8.4 Production Profiles & Configuration
← Back to Chapter 8: Testing, Deployment & Production-Ready Spring Boot
Deploying enterprise services into cloud environments (Kubernetes, AWS ECS, GCP Cloud Run) mandates adherence to the Twelve-Factor App methodology, specifically externalizing all secrets and environment configurations.
1. The Twelve-Factor Config Principle
"Store config in the environment."
A codebase should be deployed to multiple environments without modifying a single line of code or recompiling the jar archive.
Rule of Environment Variables in Spring Boot:
Spring Boot relaxes property names so that environment variables automatically map to YAML properties:
- Property
spring.datasource.password➔ Environment variableSPRING_DATASOURCE_PASSWORD - Property
app.mail.sender-address➔ Environment variableAPP_MAIL_SENDER_ADDRESS
2. Secrets Management & Vault Integration
Never commit API keys, database credentials, or private JWT signing secrets to Git repositories:
# application-prod.yml
spring:
datasource:
url: ${DB_JDBC_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
hikari:
maximum-pool-size: 20
minimum-idle: 5
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
app:
jwt:
secret-key: ${JWT_SIGNING_SECRET}
In Kubernetes environments, these environment variables are populated dynamically from Kubernetes Secrets or external secret managers (HashiCorp Vault, AWS Secrets Manager).
3. Graceful Shutdown Configuration
When a container is terminated during Kubernetes rolling deployments, pods receive a SIGTERM signal. Without graceful shutdown, active in-flight HTTP requests are severed abruptly:
server:
shutdown: graceful # Waits for active requests to finish
spring:
lifecycle:
timeout-per-shutdown-phase: 30s # Max grace period before force SIGKILL
When configured:
- Embedded Tomcat stops accepting new inbound connections immediately.
- In-flight HTTP transactions are allowed up to 30 seconds to complete gracefully.
- Database pools and thread pools are cleanly terminated.