Search code examples
spring-bootdistributed-database

application.properties configuration for distributed database pattern


I am trying to develop a microservice by using sprin and spring boot with postgresql database. I am here using distributted datbase. So for particular region I am using one DB, and for other region I am using different DB. Currently I only tried with one database. I added datasource name , username and password in application.properties.

Here my doubt is that, if I am using multiple distributed database, how cam mention different DB source URL in configuration (application.properties)? I am using following structure to use one database currently,

spring.datasource.url=jdbc:postgresql://localhost/milleTech_users
spring.datasource.username=postgres
spring.datasource.password=postgresql
spring.jpa.generate-ddl=true

Like above.

So if I am using multiple DB for multiple region How I can give configuration conditionally here? I am new to microservice world and distributed database design pattern.


Solution

  • Multiple Database details cannot be managed within a single application.properties.

    Consider using Spring Cloud Config where in you can create multiple application.properties with different profile names for every application.

    In your case, the profile names could reflect the region. When you deploy to a particular region, launch the app with that profile name so that the required config would be loaded and appropriate database connection would be used

    Edit :

    Also in your case, if you can set environment variables, you can explore on the following option mentioned in this thread