Spring Boot Config Example
Spring Cloud Configuration Server is a centralized framework that handles all application-related configuration properties in a distributed environment.
In this tutorial, we are going to learn how to setup a centralized config server with Git version
control and how to use it in REST application.
By storing configuration under Git version control, it can be modified at application runtime.
Why to use Config Server, and what are the advantages to use Config Server.
The concept of config server arrived from the 12-factor app manifesto related to the guidelines for the development of modern cloud-native applications for best practices. It therefore clearly indicates externalizing server properties or resource files where the values of those resources change during runtime, usually with different configurations that change in each environment.
Example
If suppose request come to update some properties related to application, in that
case we usually need to build and deploy the service with that updated properties. Now, if we use Config
Server i.e go with 12-factor
app approach, we just need to update the properties in config server and refresh that client service to
use the updated entry without need to build and deploy the application.
Spring Boot Config Example
We will need to develop below two microservices using spring boot.
- Config Server Service - Provides configuration at runtime from Git.
- Config Client Service - It'll use the configuration exposed from above Config Server Service.
Spring Boot Config Server Service:
Let's start with Config Server example.Project Setup
Create Spring Boot Project for Config Server from Spring Initializer site https://start.spring.io/
Dependency
Once generated, unzip the downloaded project and import in eclipse/IntelliJ. After importing the project, you can see the generated pom.xml with spring-cloud-config-server dependency.<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-server</name>
<description>Demo project for Spring Cloud Config Server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Project Structure
Take a look at our suggested posts:
Enable config server
Add @EnableConfigServer
annotation to enable config server.
package com.techgeeknext.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class SpringConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConfigServerApplication.class, args);
}
}
Configure Git repository
- You will require a GitHub account, to create a GitHub repository. If you already have a GitHub account, skip below steps.
- Go to https://github.com/join
- Type a user name, your email address, and a password.
- Choose Sign up for GitHub, and then follow the instructions.
- Click on New to create new repository.
- Provide name to repository and click on create new repository.
-
Once git repository is created, checkout the repository into eclipse/IntelliJ.
Create the properties files: Add below properties files based on profile/environment, add prefix as techgeeknext-client-config, which is application name specified in config-spring-client service project.
- techgeeknext-client-config.properties (Default Profile)
msg = Git - Default Environment - Hello from config server.
- techgeeknext-client-config-development.properties (Development Profile)
msg = Git - Development Environment - Hello from config server.
- techgeeknext-client-config-production.properties (Production Profile)
msg = Git - Production Environment - Hello from config server.
- Once committed in git, you can see the properties files into git.
Configuration to point Git Repo
Create bootstrap.properties in the src\main\resources directory of spring-config-sever project with below configuration.
#Server port
server.port = 8888
#Git repo location
spring.cloud.config.server.git.uri=https://github.com/techgeeknext/spring-cloud-config-repo
spring.cloud.config.server.git.cloneOnStart=true
#Disable security of the Management endpoint
management.security.enabled=false
Verify Configuration
- To verify if the config server can recognize the properties, run the spring-config-server microservice.
- Now open browser or postman to see the output using below URL, it will return the JSON output.
http://localhost:8888/techgeeknext-client-config/default
- Same way you can check for development and for other environments.
Spring Boot Config Client Service Project
Now will develop spring-config-client microservice, where we will use above properties through config server.
Create the Spring Boot project for Spring Client Server as given below:
Maven dependency
Add below dependencies as given for client-server service.<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-client</name>
<description>Demo project for Spring Cloud Config Client</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Configuration properties
Create bootstrap.properties in the src\main\resources
directory and add the below properties to connect
with the config server.
#application name
spring.application.name=techgeeknext-client-config
#By default without any profile, it'll consider default i.e. local
# i.e without any profile environment name (default) -> techgeeknext-client-config.properties
#For Active Profile - development -> techgeeknext-client-config-development.properties
#For Active Profile - production -> techgeeknext-client-config-production.properties
# uncomment below property to see if you change the value like development/production etc.
##spring.profiles.active=production
#connect to config server
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false
Create REST Resource
Create the Rest Controller to view the properties from Config Server Service.
package com.techgeeknext.config.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class SpringClientRestController {
@Value("$ { msg:Config Server is not working. Verify configuration properties.}")
private String msg;
@GetMapping("/message")
public String getMessage() {
return this.msg;
}
}
Test REST Endpoint
Open browser and use the http://localhost:8080/message url. It will
return below message from config server service, which in turn get the message
from git repository.
Same way you can check for production environment message by changing to spring.profiles.active=production under spring-config-client project -> bootstrap.properties file.
Download Source Code
The full source code for this article can be found on below.Download it here -
Git Configuration Repository Properties files: Spring Boot - Git configuration repository
Config Server Code: Spring Boot - Config Server Service
Config Client Code: Spring Boot - Config Client Service