Skip to content

Dropwizard bunlde which helps to achieve multi-tenancy in dropwizard application

Notifications You must be signed in to change notification settings

anandhi/dw-tenant-resolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 

Repository files navigation

dw-tenant-resolver

Dropwizard bundle which helps to achieve multi-tenancy in dropwizard application

Configuration:

Add the database configuration for each tenant, database configuration accepts all the properties which dropwizard DatabaseConfiguration object accepts - .

Shown in below example.

multiTenantDataSourceConfiguration:
  tenantHeaderName: X_TENANT_ID
  enforceTenantHeaderInAllRequests: true
  databaseConfigurations:
    tenant_1:
      driverClass: com.mysql.jdbc.Driver
      user: root
      password:
      url: jdbc:mysql://db_server_1:port/database_tenant_1
    tenant_2:
      driverClass: com.mysql.jdbc.Driver
      user: root
      password:
      url: jdbc:mysql://db_server_2:port/databse_tenant_2

Above configuration accepts, tenantHeaderName - which is been used the resolve the tenant for each request and initializes the necessary things to connect to defined datasource.

and enforceTenantHeaderInAllRequests defines - whether headerName is optional or mandatory in all requests. If value is true and tenantHeaderName is missing, then request will be rejected, and 400 response will be sent with the message Invalid Tenant Nil

Adding the tenant-resolver bundle to the Application:

During the initialization of dropwizard application, add tenant-resolver bundle, as mentioned below -

@Override
public void initialize(Bootstrap<DropwizardServiceConfiguration> bootstrap) {
    
    bootstrap.addBundle(new MultiTenantResolverBundle<DropwizardServiceConfiguration>() {
        @Override
        protected MultiTenantDataSourceConfiguration getMultiTenantDataSourceConfiguration(
                                                        DropwizardServiceConfiguration configuration) {
            return configuration.getMultiTenantDataSourceConfiguration();
        }

        @Override
        protected List<String> getPackagesToScan() {
            List<String> packagesToScan  = new LinkedList<String>();
            packagesToScan.add("com.sample.service.model");
            packagesToScan.add("com.sample.service.extensions");
            return packagesToScan;
        }
    });

and define the Configuration in Service Configuration like -

@Valid
private MultiTenantDataSourceConfiguration multiTenantDataSourceConfiguration = 
                                             new MultiTenantDataSourceConfiguration();

and define the provider for the same like -

@Provides
@Singleton
MultiTenantDataSourceConfiguration providesMultiTenantDsConfigurations(
                           Provider<DropwizardServiceConfiguration> provider) {
    return provider.get().getMultiTenantDataSourceConfiguration();
}

How to use

If you want to access the entityManager, where this snippet is getting ececuted in the scope of request and request has tenantHeader present -

TenantResolver.getEntityManager()

and if you explicitly want to use the specific tenant's entity manager -

TenantResolver.getEntityManager(tenantName)

About

Dropwizard bunlde which helps to achieve multi-tenancy in dropwizard application

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages