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

Leave a Reply

Your email address will not be published. Required fields are marked *