JDBC Backend Spring Cloud Config

JDBC Backend Spring Cloud Config thumbnail
29K
By Dhiraj 08 June, 2019

Today we will extend our previous article on Spring Cloud Config GIT backend to use JDBC Backend store to externalize our cloud config properties. We will be using MySQL (a relational database) for the JDBC backend store. For this example, the project will be built upon Spring Boot 2 with spring cloud version as Greenwich.SR1

Spring Cloud Project Setup

Head over to Spring Initiliazer to download a demo spring boot project. We require JDBC API, Config Server, MySQL driver and actuator artifacts to get started. Once, the demo project is downloaded, you can import it as a maven project in the workspace.

spring-cloud-config-jdbc-backend-project-strct

Cloud Config Database Configuration

We require to add the dependency spring-jdbc to the classpath and use the jdbc profile or by add a bean of type JdbcEnvironmentRepository to enable JDBC as a backend for configuration properties in cloud config.

The database needs to have a table called PROPERTIES with columns called APPLICATION, PROFILE, and LABEL (with the usual Environment meaning), plus KEY and VALUE for the key and value pairs in Properties style. All fields are of type String in Java, so you can make them VARCHAR of whatever length you need. Below is our sample SQL to create the required table.

The KEY column is changed to PROP_KEY to overcome the conflict between the reserved keyword KEY with MySQL DB and our PROPERTIES table.

create table PROPERTIES (id integer not null auto_increment, CREATED_ON datetime ,APPLICATION varchar(255), PROFILE varchar(255), LABEL varchar(255), PROP_KEY varchar(255), VALUE varchar(255), primary key (id)) engine=InnoDB;

Let us populate this table with a dummy record for testing.

INSERT INTO properties (CREATED_ON, APPLICATION, PROFILE, LABEL, PROP_KEY, VALUE) VALUES (NULL,'devglan','dev','latest','test-property','This is my test value');

Cloud Config Datasource Configuration

Now, let us configure our data source in the application. Spring Boot automatically configures a data source based on the dependencies on the classpath. We have below entries in the application.properties to provide our DB connection properties.

spring.profiles.active=jdbc
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.cloud.config.server.jdbc.sql= SELECT PROP_KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
spring.cloud.config.server.jdbc.order=1
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root

Testing the Cloud Config JDBC Backend Store

Now, let us test our config properties. Run PropConfigApplication.java as a java application and we have below endpoints exposed out of the box by cloud config to fetch the properties.

Running above class as a java application will expose following REST endpoints and can be accessed as http://localhost:8080/devglan/dev/latest. Here, devglan is the application name, dev is the profile name and latest is the label.

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
spring-cloud-config-jdbc-backend-api-reponse

Conclusion

This is how we implement JDBC backend spring cloud config. We can also save encrypted values such as DB password, etc. Here, is the article that discusses about Encryption and Decryption of Cloud Config Properties.

Share

If You Appreciate This, You Can Consider:

We are thankful for your never ending support.

About The Author

author-image
A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.

Further Reading on Spring Cloud