How to Build a Spring Cloud Config Server with JDBC Storage
This tutorial walks through creating a Spring Cloud Config Server that stores configuration data in a MySQL database, covering project setup, Maven dependencies, database schema creation with Flyway, Java bootstrap code, application properties configuration, and verification using curl commands to retrieve configuration values.
Build Config Server Service
Step 1: Create a basic Spring Boot project and add the following Maven dependencies in pom.xml: spring-cloud-config-server – core Config Server dependency spring-boot-starter-jdbc – JDBC support for database access mysql-connector-java – MySQL driver flyway-core – optional, used to manage the database schema
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>Create Database Schema
Step 2: Under src/main/resources create a schema folder and add V1__Base_version.sql containing the table definition used by the Config Server.
CREATE TABLE `properties` (
`id` int(11) NOT NULL,
`key` varchar(50) NOT NULL,
`value` varchar(500) NOT NULL,
`application` varchar(50) NOT NULL,
`profile` varchar(50) NOT NULL,
`label` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;This script will be executed automatically by Flyway when the application starts.
Application Bootstrap Class
Step 3: Create the main class ConfigServerBootstrap and annotate it with @EnableConfigServer and @SpringBootApplication. The class also inserts some test data into the properties table using JdbcTemplate.
@EnableConfigServer
@SpringBootApplication
public class ConfigServerBootstrap {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(ConfigServerBootstrap.class, args);
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
jdbcTemplate.execute("delete from properties");
jdbcTemplate.execute("INSERT INTO properties VALUES(1, 'com.didispace.message', 'test-stage-master', 'config-client', 'stage', 'master')");
jdbcTemplate.execute("INSERT INTO properties VALUES(2, 'com.didispace.message', 'test-online-master', 'config-client', 'online', 'master')");
jdbcTemplate.execute("INSERT INTO properties VALUES(3, 'com.didispace.message', 'test-online-develop', 'config-client', 'online', 'develop')");
jdbcTemplate.execute("INSERT INTO properties VALUES(4, 'com.didispace.message', 'hello-online-master', 'hello-service', 'online', 'master')");
jdbcTemplate.execute("INSERT INTO properties VALUES(5, 'com.didispace.message', 'hello-online-develop', 'hello-service', 'online', 'develop')");
}
}Configuration Properties
Step 4: Add the following entries to application.properties to switch the Config Server to JDBC mode and configure the data source.
spring.application.name=config-server-db
server.port=10020
spring.profiles.active=jdbc
spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` FROM PROPERTIES WHERE APPLICATION=? AND PROFILE=? AND LABEL=?
spring.datasource.url=jdbc:mysql://localhost:3306/config-server-db
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
flyway.locations=/schemaKey points: spring.profiles.active=jdbc enables the JDBC backend.
The custom SQL statement rewrites the default query because key and value are reserved words in MySQL.
Datasource settings must match the actual MySQL instance. flyway.locations tells Flyway where to find the schema script.
Verification
Start the Config Server and use curl to fetch configuration data.
Example 1 – Retrieve configuration for config-client in the stage profile:
curl http://localhost:10020/config-client/stage/ {
"name":"config-client",
"profiles":["stage"],
"propertySources":[{
"name":"config-client-stage",
"source":{
"com.didispace.message":"test-stage-master"
}
}]
}Example 2 – Retrieve configuration for hello-service with profile online and label develop:
curl http://localhost:10020/hello-service/online/develop {
"name":"hello-service",
"profiles":["online"],
"label":"develop",
"propertySources":[{
"name":"hello-service-online",
"source":{
"com.didispace.message":"hello-online-develop"
}
}]
}Conclusion
The guide demonstrates how to use the JDBC storage option introduced in Spring Cloud Config Edgware, showing the full setup from Maven dependencies to schema creation, server configuration, and runtime verification. Further improvements can include indexing the properties table, optimizing the custom SQL, and tailoring the schema to specific needs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
