When you build modern microservices with Spring Boot, your application often interacts with multiple infrastructure components — databases, caches, message brokers, authentication services, and more.
Instead of hardcoding configurations, Spring Boot uses an elegant externalized configuration mechanism via application.properties
(or application.yml
).
In this blog, we’ll see practical examples of configuring:
- MySQL
- MongoDB
- Redis
- Apache Kafka
- JWT authentication
- OAuth2
1. MySQL Configuration
Spring Boot auto-configures DataSource
and JPA if it detects spring-boot-starter-data-jpa
in your classpath.
# ===============================
# MySQL Database Configuration
# ===============================
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA / Hibernate
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
Notes:
-
ddl-auto=update
is good for dev, but usevalidate
ornone
in production. -
show-sql=true
helps in debugging SQL queries.
2. MongoDB Configuration
If you add spring-boot-starter-data-mongodb
, Spring Boot will create a MongoTemplate automatically.
# ===============================
# MongoDB Configuration
# ===============================
spring.data.mongodb.uri=mongodb://localhost:27017/mydatabase
spring.data.mongodb.database=mydatabase
3. Redis Configuration
Spring Boot + spring-boot-starter-data-redis
lets you easily set up Redis caching or key-value store usage.
# ===============================
# Redis Configuration
# ===============================
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=
spring.data.redis.timeout=60000
4. Apache Kafka Configuration
For message-driven microservices using Kafka (spring-kafka
dependency):
# ===============================
# Kafka Configuration
# ===============================
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=my-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
5. JWT Configuration
JWT isn’t auto-configured by Spring Boot — you define properties for your custom security configuration.
# ===============================
# JWT Configuration
# ===============================
jwt.secret=my-super-secret-key
jwt.expiration=3600000 # 1 hour in milliseconds
jwt.refresh-expiration=604800000 # 7 days
6. OAuth2 Configuration
For OAuth2 login with Google, GitHub, or other providers (spring-boot-starter-oauth2-client
):
# ===============================
# OAuth2 Configuration (Google Example)
# ===============================
spring.security.oauth2.client.registration.google.client-id=your-client-id
spring.security.oauth2.client.registration.google.client-secret=your-client-secret
spring.security.oauth2.client.registration.google.scope=email,profile
spring.security.oauth2.client.registration.google.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}
spring.security.oauth2.client.provider.google.authorization-uri=https://accounts.google.com/o/oauth2/auth
spring.security.oauth2.client.provider.google.token-uri=https://oauth2.googleapis.com/token
spring.security.oauth2.client.provider.google.user-info-uri=https://www.googleapis.com/oauth2/v3/userinfo
7. Logging for Debugging
While integrating multiple services, detailed logging can be invaluable.
# ===============================
# Logging Configuration
# ===============================
logging.level.root=INFO
logging.level.org.springframework=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
8. Profile-Specific Configuration
You can create environment-specific property files:
application-dev.properties
application-prod.properties
Yaml Based configuration
Got it — I’ll extend the blog so it now includes a .yml
version of all those configurations.
We’ll keep the structure parallel so readers can easily map .properties
to .yml
syntax.
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.