How to Build a Git‑Backed Spring Cloud Config Server and Client

This guide walks through creating a Spring Cloud Config server backed by a Git repository, configuring it, exposing configuration via REST endpoints, and building a Spring Boot client that retrieves and binds those configurations, complete with code snippets and URL patterns.

Programmer DD
Programmer DD
Programmer DD
How to Build a Git‑Backed Spring Cloud Config Server and Client

Introduction

Spring Cloud Config provides centralized external configuration for distributed systems, offering a server (the configuration center) and clients that fetch configuration at startup. By default it stores configuration in a Git repository, enabling version control and easy management.

Prepare the Configuration Repository

Create a Git repository (e.g., on Gitee or GitHub). Add a default configuration file config-client.yml:

info:
  profile: default

and a development‑specific file config-client-dev.yml:

info:
  profile: dev

Build the Config Server

Create a Spring Boot project named config-server-git and add the Config Server dependency:

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

Add the @EnableConfigServer annotation to the main application class:

@EnableConfigServer
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}

Configure application.yml with the Git repository location and server port:

spring:
  application: config-server
cloud:
  config:
    server:
      git:
        uri: http://git.oschina.net/didispace/config-repo-demo/
server:
  port: 1201

Start the server and verify it runs without errors.

Accessing Configuration via URLs

The server exposes several URL patterns, for example:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

Requesting http://localhost:1201/config-client/dev/master returns JSON similar to:

{
  "name": "config-client",
  "profiles": ["dev"],
  "label": "master",
  "propertySources": [
    {"name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml", "source": {"info.profile": "dev"}},
    {"name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml", "source": {"info.profile": "default"}}
  ]
}

Build the Config Client

Create a Spring Boot project named config-client and add the following dependencies:

<dependencies>
  <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>

Add the main application class (same as the server example, without @EnableConfigServer ).

Create bootstrap.yml to point to the config server and specify the profile and label:

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:1201/
      profile: default
      label: master
server:
  port: 2001

Note: these properties must be placed in bootstrap.yml (or bootstrap.properties ) so they are loaded before the application starts.

Run both the server and client. Access http://localhost:2001/info on the client to see the configuration fetched from the Git repository, e.g.: {"profile": "default"} Changing the client’s profile to dev will return the dev‑specific configuration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaMicroservicesGitSpring CloudConfig Server
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.