Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating a Spring Boot Configuration Server Using a Database

DZone's Guide to

Creating a Spring Boot Configuration Server Using a Database

Not many people talk about how to create a Spring Boot configuration server with a database as opposed to a file system or git repository. But here's how to do it.

· Database Zone ·
Free Resource

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

You can find many articles on the internet about creating a Spring Boot configuration server using either a file system or git repository, but this is not true for a configuration server based on a database. In this article, I am going to discuss reasons to use a database as a configuration server and demonstrate how to create it using Spring Boot.

Storing configurations on a database seems to be a better idea than storing it on either a file system or git. Because it is less trackable, it is less secure. I admit that git has security and tracking options, but they seem to be complicated for a developer. However, tracking a database table is just creating a trigger for updates, and securing a database is not an issue for a developer. Again, changing a value in a database table is just so simple and easy to commit; however, it can be a cumbersome task to make a change in a git repository.

The overall architecture is as follows:

Image title

Once you have created your Spring Boot project from the Spring initializer, you need to add the following dependencies to your pom.xml file:

...
  
<!-- CONFIG SERVER -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- /CONFIG SERVER -->
<!-- JDBC -->
<dependency>
	<groupId>com.github.ulisesbocchio</groupId>
	<artifactId>jasypt-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jdbc</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>com.zaxxer</groupId>
	<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
	<!-- Your database driver dependencies here -->
</dependency>
<!-- /JDBC -->

...

Next, you will need to create a table in your database to store the configuration:

CREATE TABLE my_properties 
  ( 
     application VARCHAR(200), 
     profile     VARCHAR(200), 
     label       VARCHAR(200), 
     KEY         VARCHAR(200), 
     value       VARCHAR(200) 
  ) 

Finally, you will need to configure your configuration server with a application.yml file as shown below:

...
spring:
  cloud:
    config:
      server:
        jdbc:
          sql: SELECT KEY, VALUE from  MY_PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
          order: 1 
  datasource:
    url: <your db url here>
    driver-class-name: <your databases driver class here>
    username: <your user to database>
    password: 
    hikari:
      maximum-pool-size: 10
      connection-timeout: 5000
  profiles:
    active:
    - jdbc
...

Now, you can test your configuration server with a configuration client.

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

Topics:
database ,spring boot ,configuration server ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}