TheShop is a very basic Multi-Tenant e-shop demonstrating how to build a polyglot system. This sample application demonstrates how to build a multi-tenant with the tenancy handled at the Identity Server (Openid-Connect) and Database layer.
You'd need to create the following DNS entries in your /etc/hosts
file:
- Windows:
C:\Windows\System32\drivers\etc\hosts
- Linux:
/etc/hosts
You must have
Administrator
orsudo
privileges to edit this file
- 127.0.0.1 theshop.com
- 127.0.0.1 dulcet.theshop.com
- 127.0.0.1 aparel.theshop.com
- 127.0.0.1 resk.theshop.com
If you're using the Visual Studio Code devcontainer
extension, just run Remote-Containers: Reopen in Container
.
Otherwise start up the containers with ``docker-compose up -d. This will startup
postgres`, `nginx` and
`keycloak`.
Build all projects with
> gradlew assemble
You need to run the tasks
subproject to run the database migrations
> gradlew :tasks:bootRun
After that run the storefront
> gradlew :shops:bootRun
If the application fails to run
- Confirm the database migrations run. The database should be running on port
8080
withjulius:julius123
asusername:password
combination. - Confirm the keycloak realm
tenant
is created and a confidential clientshop_service
is created. Copy the client secret and update the propertyspring.security.oauth2.client.registration.shop.client-secret=:id
.
At the database layer, multi-tenancy can be handled in one of three ways:
- Tenant per database - Each tenant has his own database
- Tenant per schema - Each tenant has his own schema, sharing the same database
- Tenant shares database, schema and tables - In this approach, each tenant does not have a unique database or schema, but joins the same pool of data, with each tenant's data identified by a discriminator column in the table
-- TODO image here
I decided to go with the first approach to data handling. Each tenant will have his own database.
I took it one step further, by creating a microservice based application, with each service owing its data. Each service also has a database created per tenant.
The following services will be created
- Shops Service - Also serves as the entrypoint and catalog area. This list all the shops (tenants)
- Payment Service
- Orders Service
- Basket Servoce
- Location Service
- Marketing Service
- Tenants Service