Config Server with Git

Create the Server

New Spring Boot app config-server with the dependency Config Server (spring-cloud-config-server):

package com.shopease.config;

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);
    }
}
server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/shopease/config-repo.git
          default-label: main          # branch
          search-paths: '{application}' # optional: one folder per service
          clone-on-start: true          # fail at startup if the repo is wrong
          username: shopease-bot
          password: ${GIT_TOKEN}        # never commit the token

Tip: For local practice use a folder: uri: file:///C:/work/config-repo (it must be a Git repo, run git init and commit). Or use the native profile with spring.cloud.config.server.native.search-locations.

Files in the Config Repo

# application.yml  (shared by ALL services)
management:
  endpoints:
    web:
      exposure:
        include: health,info,refresh
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

# order-service.yml  (all profiles of order-service)
shopease:
  orders:
    max-items-per-order: 10
    cod-enabled: true

# order-service-prod.yml  (only the prod profile)
shopease:
  orders:
    max-items-per-order: 5
spring:
  datasource:
    url: jdbc:postgresql://orders-db.internal:5432/orders

The Server Endpoints

Example

URL pattern: /{application}/{profile}[/{label}]. Most specific file comes first:

curl -s localhost:8888/order-service/prod

Example output

{
  "name": "order-service",
  "profiles": ["prod"],
  "label": null,
  "version": "4c1e9a7d2b0f6e3a8c5d1b9e7f2a6c4d8e0b3f1a",
  "state": null,
  "propertySources": [
    { "name": "https://github.com/shopease/config-repo.git/order-service-prod.yml",
      "source": { "shopease.orders.max-items-per-order": 5,
                  "spring.datasource.url": "jdbc:postgresql://orders-db.internal:5432/orders" } },
    { "name": "https://github.com/shopease/config-repo.git/order-service.yml",
      "source": { "shopease.orders.max-items-per-order": 10, "shopease.orders.cod-enabled": true } },
    { "name": "https://github.com/shopease/config-repo.git/application.yml",
      "source": { "management.endpoints.web.exposure.include": "health,info,refresh",
                  "eureka.client.service-url.defaultZone": "http://localhost:8761/eureka/" } }
  ]
}

Example

Or as one merged file:

curl -s localhost:8888/order-service-prod.yml

Example output

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:
        include: health,info,refresh
shopease:
  orders:
    cod-enabled: true
    max-items-per-order: 5
spring:
  datasource:
    url: jdbc:postgresql://orders-db.internal:5432/orders