SpringBoot Multi Tenancy with JPA and JdbcTemplate - Dynamically reading RoutingDataSource from a data base table
The last 2 posts were about how to do a Multitenant mode with JPA and JdbcTemplate but the dataSources were hardcoded, what we are going to be doing in this post is to be able to read the dataSources from a database instead of being hardcoded.
Let's supposed you've got a SaaS Application and there are more and more customers who want to use your application, and you want to create a customer in an automatically way, with this post you are going to be able to do part of it, so that for doing a process like that you would need a DevOps for sure when dealing with new DNS and deploying a database structure to start with and may other things.
what this post will cover is doing 2 SpringBoot Applications, the first one,it is a Dynamic Database Manager which is going to get the url, the tenantId, the username and password of the tenants.
the second SpringBoot Application the MultitenantJPA-and-JdbcTemplate which we did in the previous post, the only difference is that its dataSources are not hardcoded any longer, each time the application starts it reads the dataSources through a request done to the first SpringBoot Application DynamicDBManager the one who is the responsible for getting back all tenants details to MultitenantJPA-and-JdbcTemplate.
Let's gets started creating the first SpringBoot Application the DynamicDBManager
Note: Before starting let's create or use a database in which you will have the details about your tenants, for this purpose I am using a database called data and the table to save the tenant settings is
Tenant Details
let's got to https://start.spring.io/ and create a project in Artifact called DynamicDBManager and let's replace in Project Metadata com.example by com.managerdb
let's create this hierarchy folder
let's fill the tenant_details table
now with Postman let's get the databases with the endPoint localhost:8090/dynamic/databases
ok, so as you can see now we can our tenants details from this SpringBoot Application called DynamicDBManager, as always you get the whole of both projects in GitHub on this link https://github.com/juandavidmarin368/DYNAMIC-MULTITENANT
the next SpringBoot Application is called MultitenantJPA-and-JdbcTemplate and it is the samething as we did in the previous link, there is just a change in the package package com.tenancy.MultitenantJPAandJdbcTemplate.MultiTenantSettings.TenantDataSources; in the class DataSource.java so that is the one is loading the DataSources but through an HttpGet request to the first SpringBoot Application
and there were added 2 more dependencies to the pom.xml file
endpoint getting data from the tenant or Database db2
http://localhost:8085/multitenant/users/8?tenantId=tenantId2
endpoint getting data from the tenant or Database db3
http://localhost:8085/multitenant/users/8?tenantId=tenantId3
you can get the both projects from this link on GitHub https://github.com/juandavidmarin368/DYNAMIC-MULTITENANT next posts will be covering how to do the process to deploy a blueprint database for doing a better automatic deployment, and how to create a service to auto restart the SpringBoot Application to refresh the new added databases to the datasource Map
Let's supposed you've got a SaaS Application and there are more and more customers who want to use your application, and you want to create a customer in an automatically way, with this post you are going to be able to do part of it, so that for doing a process like that you would need a DevOps for sure when dealing with new DNS and deploying a database structure to start with and may other things.
what this post will cover is doing 2 SpringBoot Applications, the first one,it is a Dynamic Database Manager which is going to get the url, the tenantId, the username and password of the tenants.
the second SpringBoot Application the MultitenantJPA-and-JdbcTemplate which we did in the previous post, the only difference is that its dataSources are not hardcoded any longer, each time the application starts it reads the dataSources through a request done to the first SpringBoot Application DynamicDBManager the one who is the responsible for getting back all tenants details to MultitenantJPA-and-JdbcTemplate.
Let's gets started creating the first SpringBoot Application the DynamicDBManager
Note: Before starting let's create or use a database in which you will have the details about your tenants, for this purpose I am using a database called data and the table to save the tenant settings is
Tenant Details
let's got to https://start.spring.io/ and create a project in Artifact called DynamicDBManager and let's replace in Project Metadata com.example by com.managerdb
let's create this hierarchy folder
JDBC - Model - Database.java
JDBC - Dao - DatabaseDAO.java
JDBC - Dao - DatabaseController
let's fill the tenant_details table
now with Postman let's get the databases with the endPoint localhost:8090/dynamic/databases
ok, so as you can see now we can our tenants details from this SpringBoot Application called DynamicDBManager, as always you get the whole of both projects in GitHub on this link https://github.com/juandavidmarin368/DYNAMIC-MULTITENANT
the next SpringBoot Application is called MultitenantJPA-and-JdbcTemplate and it is the samething as we did in the previous link, there is just a change in the package package com.tenancy.MultitenantJPAandJdbcTemplate.MultiTenantSettings.TenantDataSources; in the class DataSource.java so that is the one is loading the DataSources but through an HttpGet request to the first SpringBoot Application
and there were added 2 more dependencies to the pom.xml file
let's test it now with POSTMAN
endpoint getting data from the tenant or Database db1
http://localhost:8085/multitenant/users/8?tenantId=tenantId1
endpoint getting data from the tenant or Database db2
http://localhost:8085/multitenant/users/8?tenantId=tenantId2
endpoint getting data from the tenant or Database db3
http://localhost:8085/multitenant/users/8?tenantId=tenantId3
you can get the both projects from this link on GitHub https://github.com/juandavidmarin368/DYNAMIC-MULTITENANT next posts will be covering how to do the process to deploy a blueprint database for doing a better automatic deployment, and how to create a service to auto restart the SpringBoot Application to refresh the new added databases to the datasource Map
Comments
Post a Comment