0

How to manage configuration in a spring boot application for multiple tenant? Is this achievable using spring config server?

e.g. enter image description here

rDX
  • 228
  • 1
  • 4
  • 15

1 Answers1

0

Based on my understanding, we can use the configuration from the config server by adding tenant specific properties and loading them from a git repository or other custom location (can be identified by the classpath etc)

Please refer the section

Dynamic Tenant identification - Simple approach in the blogpost where we identify the tenant from the URL and then load the necessary configuration for the given tenant from the file. This approach is a simpler one for understanding, however in the production ready application, we will have to manage this reading process to be thread safe and cleanup to be done efficiently.

I did some exploration long back and noted down the points in the post, hope it can be of help to you.

Sample Tenant specific configuration for Tenant 1

# application-tenant1.properties
greeting.message=Hello from Tenant 1!

Sample Tenant specific configuration for Tenant 2

# application-tenant2.properties
greeting.message=Hello from Tenant 2!

Reading the settings for each tenant from the config

@RestController
public class MyAppController {
    
@Value("${greeting.message}")
private String greetingMessage;

@GetMapping("/greeting/{tenant}")
public String getGreetingMessage(@PathVariable String tenant) {
    // Set the active profile dynamically based on the tenant
    System.setProperty("spring.profiles.active", tenant);
    try {
        // Business logic using the tenant-specific profile
        return greetingMessage;
    } finally {
        // Clean up the active profile to avoid affecting subsequent requests
        System.clearProperty("spring.profiles.active");
    }
}
}

If you wanted to have dynamic configuration to be loaded for each tenant once the tenant identification is found as part of each request via a filter or other approach, we can load the config from db and set up a tenant context similar to the authentication one and use it for each request which will be more robust and easy to scale for dynamic tenant management.

Saravanan
  • 7,637
  • 5
  • 41
  • 72