Spring Cloud: JdbcEnvironmentRepository
Sample of Spring Cloud Config Server using a relational database (JdbcEnvironmentRepository) as its backend and a Config Client loading properties via Spring Boot’s Config Data API (spring.config.import)—without legacy bootstrap.yml or spring-cloud-starter-bootstrap.
Background
Spring Cloud Config Server supports multiple EnvironmentRepository backends, including Git, file system, Vault, Redis, and relational databases. This tutorial demonstrates JdbcEnvironmentRepository backed by MySQL.
Modern Spring Cloud replaces the legacy bootstrap context (bootstrap.yml and spring-cloud-starter-bootstrap) with Spring Boot’s Config Data API:
-
A standalone Config Server configures its
JdbcEnvironmentRepositorydirectly inapplication.yml, withoutbootstrap.ymlor customMETA-INF/spring.factoriesbootstrap configuration. -
A Config Client imports its configuration in
application.ymlusingspring.config.import: configserver:…and reloads updated values through/actuator/refresh.
Modular Layout
The module separates the Config Server entry point from the greeting client domain slice:
-
zin.rashidi.boot.cloud.jdbcenvrepo: CloudJdbcEnvRepoApplication starts the standalone Config Server (@EnableConfigServer). -
zin.rashidi.boot.cloud.jdbcenvrepo.greet: package-private domain slice containing GreetConfiguration, GreetProperties, and GreetResource used by the Config Client.
Dependencies
Include spring-cloud-config-server, spring-boot-starter-data-jdbc, and spring-boot-starter-actuator in build.gradle.kts (spring-cloud-starter-bootstrap is not needed):
implementation("org.springframework.boot:spring-boot-restclient")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.cloud:spring-cloud-config-server")
runtimeOnly("com.mysql:mysql-connector-j")
Creating Table and Populating Data
By default, JdbcEnvironmentRepository queries a table named PROPERTIES with the following columns:
-
KEY -
VALUE -
APPLICATION -
PROFILE -
LABEL
Create Table Schema
The table definition is in init-script.sql:
CREATE TABLE `PROPERTIES`
(
`KEY` VARCHAR(128),
`VALUE` VARCHAR(128),
`APPLICATION` VARCHAR(128),
`PROFILE` VARCHAR(128),
`LABEL` VARCHAR(128),
PRIMARY KEY (`KEY`, `APPLICATION`, `LABEL`)
);
KEY, APPLICATION, and LABEL form a composite primary key to prevent duplicate entries.
Populate Table
For this tutorial, we seed a property named app.greet.name in init-script.sql:
INSERT INTO PROPERTIES (`APPLICATION`, `PROFILE`, `LABEL`, `KEY`, `VALUE`)
VALUES ('demo', 'default', 'main', 'app.greet.name', 'Demo');
This row defines app.greet.name=Demo for:
-
application
demo -
profile
default -
label
main
Configure the Standalone Config Server
Activate the jdbc profile and configure the MySQL-compatible queries directly in application.yml:
spring:
application:
name: config-server
profiles:
active: jdbc
cloud:
config:
server:
accept-empty: false
jdbc:
default-label: main
sql: SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
sql-without-profile: SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE='default' and LABEL=?
By default, JdbcEnvironmentProperties uses unquoted KEY and VALUE column names, so backticks are required for MySQL.
-
default-label: maintells the JDBC repository to query themainlabel when no label is supplied in a request. -
accept-empty: falsecauses the Config Server to respond with HTTP404 Not Foundwhen no properties exist for a requested application.
CloudJdbcEnvRepoApplication enables the Config Server HTTP endpoints without scanning the client’s greet package:
@SpringBootConfiguration
@EnableAutoConfiguration
@EnableConfigServer
public class CloudJdbcEnvRepoApplication {
public static void main(String[] args) {
SpringApplication.run(CloudJdbcEnvRepoApplication.class, args);
}
}
With @EnableConfigServer, GET /demo/default returns the properties stored for application demo under the main label, and GET /unknown/default returns HTTP 404.
Consume Configuration via spring.config.import
Instead of bootstrap.yml, a Spring Cloud Config Client declares spring.config.import in its configuration file. In this module, greet-client.yml configures the demo client:
spring:
application:
name: demo
config:
import: configserver:http://localhost:${config.server.port}
app:
greet:
name: Default
management:
endpoints:
web:
exposure:
include: refresh
The local fallback value app.greet.name: Default is overridden by the Demo value fetched from the JDBC-backed Config Server.
GreetResource reads app.greet.name through GreetProperties:
@RestController
@RefreshScope
class GreetResource {
private final GreetProperties properties;
GreetResource(GreetProperties properties) {
this.properties = properties;
}
@GetMapping(value = "/greet", produces = MediaType.TEXT_PLAIN_VALUE)
public String greet(@RequestParam String greeting) {
return "%s, my name is %s".formatted(greeting, properties.getName());
}
}
In a standalone Config Client application that depends on spring-cloud-starter-config, spring.cloud.config.enabled defaults to true. Because this tutorial’s integration test runs both the Config Server and Config Client on the same classpath (spring-cloud-config-server disables the client by default so a server does not call itself), the test explicitly passes --spring.cloud.config.enabled=true and excludes DataSourceAutoConfiguration when starting the client context.
|
Refresh Configuration
The client exposes only the refresh actuator endpoint:
management:
endpoints:
web:
exposure:
include: refresh
After updating the app.greet.name row for demo, default, and main in PROPERTIES, send POST /actuator/refresh to the Config Client. Spring Cloud re-runs the Config Data import against the Config Server over HTTP, rebinds GreetProperties, and recreates the @RefreshScope GreetResource. A subsequent GET /greet?greeting=Hello returns the updated name without restarting the application. Protect the refresh and Config Server endpoints appropriately in production.
Run the Tests
The integration tests use Testcontainers (@Testcontainers and @ServiceConnection) with MySQL and require Docker. They verify Config Server responses for existing and missing applications as well as a Config Client importing and dynamically refreshing database-backed properties via spring.config.import.
./gradlew :core:cloud-jdbc-env-repo:check