Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

아낌없이 주는 자바 개발자

제2장 Spring Cloud Config - Config Server 본문

Spring/Cloud

제2장 Spring Cloud Config - Config Server

홍캐리 2016. 7. 21. 14:04

0. 본 포스팅은

1장에서 예고한대로 Git repository 에 설정파일을 두고, Config Server 가 이를 참조하여

각 Application 에 properties 를 제공하는 방법을 가이드합니다.


1. Git Repository 에 properties 파일 만들기.

Github 에 로그인하여 Repository 를 만든다.(계정이 없다면 회원가입하세요. public은 무료예요)


2. 각 application 이 사용할 config 파일을 만든다.

본인 컴퓨터에 git repository 를 init 하고 commit 하는 방식으로 하셔도 좋고,

여기서 그냥 새로 Create new file 로 만드셔도 좋습니다.

저는 account-service.yml, product-service.yml 로 만들었습니다.(yml 이 아닌 properties 로 만들어도 무방함.)

좋군요.

product-service 도 마찬가지로 만드시고, 원하는 설정값을 입력합니다.


3. Spring Boot로 Config-Server Application 만들기

http://start.spring.io 에서 하셔도 좋고, STS(Spring Tool Suite) 에서 하셔도 좋은데요.

저는 STS 에서 프로젝트를 만들어보도록 하겠습니다.

1) New > Other > Spring > Spring Starter Project


2) Config server application 정보 작성


3) 사용할 Dependencies 선택

Spring Boot 를 써보신 분은 알겠지만 Application이 어떤 역할을 수행하느냐에 따라 Dependencies 를 

선택해주면 알아서 프로젝트를 만들어주죠.

이번에 만들 Application 은 Cloud Config Server 이므로 Config Server 를 선택해주고, Finish 합니다.


4) Application 이 만들어졌습니다.

기본적으로 Spring Boot Application 으로써 실행될 수 있도록 ConfigServerApplication.java 이 만들어졌구요.

Spring Boot Application 의 환경 설정을 위한 application.properties 도 만들어졌습니다.



4. Config Server 역할 부여

이제 Git repository 에 각 Application 의 properties 도 생성하였고,

이걸 서비스해줄 Config Server Application 도 생성되었으니, Config Server 의 역할을 수행할 수 있도록

간단한 설정을 해줄 차례입니다.


1) ConfigServerApplication.java 를 열어 아래와 같이 @EnableConfigServer 어노테이션을 추가합니다.

 간단하게 ConfigServer 역할을 수행하는걸 활성화해준다고 생각하시면 될 것 같습니다.


2) application.properties 를 열어 spring.cloud.config.server.git.uri 설정을 추가해줍니다.

server.port 를 8888 로 바꾼건 local 에서 config-server, account-service, product-service 

세개의 Application 을 띄울거라 포트 충돌나지 않게 하기 위해서 바꿨습니다. (기본은 8080입니다)

참고로 전 properties 를 지우고 yml 파일로 바꿨습니다만 properties 로 해도 무방합니다.

※properties 로 할 경우 : spring.cloud.config.server.git.uri=https://github.com/includym/devguide-config.git


이걸로 모든 준비가 끝났습니다.

config-server 를 Run As > Spring Boot App 으로 실행합니다.


5. Config 확인

ConfigServer 는 properties 조회를 위해 여러가지 패턴을 제공합니다.

그 중 /{application}/{profile}[/{label}] 패턴으로 active profile 은 default 로 조회해봅니다.

잘 가져오는걸 확인할 수 있습니다.

spring.profiles 를 모르시는 분들을 위해 추가로 설명해드리자면

1장에서 설명했던 것처럼 properties는 로컬, 개발, 스테이징, 운영 등 어떤 서버냐에 따라서 

각자 다른 값을 가질 수 있습니다. WAS 가 Application 을 구동할 때 spring.profiles.active 라는

JVM argument 를 전달하면 전달 된 profiles 에 맞는 profiles 에 맞는 설정값만을 읽습니다.

보통 로컬이나 개발, 스테이징에는 argument 를 던져서 특정 서버의 properties 를 읽게끔 하고,

운영에는 profiles 를 지정하을 때 제공되는 default 를 사용합니다.

위 예제는 그 default 를 사용한 것입니다.


6. 다음은

다음은 로컬, 개발, 스테이징, 운영에서 서로 다른 properties 값을 사용하는 방법을 포스팅하면서

Config Server 를 마무리하고, Config Client 로 넘어가도록 하겠습니다.


끗 'ㅅ'

Comments