Creating Profiles in Spring Cloud Config Server

Spring Cloud Config provides both server-side and client-side support for external systems in a distributed system. Using the Config Server, we can manage the configuration for multiple applications from the central place. Profiles in the Spring Cloud Config can allow you to provide different configurations for the various environments such as development, testing, and production of applications.

Spring Cloud Config

Spring Cloud Config provides both server-side and client-side support for external systems in a distributed system. With the Spring Cloud Config, we can manage the configurations for multiple applications from the central place. It ensures consistency and easier management across different development, testing, and production environments.

Profiles in Spring Cloud Config

Profiles in the Spring Cloud Config allows us to define different sets of configuration properties for different environments. For example, We can might have different database settings, API endpoints or feature toggles for the development, testing and production environments.

Working of Spring Cloud Config

  • Config Server: The Spring Boot application that runs the Spring Cloud Config server. It can fetches the configuration properties from the repository and the serves them to the client applications.
  • Config Repository: The Git repository where the configurations files are stored then the repository contains the properties files for the different profiles like application-dev.yml, application-test.yml and application-prod.yml.
  • Client Applications: Spring Boot applications that can request the configuration properties from the config server. These applications can specify their active profiles and receive the appropriate configuration properties.

Implementation to Create Profiles in Spring Cloud Config Server

We will develop the simple spring boot application that can demonstrates the creating the profiles in spring cloud config server of the project.

Step 1:

Create a new Spring Boot project using Spring Initializr and add the required dependencies,

  • Spring Web
  • Spring Cloud Config Server
  • Lombok
  • Spring DevTools

After the creation of the project is done, the folder structure will be like below.


Step 2: Setting up the Configuration Repository

Create the Git repository and add the configuration files for the different profiles of the spring application.

1. application-dev.yml

spring:
config:
activate:
on-profile: prod
cloud:
config:
server:
git:
uri: https://github.com/your-repo/config-repo
search-paths: dev
default-label: dev
message: "This is the development profile"


2. application-prod.yml

spring:
config:
activate:
on-profile: prod
cloud:
config:
server:
git:
uri: https://github.com/your-repo/config-repo
search-paths: prod
default-label: prod
message: "This is the production profile"


3. application-test.yml

spring:
config:
activate:
on-profile: test
cloud:
config:
server:
git:
uri: https://github.com/your-repo/config-repo
search-paths: test
default-label: test

message: "This is the test profile"


Step 3: Setup the Client application

Open the application.properties rename to application.yml and configure the config server uri of the project.

server:
port: 8888

spring:
cloud:
config:
server:
git:
uri: https://github.com/iammahesh123/config-repo.git

message: "This is the default profile"


Step 4: Main Class

Open the main class and add the @EnableConfigServer annotation to activate the config server functionalities of the spring application.

Java
package org.example.springconfigprofiles;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SpringConfigProfilesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringConfigProfilesApplication.class, args);
    }

}


pom.xml:

Java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>spring-config-profiles</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-config-profiles</name>
    <description>spring-config-profiles</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


Step 5: Run the Application

Now, we will run the application and it will start at port 8888.


Step 6: Verify the Profiles

Default Profile

GET http://localhost:8888/application/default

Output:

Below is the Postman output of default api.


Development Profile

GET http://localhost:8888/application/dev

Output:

Below is the Postman output of dev api.


Production Profile

GET http://localhost:8888/application/prod

Output:

Below is the Postman output of prod api.


Testing Profile

GET http://localhost:8888/application/test

Output:

Below is the Postman output of test api.

This example project demonstrates how to use the Spring Cloud Config Server to the serve different configurations based on the active profile of the client application. We can extend this setup to the include the other properties and configurations as needed.



Contact Us