Open In App

Using Native Configuration in Spring Cloud Config Server

Last Updated : 08 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring Cloud Config provides both server-side and client-side support for externalized configuration in distributed systems. The Config Server centralizes management of configuration properties for applications across various environments. By default, Spring Cloud Config uses Git for storage, but it also supports local file storage through the native profile.

In this article, we'll explore how to utilize the native configuration profile in Spring Cloud Config Server. This approach allows you to store configuration files locally on the server's file system instead of a remote Git repository.

Prerequisites

  • Basic understanding of the Spring Boot framework.
  • Familiarity with Spring Cloud components.
  • Java Development Kit (JDK) installed.
  • Maven for dependency management.
  • IntelliJ IDEA IDE installed.

What is Native Configuration?

In Spring Cloud Config, the native profile allows the Config Server to read configuration properties from the local file system. This is particularly useful when remote Git repositories or other external storage services are not required, such as in development or testing environments.

How Does It Work?

When configured to use the native profile, the Config Server searches for configuration files in a specified directory on the server. These files can be in formats such as YAML, properties, or JSON. The Config Server then serves these configuration files to client applications.

Key Terminologies

  • Spring Cloud Config: Provides centralized management of configuration files, enabling easier updates and consistency across multiple services and environments.
  • Config Server: A Spring Boot application that provides HTTP-based APIs for external configuration, serving configuration properties to client applications on demand.
  • Native Profile: A configuration option that allows the server to read configuration properties from the local file system, useful for development and testing.
  • Profiles: Used to segregate configuration for different environments (e.g., dev, prod).
  • Properties Files: Plain text files storing configuration data as key-value pairs. Commonly used in Java applications.
  • YAML Files: A human-readable data serialization format often used in Spring applications for configuration due to its readability and hierarchical structure.
  • HTTP API: Provides endpoints for client applications to request configuration data based on application name, profile, and label.
  • Property Sources: Locations where the Config Server loads configuration properties, including local files and external repositories.
  • Environment: A collection of profiles and configuration properties managed centrally.
  • Centralized Configuration: Manages configuration properties from a single location, simplifying management and ensuring consistency.
  • Client-Side Configuration: Properties consumed by client applications, fetched from the Config Server during startup.
  • Configuration Repository: Storage for configuration files. In the native profile, it's a directory on the local file system.

Implementation of the Spring Project Using Native Configuration

Step 1: Configure the External Properties File

Create the application.yml file and add the property for microservice1. Save this file in the config-repo located on the local system.

microservice1:
  property: Microservice1 Property Value

Step 2: Setup The Spring Boot Project

Create a new Spring Boot project using IntelliJ IDEA with the following options:

  • Name: spring-native-config-server
  • Language: Java
  • Type: Maven
  • Packaging: Jar

Click on the Next button.

Project Metadata

Step 3: Add the Dependencies

Add the following dependencies to the Spring Boot project:

Add Dependencies

Step 4: Project Structure

After creating the project, the file structure should look like the following image.

Project Folder Structure

Step 5: Configure the Application Properties

Rename application.properties to application.yml and add the following configuration for the Config Server:

server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        native:
          search-locations: file:///C:/Users/Syam/OneDrive/Desktop/Spring Projects/spring-native-config-server/config-repo
      label: main
  profiles:
    active: native

logging:
  level:
    org.springframework.cloud.config: DEBUG

Step 6: Main Class

In the main class, add the @EnableConfigServer annotation to activate the Config Server functionalities:

Java
package com.gfg.springnativeconfigserver;

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

@SpringBootApplication
@EnableConfigServer
public class SpringNativeConfigServerApplication {

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

}

pom.xml

XML
<?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.3.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gfg</groupId>
    <artifactId>spring-native-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-native-config-server</name>
    <description>spring-native-config-server</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Step 7: Run the application

After completing the setup, run the application, which will start at port 8888.

Application Runs

Config Client Application Implementation

Step 1: Setup The Spring Boot Project

Create a new Spring Boot project named microservice1 with the following options:

  • Name: microservice1
  • Language: Java
  • Type: Maven
  • Packaging: Jar

Click on the Next button.

Project Metadata

Step 2: Add the Dependencies

Add the following dependencies to the client Spring Boot project:

Select Dependencies

Step 3: Project Structure

The project structure should resemble the provided image.

Project Folder Structure

Step 4: Configure the Application Properties

Rename application.properties to application.yml and configure the Config Server URL:

spring:
  application:
    name: microservice1
  cloud:
    config:
      uri: http://localhost:8888
  config:
    import: configserver:http://localhost:8888

Step 5: Main Class

Rename application.properties to application.yml and configure the Config Server URL:

Java
package com.gfg.microservice1;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Microservice1Application {

   @Value("${microservice1.property}")
    private String property;



    @GetMapping("/property")
    public String getProperty() {
        return property;
    }

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

}

pom.xml

XML
<?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.3.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gfg</groupId>
    <artifactId>microservice1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>microservice1</name>
    <description>microservice1</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Step 6: Run the application

After setting up the project, run the application, which will start at port 8080.

Application Runs

Step 7: Testing the Endpoint

Use Postman or another tool to test the endpoint:

GET http://localhost:8080/property

Output:

property GET Request

Conclusion

Using the native profile in Spring Cloud Config Server provides a straightforward way to manage configuration properties locally, without relying on a remote Git repository. By following the steps outlined, you can efficiently manage configurations in a development or local testing environment.


Next Article

Similar Reads