Spring Config Server은 중앙 집중식 구성 서버로, 분산 애프리케이션에서 구성을 외부화하고 중앙에서 쉽게 관리할 수 있도록 합니다.
이를 통해 애플리케이션 인스턴스가 증가하더라도 일관된 구성을 유지하고 쉽게 변경할 수 있습니다.
또한, application.yml
등의 환경 정보가 변경될때 즉각적으로 서버에 반영될 수 있도록 하는 등의 기능을 별도로 제공합니다.
spring boot 3.3.1
jdk 17
Project 생성 (git 활용)
spring config server 를 생성하기 앞서 실습을 위한 폴더를 생성
해당하는 폴더에서 powershell → git init
명령을 이용하여 git 으로 관리
Web Git 환경에서 repository 를 생성하고 clone 한 뒤 작업하는 경우 로컬과 원격 사이에 working tree 가 일치하지 않아 문제가 생길 수 있으므로 local 에서 작업한 뒤 remote 와 연결 할 것
폴더에 환경 변수를 관리 하기 위한 yml
파일 생성 (ex: ecommerce.yml
)
token:
expiration_time: 86500000
secret: token-secret
gateway:
ip: 192.168.0.15
커밋하기 (푸쉬는 X) → 커밋하지 않을 경우 Config Server 가 브랜치를 찾지 못함
Spring Project 생성
의존성 주입
implementation 'org.springframework.cloud:spring-cloud-config-server'
@EnableConfigServer
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
@EnableConfigServer
어노테이션을 통해 Config Server 로 사용될 것임을 알려준다.application.yml
설정
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: file:///D:/git/spring-cloud-microservice
file://D:/git/spring-cloud-microservice
file:///D:/git/spring-cloud-microservice
Config Server 로 부터 환경 변수를 불러올 서버들의 설정
dependency 추가
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config
implementation 'org.springframework.cloud:spring-cloud-starter-config'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
bootstrap.yml
은 application.yml
보다 먼저 로드 되며 외부 설정 서버와의 연결등을 포함bootstrap.yml
을 application.yml
과 동일한 위치에 생성
spring:
cloud:
config:
name: ecommerce
uri: http://127.0.0.1:8888
# profiles:
# active: dev # Configuration 정보의 Active profiles 에서 설정 가능
yml
파일의 이름을 지정yml
의 Profile을 지정 (ex: dev 설정시 ecommerce-dev.yml
)Spring Config Server 를 보다 원활하게 관리하고 다른 사람들과 동일한 정보를 유지하기 위해서 Git 에 연동할 수 있습니다.
local git 폴더를 git repository와 연동하기
git 에서 현재 local git 폴더를 push 하기 위한 repository 를 생성
local git 폴더에서 console 창 열기
remote 와 local git 연동
git remote add origin {{git-repository-url}}
git push --set-upstream origin master # 초기 설정시 필요
Config Server application.yml
설정
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
default-label: master
uri: https://github.com/duckbill413/spring-cloud-microservice.git
# username: [username] # private repository 인 경우
# password: [password] # private repository 인 경우
Git 과 연동을 완료하였으나 로컬 환경에서 개발 및 테스트를 위해 로컬 환경의 환경 변수를 활용하고 싶을 수 있다. 이때는 native
옵션을 활용하면 된다.
server:
port: 8888
spring:
application:
name: config-server
profiles:
active: native # 로컬 폴더의 환경 파일을 읽기 위한 profile 설정 (default: )
cloud:
config:
server:
native:
search-locations: file:///${user.home}/native-file-repo # 로컬 폴더의 환경 파일 위치
git:
default-label: master
uri: https://github.com/duckbill413/spring-cloud-microservice.git
# uri: file:///D:/git/spring-cloud-microservice
# username: [username] # private repository 인 경우
# password: [password] # private repository 인 경우
profiles.active
: native
로컬 파일을 활용할 것임을 알려줌search-location
: 로컬 환경 변수 파일들이 위치하는 폴더 경로 (${user.home}
: window 는 C:\{username}