SpringBoot Multi Tenancy with JdbcTemplate - Dynamically RoutingDataSource - AbstractRoutingDataSource
Introduction to multi tenant applications
What are multi tenant apps?
Multi tenant applications allow you to serve multiple customers with one install of the application. Each customer has their data completely isolated in such an architecture. Each customer is called a tenant.
Most modern Software as a Service applications are multi tenant. Whether it is Salesforce, Freshbooks, Zoho or Wordpress, most modern cloud based applications are delivered with a multi-tenant architecture.
What about if you've got your application done in SpringBoot and you sell it to 10 costumers which want to use your application, there would be several ways to do this, one way would be a deployment per customer with its own database and so on, and what about if those10 costumers later turn into 30 and you must do an important change to the application, then you would have to make 30 redeploys of the same application,
so to avoid this kind of thing there is a solution turning our application into a multitenant.
We can develop multi-tenant applications using SpringBoot, where we can select the database at runtime based on the user. For example, when a request comes from client1 then select client1’s database, the request comes from client2 then select client2’s database and so on. but for this, we need a specific identifier for each client’s request identifier also knows as the tenant.
* We can pass the tenant as a request parameter, like ?tenantId=client1 or ?tentantId=client2
* We can store the tenant in session once user login and fetch from the session whenever it is needed.
Let's create 2 databases db1, and db2 and let's create a table on each database like this
and let's fill the tables with some data with https://mockaroo.com/
let's go to https://start.spring.io/ and let's create project naming MultitenantJdbcTemplate adding the next dependencies to the pom, I am using MySQL you can choose any other database engine, or you can even have several at the same time
let's create this hierarchy folder
next chapter we are going to be doing the same process but for JPA and JdbcTemplate at the same time
What are multi tenant apps?
Multi tenant applications allow you to serve multiple customers with one install of the application. Each customer has their data completely isolated in such an architecture. Each customer is called a tenant.
Most modern Software as a Service applications are multi tenant. Whether it is Salesforce, Freshbooks, Zoho or Wordpress, most modern cloud based applications are delivered with a multi-tenant architecture.
What about if you've got your application done in SpringBoot and you sell it to 10 costumers which want to use your application, there would be several ways to do this, one way would be a deployment per customer with its own database and so on, and what about if those10 costumers later turn into 30 and you must do an important change to the application, then you would have to make 30 redeploys of the same application,
so to avoid this kind of thing there is a solution turning our application into a multitenant.
We can develop multi-tenant applications using SpringBoot, where we can select the database at runtime based on the user. For example, when a request comes from client1 then select client1’s database, the request comes from client2 then select client2’s database and so on. but for this, we need a specific identifier for each client’s request identifier also knows as the tenant.
* We can pass the tenant as a request parameter, like ?tenantId=client1 or ?tentantId=client2
* We can store the tenant in session once user login and fetch from the session whenever it is needed.
Let's create 2 databases db1, and db2 and let's create a table on each database like this
and let's fill the tables with some data with https://mockaroo.com/
let's go to https://start.spring.io/ and let's create project naming MultitenantJdbcTemplate adding the next dependencies to the pom, I am using MySQL you can choose any other database engine, or you can even have several at the same time
application.properties
let's create this hierarchy folder
DataSourceMap.java
DataSourceMap inside the package TenantDataSources will return the Map of data source where key contains tenantId and value contain data source. The datasource object contains a database driver, database URL, username and password.
CustomRoutingDataSource.java
When any database operation is performed then it automatically comes here, This method will return a tenantId as a String based on that database will be selected to perform the operations. We are maintaining tenantId in URL so reading from URL as a parameter using ServletRequestAttributes
@SpringBootApplication
Let's open the main java application if you gave it the same name as suggested then lets open MultitenantJdbcTemplateApplication.java and let's create a bean DataSource of class CustomRoutingDataSource, TargetDataSources indicate Key and Value pair, where the key is tenantId and value is a data source.
Model - User.java
Controller - UserController.java
Dao - UserDAO.java
Let's tested with Postman
Let's get the data from the users table from the database db1
http://localhost:8085/multitenant/users/5?tenantId=tenantId1
http://localhost:8085/multitenant/users/5?tenantId=tenantId2
you can download the whole code from GitHub in https://github.com/juandavidmarin368/SpringBoot-MultitenantJdbcTemplate
next chapter we are going to be doing the same process but for JPA and JdbcTemplate at the same time
Comments
Post a Comment