Using Native Configuration in Spring Cloud Config Server
Last Updated :
08 Aug, 2024
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.
Step 3: Add the Dependencies
Add the following dependencies to the Spring Boot project:
Step 4: Project Structure
After creating the project, the file structure should look like the following image.
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
.
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.
Step 2: Add the Dependencies
Add the following dependencies to the client Spring Boot project:
Step 3: Project Structure
The project structure should resemble the provided image.
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
.
Step 7: Testing the Endpoint
Use Postman or another tool to test the endpoint:
GET http://localhost:8080/property
Output:
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.
Similar Reads
Customizing the Configuration Location in Spring Cloud Config Server
Spring Cloud Config provides both server and client-side support for externalized configuration in distributed systems. The Config Server manages configuration properties for applications across various environments, allowing them to retrieve their configuration from a central location. By default,
5 min read
Managing Configuration for Microservices with Spring Cloud Config
Spring Cloud Config provides the centralized external configuration management system and it is designed to work well with modern microservices architectures. It is the part of larger Spring Config suite of the tools that aim to help the developers built the cloud-native applications. Spring Cloud C
4 min read
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 config
4 min read
Dynamic Configuration Updates with Spring Cloud Config
In a microservices architecture, managing configuration properties across multiple services can become challenging, especially when these configurations need to be updated frequently. Spring Cloud Config provides a centralized configuration management solution, allowing you to manage external proper
6 min read
Implementing Configuration Versioning with Spring Cloud Config
Configuration versioning is the critical aspect of maintaining and managing the configurations in the microservices architecture. It can allow the teams to track the changes, revert to previous versions and manage the configurations more efficiently, Spring Cloud Config can provide a powerful way to
5 min read
Encrypting Sensitive Configuration Data in Spring Cloud Config
Encrypting sensitive configuration data in Spring Cloud Config is essential for securing information like passwords, API keys, and other credentials. This extra layer of protection is crucial because it helps prevent unauthorized access and ensures that sensitive data remains safe, even if the confi
4 min read
Using Git as a Backend for Spring Cloud Config Server
Spring Cloud Config Server is a powerful tool and it provides server-side as well as client-side support for externalized configuration in a distributed system. It allows us to manage the configurations of multiple applications in a centralized way. By using Git as a backend, you can version the con
5 min read
Spring Boot - Cloud Configuration Server
In microservices, we have several different services responsible for different functionalities. These services act like smaller application modules, which together form the application. Each of these modules has its own responsibility, based on the business logic of the application being built. Thes
10 min read
Monitoring and Logging in Spring Cloud Config Server
In Microservices, Spring Cloud Server can play a crucial role in Spring microservices by providing centralized configuration management. It helps externalize configuration properties across all environments for applications, making them easier to manage and update without redeploying or restarting t
9 min read
Configuring Spring MVC with XML and Java Config
Spring MVC (Model-View-Controller) is a web framework within the Spring Framework that enables the development of web applications following the MVC design pattern. It separates application logic into three components: Model, View, and Controller, making it easier to maintain, test, and scale.Spring
6 min read