Spring Boot Config Server example using Git
This tutorial shows how to configure and launch a Config Server using Spring cloud & Git.
Spring Cloud Config provides server as well as client side support for externalized configuration
in a distributed system. Using Config Server, you can keep application configuration related properties across all environments at one place.
The Spring Cloud Configuration server is a REST-based application which is built on top of Spring Boot. It exposes
APIs which return property in form of JSON
, YAML
, properties format.
Spring Cloud Configuration server can be configured with Git/Database/File
System as backend which will
store the application configuration. In this example we will use Git
as backend system.
Step 1) Download Git
Now create a directory named config-server and application-local.properties, application-dev.properties files using below commands.
C:\ cd %HOMEPATH% C:\ mkdir config-repo C:\ cd config-repo C:\ git init . C:\ echo env = dev > config-server-dev.properties C:\ echo message = this is dev configuration >> config-server-dev.properties C:\ echo env = local > config-server-local.properties C:\ echo message = this is local configuration >> config-server-local.properties C:\ git add -A . C:\ git commit -m "Added properties files"
Step 2) Create pom.xml for config server project and add below dependencies
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.1.6.RELEASE</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>central-repo</id> <name>Central Repository</name> <url>https://repo1.maven.org/maven2</url> </repository> </repositories>
Step 2) Create ConfigServer class that will launch config server
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServer { public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); } }
Step 3) Create bootstrap.yml file
You need bootstrap.yml file to load the config server configuration. Choosing port as 8888 is better as by default on client side this port is used by spring.cloud.config.uri.
#Server port server: port: 8888 #Git repo location spring: application: name: config-server cloud: config: server: git: uri: C:\\Users\\bisht\\Git\\config-repo
Step 4) Launch ConfigServer application
Console Output :
2020-09-04 13:25:55.390 INFO 7768 --- [ main] com.example.demo.ConfigServer : Started ConfigServer in 10.234 seconds (JVM running for 11.025)
Step 5) Validate Config Server
Open any browser and launch http://localhost:8888/config-server/dev. You will see below details displayed in the broswer.
References :
Spring Cloud Config