This project is a Spring Boot application designed to manage car data. It supports multi-tenancy, allowing different tenants (e.g., VW and BMW) to manage their car inventories independently. Each tenant has its own dedicated H2 in-memory database.
This README will guide you through setting up, running, and interacting with the application.
- Java 11 or higher
- Gradle (the project uses the Gradle wrapper, so a local installation is not strictly required)
To build the project, navigate to the root directory and run:
./gradlew buildThis command will compile the code and package it into a JAR file.
You can run the application in a few ways:
- Using Gradle:
./gradlew bootRun
- Running the JAR file:
After building the project, you can find the JAR file in the
build/libs/directory. Run it using:(Replacejava -jar build/libs/multi-tenant-datasource-0.0.1-SNAPSHOT.jar
multi-tenant-datasource-0.0.1-SNAPSHOT.jarwith the actual JAR file name if it differs).
The project includes a run.sh script that demonstrates basic API interactions. Once the application is running, you can execute this script from the root directory:
./run.shThis script will:
- List cars for the 'vw' tenant.
- Add a new car ('tiguan') for the 'vw' tenant.
- List cars for the 'vw' tenant again to show the newly added car.
- List cars for the 'bmw' tenant.
- Add a new car ('X5') for the 'bmw' tenant.
- List cars for the 'bmw' tenant again.
The application exposes the following RESTful API endpoints for managing cars:
- URL:
/cars - Method:
GET - Headers:
X-Tenant: Specifies the tenant whose cars you want to list (e.g.,vworbmw).
- Description: Retrieves a list of all cars for the specified tenant.
- Example:
curl --request GET --header "X-tenant: vw" localhost:8080/cars
- URL:
/cars - Method:
POST - Headers:
X-Tenant: Specifies the tenant for whom you are adding the car (e.g.,vworbmw).Content-Type:application/json
- Body: A JSON object representing the car to be added.
{ "name": "car_name", "color": "car_color" } - Description: Adds a new car to the inventory of the specified tenant.
- Example:
curl --request POST --header "X-Tenant: vw" --header "Content-Type: application/json" --data '{"name":"tiguan","color":"black"}' localhost:8080/cars
This application uses a multi-tenant database architecture, where each tenant has its own isolated H2 in-memory database. This ensures data separation and security between different tenants.
The database configurations for each tenant are defined in the src/main/resources/application.yaml file. Here's an example snippet:
spring:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
flyway:
enabled: false # Flyway is currently disabled
tenants:
datasources:
vw: # Tenant 'vw'
jdbcUrl: jdbc:h2:mem:vw # In-memory H2 database named 'vw'
driverClassName: org.h2.Driver
username: sa
password: password
bmw: # Tenant 'bmw'
jdbcUrl: jdbc:h2:mem:bmw # In-memory H2 database named 'bmw'
driverClassName: org.h2.Driver
username: sa
password: passwordKey points about the database setup:
- In-Memory: The H2 databases are in-memory, meaning data will be lost when the application stops. This is suitable for development and testing. For production, you would typically switch to a persistent database.
- Tenant-Specific Schemas: Each tenant (
vw,bmw) has its own JDBC URL (jdbc:h2:mem:vw,jdbc:h2:mem:bmw), effectively creating separate database instances. - Flyway: Database migrations with Flyway are currently disabled (
flyway: enabled: false). If you enable it, you would typically manage schema changes undersrc/main/resources/db/migration/for each tenant.
Contributions to this project are welcome! If you have suggestions for improvements or find any issues, please feel free to:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes.
- Commit your changes and push them to your fork.
- Submit a pull request.
Please ensure your code follows the existing style and that any new features are appropriately tested.