해당 서버를 통해, properties를 하나의 서버로 모으고
config client 서버가 시작할 때 해당 서버로부터 property 값을 읽어와 적용하게 된다.
가장 먼저, 해당 properties 값을 제공하는 config server를 셋팅해보도록 하자.
아래와 같이 @EnableConfigServer 와 @SpringBootApplication 를 설정함으로써
해당 서버가 cloud config server로 사용되는 것으로 선언한다.
package org.blog.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.context.annotation.Configuration; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
이후, 아래와 같이 resource를 어디에서 읽어올지 설정하면 끝~!!
아래 예제에서는 git이 아닌, 서버 내 file을 통해 property를 제공하도록 설정하였다.
spring: profiles: active: native cloud: config: server: native: search-locations: classpath:/config/{application}/{profile} server: port: 8888
config server를 설정하고 테스트하면서, 어려웠던 점은 해당 config server에서 제대로 property를 제공하고 있는지 확인하는 작업이었다.
다수의 구글링 끝에 아래와 같이 확인이 가능하다는 것을 찾았다~!!
http://localhost:8888/config-client/stg { "name": "config-client", "profiles": [ "stg" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "classpath:/config/config-client/stg/application.properties", "source": { "message": "hellow stg" } } ] }
다음으로, config client를 설정해보도록 하자.
spring cloud starter config 를 dependency에 추가해준 후,
아래와 같이 설정하면, cloud 상에서 해당 서버의 name과 profile 설정이 가능하다.
아래와 같이 설정한 값을 바탕으로 cloud config server로부터 값을 읽어오게 된다.
spring: cloud: config: name: config-client profile: stg
- profile이 dev인 경우
2017-06-13 23:57:21.886 INFO 12464 --- [ main] org.blog.test.ConfigClientApplication : message : hellow dev
- profile이 stg인 경우
2017-06-13 23:58:13.139 INFO 1056 --- [ main] org.blog.test.ConfigClientApplication : message : hellow stg
전체 예제는 아래 링크에서 다운로드 가능하다.
https://gitlab.com/shashaka/config-server-project
https://gitlab.com/shashaka/config-client-project
댓글 없음 :
댓글 쓰기