Spring Cloud Config란?
Spring Cloud Config는 분산 시스템에서 애플리케이션 설정을 중앙 집중식으로 관리하는 솔루션이다.
환경별 설정을 관리하고, 변경 사항을 실시간으로 반영할 수 있다.
왜 사용할까?
애플리케이션마다 application.yml을 따로 관리하면 설정 변경이 번거로움
마이크로서비스 환경에서 각 서비스의 설정을 중앙에서 통합 관리 가능
Git 또는 로컬 파일 시스템을 설정 저장소로 활용 가능
설정 변경 시 애플리케이션을 재시작하지 않고 실시간 반영 가능
사용법
1. Git private Repository 생성 후 SSH로 연결
window cmd창에서 아래 명령어 실행
ssh-keygen -t rsa -b 4096 -C "email"
실행하면 C:\User\사용자이름\.ssh 에 public key와 private key가 생성된다.
id_rsa.pub 가 public key고 id_rsa가 private key다.
이제 id_rsa.pub를 github에 등록해주자!
깃허브의 개인 설정에 들어가서 SSH and GPG keys 메뉴로 들어간다.
New SSH Key를 누르고
title은 아무거나 입력하고, Key에 id_Rsa.pub의 내용을 복붙해주고 Add SSH key를 눌러 마무리한다.
정상적으로 SSH Key가 등록되었으면
git bash에서 아래 명령어를 실행했을 때,
ssh -T git@github.com
아래처럼 나오면 정상적으로 등록된 것이다.
> Hi USERNAME! You've successfully authenticated, but GitHub does not > provide shell access.
> The authenticity of host 'github.com (IP ADDRESS)' can't be established. > ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. > Are you sure you want to continue connecting (yes/no)?
이런 경고도 나올 수 있는데 그냥 yes하고 넘어가면 된다.
2. Config 서버 구축
설정을 중앙에서 관리하는 역할을 하는 config-server 모듈을 하나 생성하고 다음과 같은 의존성을 추가해준다.
ext {
set('springCloudVersion', "2024.0.0")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-config-server'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
메인 애플리케이션에 @EnableConfigServer 어노테이션을 추가해준다.
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
그리고 config 서버의 application.yml 파일에 git과의 연결을 위한 설정을 추가해준다.
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: git@github.com:github닉네임/repository이름it # Git 저장소 URL
private-key: |
-----BEGIN OPENSSH PRIVATE KEY-----
id_rsa의 내용
-----END OPENSSH PRIVATE KEY-----
default-label: main
clone-on-start: true # 서버 시작 시 설정 클론
3. 각 모듈의 application.yml 설정
spring:
application:
name: api-gateway
config:
import: "optional:configserver:http://localhost:8888"
이렇게 설정해주면 git repository의 api-gateway.yml 파일을 가져와서 gateway 모듈에서 사용하게 할 수 있다.
아까 만든 private repository에 기존에 사용하던 gateway의 application.yml 파일을 올리고 이름을 api-gateway로 바꿔주자.
주의할 점
설정 파일은 크게 다음의 위치에 존재할 수 있으며 다음의 순서대로 읽어진다. 나중에 읽어지는 것이 우선순위가 높다.
- 프로젝트의 application.yaml
- 설정 저장소의 application.yaml
- 프로젝트의 application-{profile}.yaml
- 설정 저장소의 {application name}/{application name}-{profile}
또 git 서버나 설정 서버의 장애가 전파될 수도 있고, 우선순위에 의해 덮어씌워질 수 있으니 사용에 주의해야한다.
다음엔 git repository의 설정이 변경되면 자동으로 반영하는 법을 써야겠다.
'Spring' 카테고리의 다른 글
Vue.js 프로젝트를 EC2에서 Nginx로 배포하는 방법 (0) | 2025.03.08 |
---|---|
AWS에 백엔드 서버 배포하는 법 (0) | 2025.03.07 |
서킷 브레이커(Circuit Breaker)란? (0) | 2025.03.04 |
멀티모듈 환경의 Swagger 설정 (2) | 2025.03.04 |
멀티 모듈에서 타 모듈의 Bean 등록 오류 (0) | 2025.02.28 |