Giter VIP home page Giter VIP logo

multi-tenancy's Introduction

Getting Started

Technology

  • Spring boot
  • Postgresql
  • MySQL
  • Jooq

PostgreSQL: Application Implementation

  • Same database, same schema, same table, filter by tenant_id column
  • Auto extend SQL, add where tenant_id in or and tenant_id in conditions
  • Check tenant_id related conditions when we select,update, delete tenant tables
  • See multi-tenancy-library and springboot-postgres-jooq

Install Postgresql with Docker

docker run --name postgres -e POSTGRES_PASSWORD=123456 -e TZ=PRC -p 5432:5432 postgres:latest

Create Table and Insert Data into Table

Start Application

Check Results

Configuration

multi:
  tenancy:
    # enable multi tenancy
    enabled: true
    # tenant table tenant related column name
    tenant-identifier: tenant_id
    # tenant tables
    tables:
      - public.t_user
      - public.t_order
    # check tenant condition exist in SQL or not
    sql-check-filters-exist: true
    # auto add tenant condition to SQL
    sql-auto-add-filters: true

Scheduler

  • If there are SQL releated schedulers in application, we can not get tenantID through request.getHeader("X-TenantID") in WebMvcConfig
  • We need to add MultiTenancyStorage.setTenantID(tenantID) before scheduler logic and add MultiTenancyStorage.setTenantID(null) after scheduler logic
  • Please check springboot-postgres-jooq/src/main/java/com/example/springbootpostgresjooq/job/ScheduledJob.java for detailed information

PostgreSQL: Row Level Security Implementation

  • Same database, same schema, same table, filter by tenant_id column
  • Using PostgreSQL Row Level Security
  • See rls

Create Table and Insert Data into Table

Enable RLS and Add Policies

Start Application

  • Start rls application

Check Results

MySQL: Application Implementation

  • Same database, same schema, same table, filter by tenant_id column
  • Auto extend SQL, add where tenant_id in or and tenant_id in conditions
  • Check tenant_id related conditions when we select,update, delete tenant tables
  • See multi-tenancy-library and springboot-mysql-jooq

Install MySQL with Docker

docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

Create Table and Insert Data into Table

Start Application

Check Results

Configuration

multi:
  tenancy:
    # enable multi tenancy
    enabled: true
    # tenant table tenant related column name
    tenant-identifier: tenant_id
    # tenant tables
    tables:
      - multi_tenancy.t_user
      - multi_tenancy.t_order
    # check tenant condition exist in SQL or not
    sql-check-filters-exist: true
    # auto add tenant condition to SQL
    sql-auto-add-filters: true

Scheduler

  • If there are SQL releated schedulers in application, we can not get tenantID through request.getHeader("X-TenantID") in WebMvcConfig
  • We need to add MultiTenancyStorage.setTenantID(tenantID) before scheduler logic and add MultiTenancyStorage.setTenantID(null) after scheduler logic
  • Please check springboot-mysql-jooq/src/main/java/com/example/springbootmysqljooq/job/ScheduledJob.java for detailed information

multi-tenancy's People

Contributors

springmonster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

excelletaorg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.