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.

1. 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 −

package com.maixuanviet.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 properties file and replace the application.properties file into bootstrap.properties file. Observe the code given below −

server.port = 8888
spring.cloud.config.server.native.searchLocations=file:///C:/configprop/
SPRING_PROFILES_ACTIVE=native

Configuration Server runs on the Tomcat port 8888 and application configuration properties are loaded from native search locations.

Now, in file:///C:/configprop/, 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 file:///C:/configprop/.

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

welcome.message = Welcome to Spring cloud config server

The complete build configuration file is given below −

Maven users can use pom.xml given below −

<?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 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.maixuanviet</groupId>
   <artifactId>configserver</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>configserver</name>
   <description>Demo project for Spring Boot</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Edgware.RELEASE</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 −

<scope>import</scope>
</dependency>
</dependencies>
buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   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.maixuanviet'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = 'Edgware.RELEASE'
}
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}"
   }
}

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 −

Tomcat Port 8888 Output

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

Related posts:

Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Object Type Casting in Java
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Functional Interfaces in Java 8
Function trong Java 8
Debugging Reactive Streams in Java
The Modulo Operator in Java
Disable Spring Data Auto Configuration
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Generate N Number of Passwords of Length M Each
Retrieve User Information in Spring Security
Removing all duplicates from a List in Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Overflow and Underflow in Java
Send an email with an attachment
Java Program to Convert a Decimal Number to Binary Number using Stacks
Spring Security Form Login
Java Program to Perform Search in a BST
Toán tử instanceof trong java
Configure a Spring Boot Web Application
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Implement Horner Algorithm
Spring Boot - Zuul Proxy Server and Routing
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
HashSet trong Java hoạt động như thế nào?
Java Program to Implement Variable length array
Java 8 Stream API Analogies in Kotlin
A Quick Guide to Spring Cloud Consul
Hướng dẫn Java Design Pattern – Decorator
Getting the Size of an Iterable in Java