Spring Cloud Config Server with JDBC backend

Introduction

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments. As an application moves through the deployment pipeline from dev to test and into production, you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage back-end uses git, so it easily supports labelled versions of configuration environments as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations for SVN , VALUT , JDBC and plug them in with Spring configuration.

Spring Cloud Config Server

Spring Cloud Config Server offers the following benefits:

  • HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content).
  • Encrypt and decrypt property values (symmetric or asymmetric).
  • Embeddable easily in a Spring Boot application using @EnableConfigServer.

Spring Cloud Config Client

Specifically for Spring applications, Spring Cloud Config Client lets you:

  • Bind to the Config Server and initialize Spring Environment with remote property sources.
  • Encrypt and decrypt property values (symmetric or asymmetric).
  • @RefreshScope for Spring @Beans that want to be re-initialized when configuration changes.
  • Use management endpoints:
  • /env for updating Environment and rebinding @ConfigurationProperties and log levels.
  • /refresh for refreshing the @RefreshScope beans.
  • /restart for restarting the Spring context (disabled by default).
  • /pause and /resume for calling the Lifecycle methods (stop() and start() on the ApplicationContext).
  • Bootstrap application context: a parent context for the main application that can be trained to do anything (by default, it binds to the Config Server and decrypts property values).

Quick Start

· This quick start walks through using both the server and the client of Spring Cloud Config Server.

· In this example we are connecting Spring Cloud Config Server with database.

· Client Side Usage

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

· As spring actuator expose rest point for @RefreshScope with URL http://localhost:8080/actuator/refresh in Postman .

If there is any change in configuration in externalized configuration in a distributed system, by executing /refresh URL Client Side Application will load the change.

· Environment Repository

· {application}, which maps to spring.application.name on the client side.

· {profile}, which maps to spring.profiles.active on the client (comma-separated list).

· {label}, which is a server side feature labelling a “versioned” set of config files.

For example in client side bootstrap.properties, we will configure this

spring.cloud.config.uri=http://localhost:8081
spring.application.name=my_application
spring.cloud.config.label=latest
spring.cloud.config.profile= production

Here, we are configuring and giving the spring config server side location to connect and load.

Now you can create a standard Spring Boot application, such as the following HTTP server:

@RestController
@RefreshScope
public class MessageController {
@Value("${message:Hello default}")
private String message;
@RequestMapping("/message")
String message() {
return message;
}
}

Here “message” is the property which will be getting its value from Database with config server side.

· Server Side Usage

Pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>

Spring Jdbc dependency to set the environment repository.

For connection with Database

server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/springconfig
spring.datasource.username=root
spring.datasource.password=Welcome123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

For getting key and value pair from database we configure

spring.profiles.active= jdbc
spring.cloud.config.server.jdbc.sql= SELECT PROPERTIES.KY, PROPERTIES.VAL from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
spring.cloud.config.server.jdbc.order=1

Where Application, Profile and Label we have defined at Client Side.

Now you can create a standard Spring Boot application.

@SpringBootApplication
@EnableConfigServer
public class SpringConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConfigServerApplication.class, args);
}
}