Spring Cloud Centralized Configuration By Using Database

Motivation

Maintenance of the microservice configurations are getting harder when we have a lot microservices. By default, configurations divided into files and named by profile and microservice names like:

user-service-dev.yml

store-service-test.yml

If we need to do some configuration changes, we need to update these files and re deploy them again and again. For that reason, we may need to use database to store microservice configurations. In this article, you can learn how to do it also, you can check example code on the Github.

Setup

Let’s start quick and easy. Go to the Spring Initializr Website and add dependecies such as H2 Database for demo database system , Spring Data JPA for quick Database Setup and Config Server for serving configurations. Naturally you don’t need to H2 Database and Spring Data JPA, you can use a RDMS system such as Oracle, MySQL and create tables manually, but in this tutorial we will use them for building our example application.

Spring Initializr

After that, click generate button and download project files.

Extract RAR file and import project to your favourite Java IDE. Add the following lines to the application.properties file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring.profiles.active=jdbc

spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.cloud.config.server.default-profile=test
spring.cloud.config.server.default-label=latest
spring.cloud.config.server.jdbc.sql=SELECT `key`, `value` FROM `properties` WHERE `application`=? AND `profile`=? AND `label`=?;
spring.cloud.config.server.jdbc.order=0
  • We need to set active profile to jdbc because we need to tell spring to use jdbc based configuration server.

  • Spring Datasource properties setted based H2. You can use other RDMS systems.

  • We will use H2 console to send SQL commands. So, H2 console setted to true and console path setted to /h2-console

  • Spring Cloud Config default profile and default label setted. If no profile or label available, it will use default ones. Also, SQL Select command setted to fetch key and value from database. If you don’t want to use label value, you can exclude it from the where clause.

After that, create a package named model and create Property class on it.

We will map Property class to the PROPERTIES table. For this purpose, add the neccessary annotations to map values to the table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

import javax.persistence.*;

@Entity
@Table(name = "PROPERTIES")
public class Property {

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;

@Column(name = "APPLICATION")
String applicationName;

@Column(name = "PROFILE")
String profileName;

@Column(name = "LABEL")
String label;

@Column(name = "KEY")
String key;

@Column(name = "VALUE")
String value;

Add the EnableConfigServer annotation to the top of the main class.

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableConfigServer
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

That’s all. Run the application. It will be automatically running on the Port 8080. Properties table will be automatically created by hibernate.

Connect the H2 Console on the path http://localhost:8080/h2-console You don’t need to enter anything to the login panel if you don’t change anything to the properties file. Click the Connect button.

Run the following sql command to insert a new key value to the profile test and label latest

1
INSERT INTO PROPERTIES(APPLICATION,PROFILE,LABEL,KEY,VALUE) VALUES ('store-service','test','latest','server.location','spain')

H2 Console

Now, test the Config server.

Postman

Feel free to contact and ask any questions. Happy coding!

Github Link: https://github.com/kaanece17/sping-cloud-jdbc-config

References:

https://cloud.spring.io/spring-cloud-config/reference/html/

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×