If you are working with a Spring based micro-services application then having a centralized service to manage configuration of all your micro-services at a central place is very important. Spring Cloud Config supports multiple backends such as filesystem, git repository, s3 bucket, relational databases via JDBC, etc.
Almost all the tutorials you find on the internet are about the git backend. However, sometimes, you want to use a JDBC backend for your config as relational databases are universally available and well supported. The aim of this tutorial is to show how to use Spring Cloud config with JDBC as a backend. We will use the PostgreSQL Database for this purpose.
Spring Cloud Config Server Example
So let’s start by creating a new project with Spring Initializer. Let’s go to https://start.spring.io/ and create a new Spring Project with Maven, Spring Boot 3.0.1 and JDK 17.
Please enter the following data:
- Project: Maven
- Language: Java
- Group:
com.mi
- Artifact:
cloud-config-demo
- Name:
cloud-config-demo
- Description: Demo project for Spring Boot
- Package Name:
com.mi.cloudconfigdemo
- Packaging: Jar
- Java: 17
- Dependencies:
- Config Server
- Spring Data JDBC SQL
- Spring Web
- Spring Security
- PostgreSQL Driver
Then click on Generate Project.
To enable the cloud config server we need to use the @EnableConfigServer
annotation to our class which has @SpringBootApplication
.
File: ./src/main/java/com/mi/cloudconfigdemo/CloudConfigDemoApplication.java
package com.mi.cloudconfigdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class CloudConfigDemoApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigDemoApplication.class, args); } }
Now we need to add the required properties to our application.properties
file.
File: ./src/main/resources/application.properties
server.port=8888 spring.security.user.name=configUser spring.security.user.password=configPass spring.datasource.hikari.connection-timeout=5000 spring.datasource.hikari.maximum-pool-size=10 spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/cloud-config?useSSL=false spring.datasource.username=postgres spring.datasource.password=mobisoft spring.jpa.show-sql=true spring.profiles.include=jdbc logging.level.root=DEBUG
Here note that we have used the line spring.profiles.include=jdbc
. This line enables the JDBC backend for the Spring Cloud Config server. Rest of the properties are related to the Spring Security and Spring JDBC.
Now to store our application properties, we will need to create a table in our databases. Also we will need to add a couple of entries to this table:
create table PROPERTIES ( "ID" serial primary key, "CREATED_ON" timestamp , APPLICATION text, PROFILE text, LABEL text, "KEY" text, "VALUE" text ); INSERT INTO PROPERTIES ("CREATED_ON", APPLICATION, PROFILE, LABEL, "KEY", "VALUE") VALUES (NULL,'clientapp','dev','latest','prop1','value1'); INSERT INTO PROPERTIES ("CREATED_ON", APPLICATION, PROFILE, LABEL, "KEY", "VALUE") VALUES (NULL,'clientapp','dev','latest','prop2','value2');
To run the project we need to run:
Read More: Spring cloud