Let's walk through a basic example of how to set up and use Spring Cloud Config
Server to manage configuration for multiple tenants in a Spring Boot
Application.
Step 1: Create a Spring Cloud Config Server
Create a new Spring Boot project with the Spring Cloud Config Server dependency.<!-- pom.xml --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>Configure the Spring Cloud Config Server:
// src/main/java/com/example/ConfigServerApplication.java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }Configure the application properties to specify the Git repository where configuration files are stored:
# src/main/resources/application.yml spring: cloud: config: server: git: uri: <URL to your Git repository>
Step 2: Create Configuration Files for Tenants
In your Git repository, organize configuration files based on tenants and profiles:config-repo/ |-- application-tenant1.properties |-- application-tenant2.properties |-- application.yml
Example content for application-tenant1.properties:
# application-tenant1.properties greeting.message=Hello from Tenant 1!
Example content for application-tenant2.properties:
# application-tenant2.properties greeting.message=Hello from Tenant 2!
Step 3: Create a Spring Boot Application
Create a new Spring Boot project for the multi-tenant application:<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Other dependencies --> </dependencies>Configure the application to use the Spring Cloud Config Client:
# src/main/resources/bootstrap.yml spring: application: name: myapp cloud: config: uri: http://localhost:8888 # URL to your Config ServerCreate a controller to retrieve the configuration property:
// src/main/java/com/example/MyAppController.java import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyAppController { @Value("${greeting.message}") private String greetingMessage; @GetMapping("/greeting") public String getGreetingMessage() { return greetingMessage; } }
Step 4: Test the Application
- Start the Spring Cloud Config Server application.
- Start the multi-tenant Spring Boot application.
-
Access the greeting message for each tenant using the /greeting endpoint:
- For Tenant 1: http://localhost:8080/greeting
- For Tenant 2: http://localhost:8080/greeting
Dynamic Tenant identification - Simple approach
To dynamically load the configuration for different tenants based on a URL
parameter or path segment, you can use Spring Cloud Config's native support
for profiles and the application name to achieve this. Here's how you can
modify the configuration to achieve dynamic loading of tenant-specific
configurations:
Below given is a sample yaml configuration that can be used to dynamically identify the tenant
# src/main/resources/bootstrap.yml spring: cloud: config: uri: http://localhost:8888 application: name: myapp # The base application name profiles: active: @tenant # Placeholder for the active profile
Tenant Identification from URL
// src/main/java/com/example/MyAppController.java import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @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"); } } }
Access Configuration via URL Path:
Now you can access the tenant-specific configuration by specifying the
tenant as a path segment in the URL. For example:
- For Tenant 1: http://localhost:8080/greeting/tenant1
- For Tenant 2: http://localhost:8080/greeting/tenant2
Comments
Post a Comment