Environments with Cloud Config Server

Below is the complete, clean, and correct way to set up dev, qa, and prod environments using Spring Cloud Config Server.

This is the exact pattern used in real microservice deployments.


โœ… 1. Repo Structure in Git (MOST IMPORTANT)

Your config-repo should look like:

config-repo/
โ”‚
โ”œโ”€โ”€ auth-service.yml                # common default config
โ”œโ”€โ”€ auth-service-dev.yml            # dev environment
โ”œโ”€โ”€ auth-service-qa.yml             # qa environment
โ””โ”€โ”€ auth-service-prod.yml           # prod environment

You can do this for other microservices too:

config-repo/
โ”œโ”€โ”€ gateway-service.yml
โ”œโ”€โ”€ gateway-service-dev.yml
โ”œโ”€โ”€ gateway-service-qa.yml
โ””โ”€โ”€ gateway-service-prod.yml

โœ” suffix = profile
โœ” Spring loads config like:

FileProfile
auth-service.ymldefault
auth-service-dev.ymldev
auth-service-qa.ymlqa
auth-service-prod.ymlprod

โœ… 2. Config Server Setup

application.yml (Config Server)

server:
  port: 8888

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-org/config-repo
          default-label: main
          clone-on-start: true

โœ” No special setup needed for dev/qa/prod
โœ” Automatically loads files by profile suffix


โœ… 3. Client (Auth Service) Setup

Every microservice must have:

application.yml

spring:
  application:
    name: auth-service

  config:
    import: "optional:configserver:http://localhost:8888"

application-dev.yml

spring:
  profiles:
    active: dev

application-qa.yml

spring:
  profiles:
    active: qa

application-prod.yml

spring:
  profiles:
    active: prod

๐Ÿ‘‰ You enable a profile by using:

java -jar auth-service.jar --spring.profiles.active=qa

or in IntelliJ run config:

--spring.profiles.active=dev

โœ… 4. How Config Server Resolves Config

When you start:

java -jar auth-service.jar --spring.profiles.active=dev

Config Server loads:

  1. auth-service.yml (default)
  2. auth-service-dev.yml (profile-specific)

Both are merged โ†’ dev overrides defaults


๐Ÿงช 5. Test using URLs

Default

http://localhost:8888/auth-service/default

Dev

http://localhost:8888/auth-service/dev

QA

http://localhost:8888/auth-service/qa

Prod

http://localhost:8888/auth-service/prod

โญ Example Files

auth-service.yml (Default)

server:
  port: 9000

auth:
  common-message: "Auth Service Default"

auth-service-dev.yml

server:
  port: 9001
auth:
  db-url: jdbc:postgresql://dev-db/auth

auth-service-qa.yml

server:
  port: 9002
auth:
  db-url: jdbc:postgresql://qa-db/auth

auth-service-prod.yml

server:
  port: 9003
auth:
  db-url: jdbc:postgresql://prod-db/auth

๐ŸŽฏ Best Practice (Used in Production)

EnvironmentHow to Run
Dev--spring.profiles.active=dev
QA--spring.profiles.active=qa
Prod--spring.profiles.active=prod

Each profile pulls its own config file from the Config Server.

Share with