Categories: Uncategorised

Spring Cloud Config Server – JDBC Backend

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.

  • {application}: The name of the application.
  • {profile}: The active profile for the application.
  • {label}: (Optional) A Git label. It represents a specific version or branch in the Git repository.

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

mahendravarman.m@gmail.com

Recent Posts

Spring Webflux Functional Endpoint – File Upload

In this blog using the Spring WebFlux module, we are going to leverage the functional…

9 months ago

Serverless Functions with Spring Cloud Function, AWS Lambda

Spring Cloud Function is a project within the Spring ecosystem that allows developers to build…

9 months ago

Spring Boot + RabbitMQ – Decoupling Microservices Communication

RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP).…

9 months ago

Spring Integration – Sending files over SFTP

Spring Integration is a powerful extension of the Spring Framework designed to support the implementation…

9 months ago

Spring Cloud Config Client

The Spring Cloud Config Client is a component of the Spring Cloud framework that enables…

10 months ago

Handling CSV in Python

In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.…

10 months ago