본문 바로가기

spring/cloud

[spring cloud]#1 spring cloud config server에서 application 설정 정보 조회하기

spring cloud config server 이점

  • 분산 시스템에서 서버, 클라이언트 구성에 필요한 설정 정보를 외부 시스템에서 관리하기 위해서 사용하는 기술
  • 하나의 중앙화 된 저장소에서 구성요소 관리 가능
  • 각 서비스를 다시 빌드하지 않고 설정 정보를 동적으로 변경이 가능

 

구현 절차

1. config server 만들기

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.1.2'
	id 'io.spring.dependency-management' version '1.1.2'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '17'
}

repositories {
	mavenCentral()
}

ext {
	set('springCloudVersion', "2022.0.4")
}

dependencies {
	implementation 'org.springframework.cloud:spring-cloud-config-server' # config server 라이브러리 추가
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

tasks.named('test') {
	useJUnitPlatform()
}
server:
  port: 8888

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: file:///C:\\samplePath\\git-local-repo # 설정 파일 저장소 uri
@SpringBootApplication
@EnableConfigServer # 추가
public class ConfigApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigApplication.class, args);
	}
}

 

2. 설정 파일 생성하기

3. config server 가동 이후 설정 정보 조회하기

 

리소스 url 규칙

http://mydomain.com/{application}/{profile}[/{label}]
http://mydomain.com/{application}-{profile}.yml
http://mydomain.com/{label}/{application}-{profile}.yml
http://mydomain.com/{application}-{profile}.properties
http://mydomain.com/{label}/{application}-{profile}.properties

 

예를 들어서 ecommerce.yaml, ecommerce-dev.yaml 파일을 만들었다면 ecommerce 는 application 이고 dev는 profile이다. 또한 깃의 브랜치 이름이 label과 대응하는 관계이다.

 

ecommerce.yaml 조회 -> http://domain.com/ecommerce/default

ecommerce-dev.yaml 조회 -> http://domain.com/ecommerce/dev   

 

참고

 

Spring Cloud Config

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments. The concepts o

docs.spring.io