Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 70

    Config Server

    Spring Cloud Config Server centralises externalised configuration for many Boot apps in a Git-backed (or native) repository.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Spring Cloud Config Server centralises externalised configuration for many Boot apps in a Git-backed (or native) repository. Services import config at startup by application name and profile — change a property, refresh or restart, without rebuilding JARs.

    On Boot 3, clients typically use spring.config.import=configserver:.... Keep secrets out of Git: reference a vault/secret manager, or encrypt with care — never commit real passwords to the config repo.

    Modern Kubernetes estates often prefer ConfigMaps/Secrets plus Spring Cloud Kubernetes or external secret operators. Config Server remains widely used and interview-relevant; know both.

    This lesson wires TechLearningPRO Orders to central config honestly alongside cloud-native alternatives.

    Purpose of this lesson

    Run a Git-backed Config Server and import shared Orders configuration safely without embedding secrets.

    Understanding the topic

    One config repo; many services; profile-specific overlays.

    • Server: @EnableConfigServer serving Git (or native) files.
    • Client: spring.application.name + spring.config.import.
    • Files: orders-service.yml, orders-service-prod.yml, application.yml.
    • Secrets: externalise — vault/K8s Secret — not plaintext in Git.
    • Alt: ConfigMaps + Secrets + Spring Cloud Kubernetes reload.

    Step-by-step explanation

    1. Create a config repo with orders-service.yml shared properties.
    2. Boot a Config Server pointed at that Git URI (local file:// is fine for lab).
    3. In Orders, set application name and spring.config.import=optional:configserver:http://config:8888.
    4. Change a non-secret property and demonstrate restart/refresh picking it up.

    Informative example

    Config Server + Orders client import.

    yaml
    // config-server
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigApp {
    public static void main(String[] args) {
    SpringApplication.run(ConfigApp.class, args);
    }
    }
    # config-server application.yml
    server.port: 8888
    spring:
    cloud:
    config:
    server:
    git:
    uri: https://github.com/acme/config-repo
    default-label: main
    # orders-service bootstrap/application
    spring:
    application:
    name: orders-service
    config:
    import: optional:configserver:http://config:8888
    # config-repo/orders-service.yml
    orders:
    checkout:
    max-lines: 50
    # passwords stay in Secret Manager / K8s Secret, e.g. ${DB_PASS}

    Real-world use

    TechLearningPRO once rebuilt Orders images to flip a feature flag. Config Server (and later ConfigMaps) moved that flag to data. A bad commit once put a database password in the Git config repo — rotation and secret-manager migration followed; the lesson stuck harder than any slide.

    Security implications

    Central config is high blast radius.

    • Protect the config repo and server with auth and network policy.
    • Never commit real credentials — use ${DB_PASS} placeholders.
    • Audit who can change prod overlays.

    Best practices

    • Git-back non-secret config for auditability; secrets elsewhere.
    • Use profiles (dev/prod) as overlays, not copy-paste sprawl.
    • On Kubernetes, evaluate ConfigMaps/Secrets before adding Config Server by habit.

    Common mistakes

    • Storing production passwords in the config Git repo.
    • Optional import silently skipping config and running on unsafe defaults.
    • Assuming @RefreshScope fixes every bean without understanding refresh semantics.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat does Spring Cloud Config Server provide?+

    Answer

    Centralised external configuration, commonly Git-backed, served to many Spring clients by name/profile.
    2IntermediateQuestionHow do Boot 3 clients import config?+

    Answer

    Typically spring.config.import=configserver:... (optional: prefix for soft dependency).
    3IntermediateQuestionWhat is a cloud-native alternative?+

    Answer

    Kubernetes ConfigMaps/Secrets (often with Spring Cloud Kubernetes) or a dedicated secret manager.

    Hands-on exercise

    Lab. Stand up Config Server with a local Git repo. Import orders.checkout.max-lines into Orders. Move a pretend DB password to an env var and prove the app fails fast if it is missing.

    Summary

    Config Server (or K8s config) keeps Orders settings out of rebuilds. Next: protect remote calls with the circuit breaker pattern.

    Ready to mark this lesson complete?Track your journey across the entire course.