This is a Quarkus project in Java 21 that implements dynamic tenants functionality, allowing connections to different databases. Each tenant has a separate PostgreSQL database, and migrations are managed using Flyway and Agroal libraries. Tenant registration is performed through REST requests, and to test the application, we have a car API that performs selects on the databases related to each tenant.
Make sure you have the following requirements installed on your machine before proceeding:
- Java 21
- Quarkus Framework 3.4.3
- PostgreSQL 15
- Flyway
- Agroal (Connection pool management library)
- Model Mapper
- Lombok
- Hibernate with Panache
- Resteasy with Jackson
- Virtual Threads
To use this project, you will need to have a PostgreSQL server running and configure the connection details in the application.properties
file. Quarkus will automatically create the tenant's database when a new tenant is created.
Follow the steps below to install and run the application:
- Clone the project repository:
git clone https://github.com/Willian199/sample-multitenacy
cd sample-multitenacy
-
Configure the database information in the
application.properties
file. -
Run the Quarkus application:
.\mvnw.cmd quarkus:dev
- The Quarkus server will start, and migrations will be executed in a separate thread.
GET http://localhost:8080/tenantController/listAll
DELETE http://localhost:8080/tenantController/delete/{id}
POST http://localhost:8080/tenantController/save
The request body should contain the Tenant details in JSON format, for example:
{
"id": 1,
"tenantId": "tenant1",
"datasourceHost": "jdbc:postgresql://localhost:5432/",
"datasourceName": "tenant_1",
"datasourceUsername": "user",
"datasourcePassword": "password",
"flagSSL": true
}
GET http://localhost:8080/carController/listAll
DELETE http://localhost:8080/carController/delete/{id}
POST http://localhost:8080/carController/save
The request body should contain the Car details in JSON format, for example:
{
"id": 5,
"brand": "chevrolet",
"model": "camaro",
"year": 2022,
"engineType": "gasoline engine",
"transmissionType": "manual"
}
This project demonstrates how to create a Quarkus application with dynamic tenants support, connecting to different databases for each tenant. Flyway library is used for managing migrations, executing migrations in a separate thread and using an efficient connection pool provides a more efficient and scalable development experience.
Welcome any feedback, suggestions, or contributions. If you have ideas for improving this project or implementing additional features, please feel free to open GitHub issues or submit pull requests.