Micronaut Multitenancy

Help with common tasks such as tenant resolution for multi-tenancy aware Micronaut applications.

Version:

1 Release History

For this project, you can find a list of releases (with release notes) here:

2 Introduction

Multi-Tenancy, as it relates to software development, is when a single instance of an application is used to service multiple clients (tenants) in a way that each tenant’s data is isolated from the other.

To use the Micronaut’s multitenancy capabilities you must have the multitenancy dependency on your classpath.

implementation("io.micronaut:micronaut-multitenancy")
<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-multitenancy</artifactId>
</dependency>

A common requirement for supporting Multi-tenancy is the ability to resolve the current tenant. Micronaut ships with the following built-in TenantResolvers:

name

description

CookieTenantResolver

Resolves the current tenant from an HTTP cookie. See CookieTenantResolver Configuration Properties.

FixedTenantResolver

Resolves against a fixed tenant id. See FixTenantResolver Configuration Properties.

HttpHeaderTenantResolver

Resolves the current tenant from the request HTTP Header. See FixTenantResolver Configuration Properties.

PrincipalTenantResolver

Resolves the current tenant from the authenticated username. See PrincipalTenantResolver Configuration Properties.

SessionTenantResolver

Resolves the current tenant from the HTTP session. See SessionTenantResolver Configuration Properties.

SubdomainTenantResolver

Resolves the tenant id from the subdomain. See SubdomainTenantResolver Configuration Properties.

SystemPropertyTenantResolver

Resolves the tenant id from a system property. See SystemPropertyTenantResolver Configuration Properties.

Micronaut supports tenant propagation. As an example, take the following scenario:

multitenancy

You want incoming requests to the gateway microservice to resolve the tenant id via subdomain. However, you want your requests to other internal microservices to include the tenant Id as an HTTP Header.

Your configuration in the gateway microservice will look like:

micronaut:
  multitenancy:
    propagation:
      enabled: true
      service-id-regex: 'catalogue'
    tenantresolver:
      subdomain:
        enabled: true
    tenantwriter:
      httpheader:
        enabled: true

In the catalogue microservice the configuration will look like:

micronaut:
  multitenancy:
    tenantresolver:
      httpheader:
        enabled: true

To propagate the tenant you will need to write the resolved tenant ID to the outgoing requests.

Currently, Micronaut ships with two built-in implementations for TenantWriter:

name

description

CookieTenantWriter

Writes the current tenant to a Cookie in your outgoing requests. See CookieTenantWriter Configuration Properties.

HttpHeaderTenantWriter

Writes the current tenant to a HTTP Header. See HttpHeaderTenantWriter Configuration Properties.

3 Multi-Tenancy GORM

GORM supports Multi-tenancy and integrates with Micronaut Multi-tenancy support.

See the Micronaut Groovy module for more information.

4 Quick Start

5 Repository

You can find the source code of this project in this repository: