Spring Boot - Cloud Configuration Server



Spring Cloud Configuration Server is a centralized application that manages all the application related configuration properties. In this chapter, you will learn in detail about how to create Spring Cloud Configuration server.

Creating Spring Cloud Configuration Server

First, download the Spring Boot project from the Spring Initializer page and choose the Spring Cloud Config Server dependency. Observe the screenshot given below −

Creating Spring Cloud Configuration Server

Now, add the Spring Cloud Config server dependency in your build configuration file as explained below −

Maven users can add the below dependency into the pom.xml file.

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Gradle users can add the below dependency in your build.gradle file.

compile('org.springframework.cloud:spring-cloud-config-server')

Now, add the @EnableConfigServer annotation in your main Spring Boot application class file. The @EnableConfigServer annotation makes your Spring Boot application act as a Configuration Server.

The main Spring Boot application class file is given below −

ConfigserverApplication.java

package com.tutorialspoint.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

Now, add the below configuration to your application.properties file. Observe the code given below −

application.properties

server.port = 8888
spring.cloud.config.server.git.uri=file:///E:/Dev/config/

Configuration Server runs on the Tomcat port 8888 and application configuration properties are loaded from a git based local file system search locations.

Create Git Repo

Initialize the Git repo

Go to E:/Dev/config/ folder and run the following git command to initialize it as git repo.

git init

Add the properties file

Now, in E:/Dev/config/, place your client application - application.properties file. For example, your client application name is config-client, then rename your application.properties file as config-client.properties and place the properties file on the path E:/Dev/config/.

The code for config-client properties file is given below −

welcome.message = Welcome to Spring cloud config server

Run the following git command to stage all changes.

E:\Dev\config> git add .

Commit the Changes

Run the following git command to commit changes.

E:\Dev\config> git commit -m "First Checkin"

Verify the changes

Run the following git command to check the commited changes.

E:\Dev\config> git log
commit 8081e552232ca5b1af29cef56e6acc6e1a5bd2e3 (HEAD -> master)
Author: maheshparashar84 <mahesh.kumar@tutorialspoint.com>
Date:   Thu Sep 12 11:38:28 2024 +0530

    First Checkin

E:\Dev\config>

The complete build configuration file is given below −

Maven users can use pom.xml given below −

pom.xml

<?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>3.3.3</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>configserver</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>configserver</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>21</java.version>
      <spring-cloud.version>2023.0.3</spring-cloud.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </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>

Gradle users can use the build.gradle file given below −

build.gradle

<scope>import</scope>
</dependency>
</dependencies>
buildscript {
   ext {
      springBootVersion = '3.3.3'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = '2023.0.3'
}
dependencies {
   compile('org.springframework.cloud:spring-cloud-config-server')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

Compilation and Execution

Now, create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands −

For Maven, use the command given below −

mvn clean install

After "BUILD SUCCESS", you can find the JAR file under the target directory.

For Gradle, use the command given below −

gradle clean build

After "BUILD SUCCESSFUL", you can find the JAR file under the build/libs directory.

Run the JAR file by using the following command −

 java jar <JARFILE> 

Now, the application has started on the Tomcat port 8888 as shown here −

 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

[32m :: Spring Boot :: [39m              [2m (v3.3.3)[0;39m

[2m2024-09-12T11:42:15.720+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mc.t.c.ConfigserverApplication           [0;39m [2m:[0;39m Starting ConfigserverApplication using Java 21.0.3 with PID 13108 (E:\Dev\configserver\target\classes started by Tutorialspoint in E:\Dev\configserver)
[2m2024-09-12T11:42:15.724+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mc.t.c.ConfigserverApplication           [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2024-09-12T11:42:16.619+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.s.cloud.context.scope.GenericScope    [0;39m [2m:[0;39m BeanFactory id=f7967b62-068c-32b4-9d6a-b8da96911a03
[2m2024-09-12T11:42:16.896+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8888 (http)
[2m2024-09-12T11:42:16.918+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2024-09-12T11:42:16.918+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine   [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.28]
[2m2024-09-12T11:42:16.978+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2024-09-12T11:42:16.978+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1197 ms
[2m2024-09-12T11:42:17.716+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8888 (http) with context path '/'
[2m2024-09-12T11:42:17.739+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[           main][0;39m [2m[0;39m[36mc.t.c.ConfigserverApplication           [0;39m [2m:[0;39m Started ConfigserverApplication in 2.497 seconds (process running for 4.133)
[2m2024-09-12T11:42:43.468+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2024-09-12T11:42:43.468+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2024-09-12T11:42:43.468+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 0 ms
[2m2024-09-12T11:42:47.289+05:30[0;39m [33m WARN[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36m.c.s.e.MultipleJGitEnvironmentRepository[0;39m [2m:[0;39m Could not merge remote for master remote: null
[2m2024-09-12T11:42:47.364+05:30[0;39m [32m INFO[0;39m [35m13108[0;39m [2m---[0;39m [2m[nio-8888-exec-1][0;39m [2m[0;39m[36mo.s.c.c.s.e.NativeEnvironmentRepository [0;39m [2m:[0;39m Adding property source: Config resource 'file [E:\Dev\config\config-client.properties]' via location 'file:/E:/Dev/config/'

Now hit the URL http://localhost:8888/config-client/default/master on your web browser and you can see your config-client application configuration properties as shown here.

Config-Client Application
Advertisements