Spring Cloud Config Server provides a centralized location for managing and serving configuration information to client applications in microservices and distributed systems. It supports various backends for storing configuration data, such as Git, SVN, JDBC, and more. Clients can request their configuration from the Config Server, and the server responds with the appropriate configuration based on the application’s name and profile.
Spring Cloud Config Server exposes an HTTP-based API that allows client applications to retrieve their configurations. The API is RESTful and follows a simple convention. Clients can request configuration properties for specific applications, profiles, and labels.
To set up a Spring Cloud Config Server, follow these steps:
i) Add required dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
ii) Annotate main class with @EnableConfigSever
package com.example.springconfigserverdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class SpringConfigServerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConfigServerDemoApplication.class, args);
}
}
iii) Spring Cloud Config Server supports various backends for storing configuration data, and one of them is using a JDBC (Java Database Connectivity) backend. This means we can store the configuration data in a relational database, and the Config Server can fetch configurations from there. The relational DB that is used is PostgreSQL . Add the necessary dependencies for the same.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
iv) Configure database connection details in the application properties file
spring.cloud.config.server.jdbc.enabled=true
spring.profiles.active=jdbc
spring.datasource.url=jdbc:postgresql://localhost:5432/configuration
spring.datasource.username=${USERNAME}
spring.datasource.password=${PASSWORD}
spring.application.name=spring-config-server-demo
spring.cloud.config.server.jdbc.sql=SELECT KEY,VALUE FROM PROPERTIES WHERE APPLICATION=? AND PROFILE=? AND LABEL=?
v) Create table PROPERTIES with columns application, profile, label, key, value and store the required configurations
vi) Finally configuration can be fetched from the endpoint
http://<config-server-host>:<config-server-port>/<application>/<profile>
For complete source code: https://github.com/MMahendravarman/Springboot_Examples
In this blog using the Spring WebFlux module, we are going to leverage the functional…
Spring Cloud Function is a project within the Spring ecosystem that allows developers to build…
RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP).…
Spring Integration is a powerful extension of the Spring Framework designed to support the implementation…
The Spring Cloud Config Client is a component of the Spring Cloud framework that enables…
In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.…