카테고리 없음

[Spring Cloud Config] 1-2. Spring Cloud Config Server: Environment Repository

noahkim_ 2025. 8. 21. 00:53

0. Environment Repository

  • Config Server가 어디서 설정 데이터를 가져올지 정의하는 전략
  • Config Server는 설정을 읽어서 Environment 객체로 변환해 클라이언트에게 내려줌
  •  client는 config server에 "http://{config-server}/{application}/{profile}/{label}" 형식으로 요청함
  • ✅ config server는 "서비스 이름 + 환경 + 레이블" 조합으로 설정파일을 식별함

 

주요 변수

변수 매핑 대상 의미 예시
{application} 클라이언트 spring.application.name 어떤 서비스인지 식별하는 이름
user-service
order-service
{profile} 클라이언트 spring.profiles.active 어떤 환경인지 구분하는 이름
(여러 개 지정 가능, 마지막이 우선순위 가장 높음)
dev
test
prod
{label} 서버(Git) 브랜치/태그 특정 버전의 설정 파일을 가져오기 위해 사용
main
release-1.0

 

예시) 요청

더보기
http://localhost:8888/user-service/dev/main
  • Config Server는 main 브랜치에서 user-service-dev.yml 파일을 찾아서 반환

 

1. Git Backend

  • 기본적으로 사용하는 환경 저장소
  • 로컬/원격 저장소 모두 사용 가능
  • ✅ 운영 환경에서는 "ssh:" 방식으로 원격 저장소를 클론하는게 안정적
  • 브랜치or태그에 "/"가 있을 경우, "({special-string})"으로 치환해야 함

 

Pattern Matching and Multiple Repositories

  • 여러 Git 저장소를 패턴 매칭으로 연결하는 방법
  • ✅ 서비스별/팀별로 다른 저장소를 쓸 수 있음
  • application + profile 조합으로, 어떤 저장소를 쓸지 패턴 매칭해서 라우팅할 수 있음

 

설정) repos

더보기
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo   # 기본 저장소
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

 

  • 기본 저장소: https://github.com/spring-cloud-samples/config-repo
  • simple: 패턴은 simple/* → application이 simple일 때 모든 profile에 적용
  • special: 패턴은 special*/dev*, *special*/dev* → application이 special로 시작하고 profile이 dev인 경우
  • local: 패턴은 local* → application 이름이 local로 시작하는 경우

 

설정) pattern

더보기
repos:
  development:
    pattern:
      - '*/development'
      - '*/staging'
    uri: https://github.com/development/config-repo
  • application 이름은 아무거나(*) 가능
  • profile이 development나 staging이면 development/config-repo 사용

 

설정) search-paths

더보기
search-paths:
  - foo
  - bar*
  • 저장소의 루트 뿐 아니라 foo/ 디렉토리, bar로 시작하는 하위 디렉토리에서도 설정 파일을 찾음.
  • 예: foo/application-dev.yml, bar1/user-service-prod.yml 같은 것도 매칭됨.

 

설정) cloneOnStart

더보기
repos:
  team-a:
    pattern: team-a-*
    cloneOnStart: true
    uri: https://git/team-a/config-repo.git
  team-b:
    pattern: team-b-*
    cloneOnStart: false
    uri: https://git/team-b/config-repo.git
  • team-a → 서버 시작할 때 바로 clone (잘못된 URI나 권한 문제 있으면 서버 부팅 단계에서 바로 에러 확인)
  • team-b → 요청이 들어올 때 처음 clone (문제가 있어도 서버는 뜨지만, 요청 시점에 에러 발생)

 


출처