Dynamic DataSource Routing: Spring Boot
Sometimes we need to create a web application that can connect to multiple databases. We might need to connect multiple databases based on the client, customers so on.
Spring provides AbstractRoutingDataSource class which we can extend and achieve the data source routing dynamically and we can do it easily on Spring Boot.
Use cases
Sometimes for security reasons, performance, scalability, or isolation, we need data source routing such as MASTER and REPLICA DB where REPLICA DB can only be read-only.
Another use case is the real-time processing data of satellites. These are very huge volume data sets and created very frequent on an interval.
Let’s take the satellite SARAL(https://en.wikipedia.org/wiki/SARAL) and Aryabhata (https://en.wikipedia.org/wiki/Aryabhata_(satellite)) example which pushed the data on a very frequent interval of time. Suppose we have an application that can retrieve the data of the satellite and each satellite has its own database. To achieve this, we can take the help of Data source routing.
Each satellite creates a huge set of files on their FTP server (https://www.mosdac.gov.in/open-data) and based on the type, we want to retrieve the files.
In this article, we will provide the steps to configure multiple data sources dynamically using Spring Boot & JPA.
For the complete example find the code from GitHub.
Maven Dependencies
Here is the list of maven dependencies. We used MySQL Database and Spring MVC.
Find it from GitHub.
Conclusion
Spring boot provides additional configuration support which we can do if we want to configure multi-data source connectivity dynamically.
For the complete example find the code from GitHub.
No comments:
Post a Comment