poscoict-glueframework.github.io

Spring Cloud Config

분산시스템에서는 application의 설정값을 config server으로부터 제공받습니다.
Git과 같은 외부환경에 property들이 관리되고, config server에서 client들(application)에게 property를 제공합니다.

Image

Config Server는 Spring Boot Application으로 개발할 수 있습니다.

Spring Boot Application 들은 Config Server를 바라보도록 합니다.

Try it

Git 준비

config-repo 라는 git repository를 만드세요.

$ # 1. config-repo 생성
$ cd /c/workspace
$ mkdir config-repo
$ 
$ # 2. git init
$ cd config-repo
$ git init .
$ 
$ # 3. application의 property 파일 추가
$ echo app.title: Welcome. > demo.properties
$ echo biz.name: DemoBiz >> demo.properties
$ echo app.title: Demo Developing.. > demo-dev.properties 
$ echo app.title: Demo Testing.. > demo-test.properties 
$ echo app.title: Hi.  > sample.properties
$ echo biz.name: SampleBiz >> sample.properties
$ echo app.title: Sample Develooping.. > sample-dev.properties 
$ echo app.title: Sample Testing.. > sample-prod.properties
$ 
$ # 4. git repository 에 반영 
$ git add -A .
$ git commit -m "init"

Spring Boot Application 별 Config 파일은 spring.application.name 에 해당하는 값입니다. 그리고 Profile별로 Config 파일을 생성해서 관리합니다.

Server

Server용 Spring Boot Application을 다음과 같이 개발합니다.

$ # 1. 프로젝트 생성
$ # 1-1 https://start.spring.io ( 아래 참고 )
$ 
$ # 1-2 다운로드 폴더로 이동
$ cd ~/Downloads
$ 
$ # 1-3. workspace에 압축풀기
$ unzip config-server.zip -d /c/workspace
$ # 2. Eclipse 에 import 후 개발
$ # 2-1. Application 수정 ( ConfigServerApplication.java 아래 참고 ) 
$ # 2-2. property 수정 ( application.properties 아래 참고 ) 
$ # 3. 테스트
$ # 3-1 이동
$ cd /c/workspace/config-server
$ 
$ # 3-1. config-server 실행
$ mvn spring-boot:run
$ 
$ # 3-2. config-server 체크
$ # curl localhost:8888/demo-dev.yml

Client Side

Client용 Spring Boot Application을 다음과 같이 개발합니다.

$ # 1. 프로젝트 생성
$ # 1-1 https://start.spring.io ( 아래 참고 )
$ 
$ # 1-2 다운로드 폴더로 이동
$ cd ~/Downloads
$ 
$ # 1-3. workspace에 압축풀기
$ unzip config-client.zip -d /c/workspace
$ # 2. 프로젝트로 이동
$ # 2-1 이동
$ cd /c/workspace/config-client
$ 
$ # 2-2. bootstrap.properties 로 변경
$ mv src/main/resources/application.properties src/main/resources/bootstrap.properties
$ # 3. Eclipse로 import
$ # 3-1. Application 수정 ( ConfigClientApplication.java )
$ # 3-2. property 수정 ( bootstrap.properties )
$ # 4 테스트
$ # 4-1. config-client 실행
$ # mvn spring-boot:run
$ 
$ # 4-2. config-client 체크
$ # curl localhost:8080/title

etc

cd /c/workspace
server.port=8888
spring.application.name=config-server
# 
spring.cloud.config.server.git.search-paths='{application}'
spring.cloud.config.server.encrypt.enabled=false
spring.security.user.password= 
encrypt.failOnError=false
encrypt.key=yujin
encrypt.keyStore.location=classpath:server.jks
encrypt.keyStore.password=changeit
encrypt.keyStore.alias=poscoict
encrypt.keyStore.secret=changeit
curl localhost:8888/encrypt -d user

Image

Ref. 참고