4

I have ONE spring boot (1.5.4.RELEASE) project using java 8 deployed on AWS HPC. This project architect scope works for Spring Web Application(Website), Rest API Services(Mobile Developer) & Account Administration for Company. So there is 3 different respective Database like (2-SQL Server & 1-MySQL).

Here on stack-overflow, I'm posting my question for find a best way to implementation this Spring-Boot Project by help of talented stack-overflow users.

Here is my configure properties files.

application.properties

#For Public Website
spring.datasource.clone.web.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.clone.web.url = jdbc:sqlserver://127.0.0.01\\dbo:1433;databaseName=PROD_WEB;
# Username and password
spring.datasource.web.username = web
spring.datasource.web.password = ED5FLW64ZU976Q36

#For Rest API
spring.datasource.clone.url = jdbc:mysql://localhost:3306/PROD_REST;
# Username and password
spring.datasource.clone.username = rest
spring.datasource.clone.password = Firewall77#


#For Account Administration for Company Users
spring.datasource.admin.url = jdbc:sqlserver://127.0.0.01\\dbo:1433;databaseName=PROD_ADMIN;
# Username and password
spring.datasource.admin.username = admin
spring.datasource.admin.password = Firewall77#

# Backup & Cron Policy
...

I would greatly appreciate for some very good suggestion to implement it. your knowledge on this subject would help me, Thanks.

2 Answers2

3

You need to implement two different beans, one for each datasource and make them take the corresponding configuration properties respectively:

  1. First bean will be responsible for the first datasource configuration, and should be daclared as primary datasource with @Primary, so it can be setup as the main datasource for the project.
  2. The second bean will configure the second datasource.

This is how they should be implemented in Spring:

@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource.web")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.datasource.rest")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

Here's how should be your application.properties configured, to take these two beans configuration into account:

#For Public Website
spring.datasource.web.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.web.url = jdbc:sqlserver://127.0.0.01\\dbo:1433;databaseName= PROD_WEB;
# Username and password
spring.datasource.web.username = web
spring.datasource.web.password = ED5FLW64ZU976Q36

#For Rest API
spring.datasource.rest.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.rest.url = jdbc:mysql://localhost:3306/PROD_REST;
# Username and password
spring.datasource.rest.username = rest
spring.datasource.rest.password = Firewall77#

Note:

I changed the configuration properties here so they can be differentiated between web and rest datasources:

  1. Properties starting with spring.datasource.web will be dedicated to configure the first datasource.
  2. Properties starting with spring.datasource.rest will be dedicated to configure the second datasource.
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Thanks @chsdk for your support, But I'm worried about users traffic (Now We testing by Beta tester creating 1000 to 1500 request per sec, some of request connection timeout) and If one of my database disconnected in worst case. I want confirmed you that I'm already implemented this way as you post, Also we going implemented AWS SQS in this project. Let me know If Is there other way to support best. –  Oct 05 '17 at 12:25
  • @SamDev In my case we are working with this configuration, it works perfectly, we used it in a way that we should read from only one datasource and update both databases so when there's an update or an insertio it will be inserted in both databases, but honestly I haven't got the case where ther will one of the datasources is disconnected, but normally it won't have a problem if the primary one is there, according huge number of requests, it won't have any side effect with this configuration. – cнŝdk Oct 05 '17 at 13:26
  • Yes, But some request stuck and throw connection read timeout by a testing tool –  Oct 05 '17 at 13:29
  • @SamDev I don't know but as far as I know this is the best way to configure two datasources in your spring application, take a look at: [***Spring Boot Configure and Use Two DataSources** answers*](https://stackoverflow.com/a/30344608/3669624) on stackoverflow. – cнŝdk Oct 05 '17 at 13:32
1

try to use DataSource configuration specified at Configuration for each data sources for more help check this Using multiple datasources with Spring Boot and Spring Data

baba
  • 265
  • 3
  • 9