Sample of Spring Cloud Config Server embedded application that uses database as backend for configuration properties.
Spring Cloud Config Server provides several options to store configuration for an application. In general it is handled by Environment Repository.
Available options are git, file system, vault, svn, and database. This application demonstrates usage of JdbcEnvironmentRepository which allows an application to store its configuration in database.
In order to enable this feature we will include spring-boot-starter-jdbc
as one of the dependencies for the application and
include jdbc
as one of its active profiles.
This can be seen in pom.xml.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
This can be seen in bootstrap.yml.
spring:
profiles:
active: jdbc
By default the JdbcEnvironmentRepository will look into a table called PROPERTIES
which contains the following columns:
- KEY
- VALUE
- APPLICATION
- PROFILE
- LABEL
Schema to create the table can found in schema-jdbc.sql:
CREATE TABLE IF NOT EXISTS PROPERTIES (
KEY VARCHAR(2048),
VALUE VARCHAR(4096),
APPLICATION VARCHAR(128),
PROFILE VARCHAR(128),
LABEL VARCHAR(128),
PRIMARY KEY (`KEY`, `APPLICATION`, `PROFILE`, `LABEL`)
);
In the script above KEY, APPLICATION, PROFILE, and LABEL are marked as composite key in order to avoid duplicated entry.
For this demonstration will we have a configuration called app.greet.name
and this will be populated upon start-up.
Its script can be found in jdbc-data.sql.
INSERT INTO PROPERTIES (APPLICATION, PROFILE, LABEL, KEY, VALUE)
VALUES ('demo', 'default', 'master', 'app.greet.name', 'Demo');
The script above explains that the configuration app.greet.name
belongs to:
- an application called demo
- with profile called default
- and labelled master
In order for JdbcEnvironmentRepository to retrieve properties for this application it will need to be informed on its name, profile, and label. This configurations can be found in bootstrap.yml
spring:
application:
name: demo
cloud:
config:
label: master
We are not configuring spring.cloud.profile
because its default value is default
.
Finally we will need to inform Spring Cloud on what are the classes needed in order to build the bootstrap application context. This can found in spring.factories:
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
These two classes will help us to build JdbcTemplate which is needed to construct JdbcEnvironmentRepository.
In order to ensure that the application will use configurations from database we will create same configuration in application.yml:
app:
greet:
name: Default
We will have GreetService which will retrieve the value of app.greet.name
from GreetProperties.
@AllArgsConstructor
@Service
public class GreetService {
private GreetProperties properties;
public String greet(String greet) {
return String.format("%s, my name is %s", greet, properties.getName());
}
}
Next we will have ApplicationTests class that verifies that the value for app.greet.name
is Demo and not Default:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
private GreetService service;
@Test
public void greetAndResponseContainsNameFromDatabase() {
String response = service.greet("Hello");
assertThat(response)
.doesNotContain("Default")
.contains("Demo");
}
}
By executing greetAndResponseContainsNameFromDatabase()
we verify that the returned response:
- Does not contain the word Default
- Contains the word Demo