Skip to content

keeplearningandtrying/jdbc-env-repo-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Cloud: JDBCEnvironmentRepository Sample Application

Sample of Spring Cloud Config Server embedded application that uses database as backend for configuration properties.

Background

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.

Configuration

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.

Include JDBC as dependency

This can be seen in pom.xml.

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

Include jdbc as active profile

This can be seen in bootstrap.yml.

spring:
  profiles:
    active: jdbc

Creating table and populating data

By default the JdbcEnvironmentRepository will look into a table called PROPERTIES which contains the following columns:

  • KEY
  • VALUE
  • APPLICATION
  • PROFILE
  • LABEL

Create table schema

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.

Populate table

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

Configure Application Properties

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.

Create Bootstrap Application Context

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.

Verify Configuration Properties

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

About

Sample implementation to retrieve application configurations from database with Spring Cloud.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published