2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mr.DevEng
  • 2,651
  • 14
  • 57
  • 115

1 Answers1

3

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

CGS
  • 2,782
  • 18
  • 25
  • Ok I understood. Is there any documentation that you can share for implementing multiple application.properties using spring cloud as you mentioned in your answer? – Mr.DevEng Mar 01 '18 at 12:16
  • here you go http://www.baeldung.com/spring-cloud-configuration . Pls accept if the answer is useful ! – CGS Mar 01 '18 at 12:23
  • Ok. Will check this link. Thank you for your response. – Mr.DevEng Mar 01 '18 at 12:27
  • I checked the spring cloud config server properties file creation. I understood that one. But I have still doubt , It is receiving in client controller with @value anotation. How it can assign to client properties file spring.datasource.url variable ?. Can I set directly value annotation in config client application.propoerties file? – Mr.DevEng Mar 02 '18 at 11:58
  • And also how to assign spring cloud config server database variable directly to spring cloud client application.property file variable? – Mr.DevEng Mar 02 '18 at 13:09
  • 1
    The property file that you are creating in the Spring Cloud Server is going to be replaced at the client when the App initializes. To understand this, you can copy over your current application.property file, change few values and place it in the Cloud Config Server. When the app initializes, you will see the new values – CGS Mar 05 '18 at 02:48
  • Ok. I am exploring your suggestions. Any way thank you for your responses. Its helping me. But You mentioned in your answer with "launch the app with that profile name" . Is there any documentation for launching app with property file from cloud server?. I am confused that if I am creating two properties file for different region, how I can launch app with that property separately ? – Mr.DevEng Mar 05 '18 at 11:26
  • 1
    java -jar -Dspring.profiles.active=<> application.jar . for more options refer https://stackoverflow.com/questions/31038250/setting-active-profile-and-config-location-from-command-line-in-spring-boot – CGS Mar 05 '18 at 11:28
  • Ok. I understood. But I am using docker container for building images and deploying my microservice in containers.There I am creating Dockerfile by "ENTRYPOINT ["java", "-jar", "dockerdemo.war"]" . So need to modify that? – Mr.DevEng Mar 05 '18 at 11:34
  • https://stackoverflow.com/questions/43381244/entrypoint-with-environment-variables-is-not-acepting-new-params – CGS Mar 06 '18 at 02:58
  • Mr.DevEng , I came across a similar situation . I want to process DB records using spring batch , but the Current system has Active - Active replication for DB which becomes my source. How do we configure dataSource in my spring batch application such that , if the active DB is failed it should point to the next DB . I am not familiar with distributed DB setup. Were you able to manage a similar situation using spring cloud config ? – Tomz Sep 19 '22 at 05:18