실전! 스프링과 함께하는 환경변수 관리 변천사 발표자료
TRANSCRIPT
$실전! 스프링과 함께하는 환경변수 관리 변천사{ {
KSUG 이수홍
$환경변수가 뭔가요??
$
$
$
$
$WIKI를 찾아보니 “환경변수는 프로세스가 컴퓨터에서 동작하는 방식에 영향을 미치는, 동적인 값들의 모임이다.”
$“스프링 프레임워크” 환경변수
$
$ 그냥 하면 되는것 아닌가?
$ 하지만 ...
그냥 하면 되는것 아닌가?
$
$자주 변경되거나 확정되지 않은 데이터
$자주 변경되거나 확정되지 않은 데이터
같은 용도 이지만 개발, 테스트, 운영 등
다양한 조건마다 다른 데이터
$자주 변경되거나 확정되지 않은 데이터
같은 용도 이지만 개발, 테스트, 운영 등
다양한 조건마다 다른 데이터
다양한 곳에서 사용하는 공통 데이터 존재
$그래서 환경변수가 중요한 이유?
$그래서 환경변수가 중요한 이유?
바로 시스템의 유연성
$기존 환경변수 방법
$기존 환경변수 방법
JAVA
$(1) 클래스 상수
• JAVA의 static final 키워드를 사용하여 클래스 변하지 않는 변수로 사용
• 상수를 가지는 특정 클래스 의존적
• 변수 변경 때 마다 참조하고 있는 클래스를 재 컴파일
• 현재도 많이 사용
$(2) properties 파일
• Key=Value 형태의 데이터
• JAVA에서 기본 지원 ( java.util.Properties )
• 한글 미지원
$Spring with Not Profile
$Spring with Not Profile
$기본적으로 Profile기능을 지원하지 않는 스프링 3.0 이하 버전들
$기본적으로 Profile기능을 지원하지 않는 스프링 3.0 이하 버전들
$Profile 기능란?
특정 Profile에 따라 환경변수 및 기능 등 을
다르게 할 수 있도록 구성하는 것
$예를들면
$예를들면
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발
$예를들면
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.11.221:1521/test1 db.user=test1 db.password=test1
테스트 서버
$예를들면
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.11.221:1521/test1 db.user=test1 db.password=test1
테스트 서버
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.131.221:1521/staging db.user=staging1 db.password=staging1
스테이징 서버
$예를들면
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.11.221:1521/test1 db.user=test1 db.password=test1
테스트 서버
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.131.221:1521/staging db.user=staging1 db.password=staging1
스테이징 서버
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@(DESCRIPTION = (LOAD_BALANCE = OFF) … db.user=prod db.password=prod
상용 서버
$Profile 기능을 적용할만한 방법이 없을까?
$약간 무식한(?) 방법?
1. 모든 속성의 데이터를 구분
2. java 실행시 -D옵션으로 프로파일정보를 넘기고
• 예를 들면 java -Dprofile=prod
3. 내부에서 코딩(?)한다.
$약간 무식한(?) 방법?
$약간 무식한(?) 방법?
$약간 무식한(?) 방법?
$약간 무식한(?) 방법?
$Maven과 Profile 기능 그리고 properties
$Maven, Profile
$Maven, Profile
• Maven은 빌드 및 배포 프로그램
$Maven, Profile
• Maven은 빌드 및 배포 프로그램
• 빌드시 Profile을 선택할 수 있는 기능 제공
$Maven, Profile
• Maven은 빌드 및 배포 프로그램
• 빌드시 Profile을 선택할 수 있는 기능 제공
• “$ mvn -P프로파일명 install” 형태로 빌드
$Maven, Profile
• Maven은 빌드 및 배포 프로그램
• 빌드시 Profile을 선택할 수 있는 기능 제공
• “$ mvn -P프로파일명 install” 형태로 빌드
• 플러그인 사용하여 Profile 에따른 properties 선택
$예제에 앞서
$예제에 앞서
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발: build-local.properties
$예제에 앞서
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발: build-local.properties
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.11.221:1521/test1 db.user=test1 db.password=test1
테스트: build-test1.properties
$예제에 앞서
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발: build-local.properties
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.11.221:1521/test1 db.user=test1 db.password=test1
테스트: build-test1.properties
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.131.221:1521/staging db.user=staging1 db.password=staging1
스테이징: build-staging.properties
$예제에 앞서
db.driver=org.h2.Driver db.url=jdbc:h2:mem:test;MODE=Oracle; db.user=sa db.password=
개발: build-local.properties
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.11.221:1521/test1 db.user=test1 db.password=test1
테스트: build-test1.properties
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@//123.232.131.221:1521/staging db.user=staging1 db.password=staging1
스테이징: build-staging.properties
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@(DESCRIPTION = (LOAD_BALANCE = OFF) … db.user=prod db.password=prod
상용서버: build-prod1.properties
$Maven Profile 활용 1
$Maven Profile 활용 1
<context:property-placeholder location=“classpath:META-INF/config.xml"/>
스프링 “mvc-config.xml”
$Maven Profile 활용 1
<context:property-placeholder location=“classpath:META-INF/config.xml"/>
스프링 “mvc-config.xml”
<properties> <comment>환경변수 설정파일</comment> <entry key="profiles.active">${profiles.active}</entry> <entry key="db.driver">${db.driver}</entry> <entry key="db.url">${db.url}</entry> <entry key="db.user">${db.user}</entry> <entry key="db.password">${db.password}</entry></properties>
환경변수 “META-INF/config.xml”
$Maven Profile 활용 2
$Maven Profile 활용 2
$ mvn -Plocal install
$Maven Profile 활용 2
$ mvn -Plocal install
build-local.properties
$Maven Profile 활용 2
<properties> <comment>환경변수 설정파일</comment> <entry key="profiles.active">local</entry> <entry key="db.driver">org.h2.Driver</entry> <entry key="db.url">jdbc:h2:mem:test(생략)</entry> <entry key="db.user">sa</entry> <entry key="db.password"></entry></properties>
환경변수 “META-INF/config.xml”
$ mvn -Plocal install
build-local.properties
$Maven Profile 활용 2
<properties> <comment>환경변수 설정파일</comment> <entry key="profiles.active">local</entry> <entry key="db.driver">org.h2.Driver</entry> <entry key="db.url">jdbc:h2:mem:test(생략)</entry> <entry key="db.user">sa</entry> <entry key="db.password"></entry></properties>
환경변수 “META-INF/config.xml”
$ mvn -Plocal install
build-local.properties
$Maven Profile 활용 3
$Maven Profile 활용 3$ mvn -Pstaging install
$Maven Profile 활용 3$ mvn -Pstaging install
build-staging.properties
$Maven Profile 활용 3$ mvn -Pstaging install
build-staging.properties
<properties> <comment>환경변수 설정파일</comment> <entry key="profiles.active">staging</entry> <entry key="db.driver">oracle.jdbc.driver.OracleDriver</entry> <entry key="db.url">jdbc:oracle:thin:@//123.232.1(생략)</entry> <entry key="db.user">prod</entry> <entry key="db.password">prod</entry></properties>
환경변수 “META-INF/config.xml”
$Maven Profile 활용 3$ mvn -Pstaging install
build-staging.properties
<properties> <comment>환경변수 설정파일</comment> <entry key="profiles.active">staging</entry> <entry key="db.driver">oracle.jdbc.driver.OracleDriver</entry> <entry key="db.url">jdbc:oracle:thin:@//123.232.1(생략)</entry> <entry key="db.user">prod</entry> <entry key="db.password">prod</entry></properties>
환경변수 “META-INF/config.xml”
$Maven Profile 장점
$Maven Profile 장점
• 스프링 애플리케이션에서는 단순한 설정형태 유지
$Maven Profile 장점
• 스프링 애플리케이션에서는 단순한 설정형태 유지
• 이클립스 및 인텔리J 등 IDE 플러그인에서 Profile 지원 ( Profile을 변경하면 자동으로 빌드 된다거나 ... )
$Maven Profile 단점
• Profile 단위로만 properties 생성하기 때문에 중복 환경변수들이 생성
• properties 변경시마다 maven build 필요
• Maven이라는 도구에 종속되는 문제
$그 이후 드디어 ...
$프로파일 기능을 지원하는 스프링 3.1 출시
그 이후 드디어 ...
$스프링 3.1 기능?
프레임워크에서 Profile 기능이 추가Profile에 따른 Bean 정보 제어 ( @Profile(“...”), <beans profile=“...”> )
• 환경변수 기능이 “Environment”클래스로 통합관리
( 관련내용 )
$스프링 3.1 기능 적용
$• web.xml 또는 애플리케이션 실행시 Profile 파라메터를 넘겨서 선택하는 형태
스프링 3.1 기능 적용
$• web.xml 또는 애플리케이션 실행시 Profile 파라메터를 넘겨서 선택하는 형태
스프링 3.1 기능 적용
<context-param> <param-name>spring.profiles.default</param-name> <param-value>프로파일명</param-value></context-param>
또는 애플리케이션 실행시 $ java … -Dspring.profiles.active=프로파일명
$• web.xml 또는 애플리케이션 실행시 Profile 파라메터를 넘겨서 선택하는 형태
스프링 3.1 기능 적용
<context-param> <param-name>spring.profiles.default</param-name> <param-value>프로파일명</param-value></context-param>
또는 애플리케이션 실행시 $ java … -Dspring.profiles.active=프로파일명
• 선택된 Profile을 Spring 프레임워크에서 환경변수 파일을 직접 선택
$• web.xml 또는 애플리케이션 실행시 Profile 파라메터를 넘겨서 선택하는 형태
mvc-config.xml<context:property-placeholder location="classpath:META-INF/config-${spring.profiles.active:local}.properties"/>
스프링 3.1 기능 적용
<context-param> <param-name>spring.profiles.default</param-name> <param-value>프로파일명</param-value></context-param>
또는 애플리케이션 실행시 $ java … -Dspring.profiles.active=프로파일명
• 선택된 Profile을 Spring 프레임워크에서 환경변수 파일을 직접 선택
$개선 후 장점
$• Maven 종속 부분 제거
개선 후 장점
$• Maven 종속 부분 제거
• 프로퍼티 변경 시 부담감 축소 ( No Build! )
개선 후 장점
$• Maven 종속 부분 제거
• 프로퍼티 변경 시 부담감 축소 ( No Build! )
• 스프링 Bean 자체도 Profile 별로 구성 가능
개선 후 장점
$• Maven 종속 부분 제거
• 프로퍼티 변경 시 부담감 축소 ( No Build! )
• 스프링 Bean 자체도 Profile 별로 구성 가능
개선 후 장점
<beans profile="prod"> <bean class="abc"/> <!-- ... --></beans><beans profile="local"> <bean class="cde"/> <!-- ... --></beans>
$ 여전히 properties와 Profile 조합의 단점이 존재
$
$그 이후 Spring Boot 프로젝트 활성
$아하! 최근 Spring Boot 에서 “YAML” 설정방법을 지원
하는구나!
$YAML 핵심 장점
• 심플! ( json 비슷 )
• 한글지원!
• 들여쓰기 강제화! ( 파이썬 느낌 !? )
• 하나의 파일에서 문서를 나눌 수 있는 문법 (---) 지원
service: name: LeeSuHong phone: "010-‐5482-‐1212" sex: 'M'
-‐-‐-‐ spring.profiles: prod1,prod2,prod3
db: driver: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@//123.45.67.78:1521/prod user: prod password: prod1234
-‐-‐-‐ spring.profiles: test1,test2
db: driver: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@//123.45.67.78:1521/dev user: devtest password: test
$Spring Boot와 YAML
• YAML 문서를 나누는 특징을 Profile기능에 활용
• 하나의 파일에서 여러가지 Profile을 기술
#공통부분 (모든프로파일의 속성) ---spring.profiles: prod1,prod2#상용 서버 관련 속성---spring.profiles: test1# 테스트1 서버 속성 ---spring.profiles: test2# 테스트2 서버 속성---spring.profiles: local#로컬 관련 속성
$Spring Boot 환경변수 지원
• 환경변수와 Java bean과 자동 바인딩 기능을 제공
$Spring Boot 환경변수 지원
• 환경변수와 Java bean과 자동 바인딩 기능을 제공
환경변수 데이터를 Java bean과 바인딩
$Spring Boot 환경변수 지원
• 환경변수와 Java bean과 자동 바인딩 기능을 제공
환경변수 데이터를 Java bean과 바인딩
Spring Bean 형태로 객체를 주입시켜 사용
$검토 후 현재 프로젝트에서 YAML로 형태로 변경 적용
결정!
$YAML적용 후 장점
$YAML적용 후 장점
• 파일 기준 Profile 위주의 구성 -> 문서 내용 위주로 변경
$YAML적용 후 장점
• 파일 기준 Profile 위주의 구성 -> 문서 내용 위주로 변경
$YAML적용 후 장점
• 파일 기준 Profile 위주의 구성 -> 문서 내용 위주로 변경
• 환경변수의 구조화
$YAML적용 후 장점
• 파일 기준 Profile 위주의 구성 -> 문서 내용 위주로 변경
• 환경변수의 구조화
• 중복된 데이터 축소
$지금까지 내용 정리
$지금까지 내용 정리자바상수 클래스
$지금까지 내용 정리자바상수 클래스
JAVA 소스에 종속적인 환경변수
$지금까지 내용 정리자바상수 클래스
Properties 파일
$지금까지 내용 정리자바상수 클래스
Properties 파일Key=Value의 심플한
환경변수
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven ProfileBuild 시 Profile결정 후 환경정보 추가
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile프레임워크에서 Profile 환경변수 선택
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile
YAML
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile
YAMLYAML형태의 환경변수
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile
YAML
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile
YAML
$지금까지 내용 정리자바상수 클래스
Properties 파일
Maven Profile
Spring 3.1 Profile
YAML
시스템 규모의 대형화
$운영할수록 늘어 가는 서버, 애플리케이션,
환경변수
$환경변수 변경시 마다 재배포와 재시작
그리고
$클라우드, MSA ( Micro Services Architecture)
$늘어나는 애플리케이션의 환경변수를 제어 필요성
$그런 요구에 의해서 나온 프로젝트
Spring Cloud Config
$Spring Cloud Config ?
• 환경변수를 제공 해주는 REST API 서버 (필수)와 환경변수를 받는 클라이언트로 구성 (옵션)
• JSON 형식으로 제공 ( 어떤 서버에서도 사용가능 )
• 환경변수 관리 ( GIT, SVN 등 이용 )
• 환경변수에 대한 암복호화 기능 내장
• 다양한 Application, Profile, Version 형태를 지원
$기존 환경변수 구조
시스템서버1
환경변수정보
시스템서버2
환경변수정보
시스템서버3
환경변수정보
소스버전관리 시스템
$기존 환경변수 구조
시스템서버1
환경변수정보
시스템서버2
환경변수정보
시스템서버3
환경변수정보
소스버전관리 시스템
소스 배포로 인한 환경변수 갱신
$기존 환경변수 구조
시스템서버1
환경변수정보
시스템서버2
환경변수정보
시스템서버3
환경변수정보
소스버전관리 시스템
$Spring Cloud Config
시스템 구조
Spring 시스템
서버1
Spring Cloud Config Server
Spring 시스템
서버2
NodeJS 시스템
서버3
환경변수 저장소 ( GIT, SVN, Database 등 )
Spring Cloud Config Client
Spring Cloud Config Client
$Spring Cloud Config
시스템 구조
Spring 시스템
서버1
Spring Cloud Config Server
Spring 시스템
서버2
NodeJS 시스템
서버3
환경변수 저장소 ( GIT, SVN, Database 등 )
환경변수 정보 버전 관리
Spring Cloud Config Client
Spring Cloud Config Client
$Spring Cloud Config
시스템 구조
Spring 시스템
서버1
Spring Cloud Config Server
Spring 시스템
서버2
NodeJS 시스템
서버3
환경변수 저장소 ( GIT, SVN, Database 등 )
JSON API 형태로 환경변수를 시스템에 공급
환경변수 정보 버전 관리
Spring Cloud Config Client
Spring Cloud Config Client
$API서버를 통하여 다수의 애플리케이션에 환경변수를
제공
$API서버를 통하여 다수의 애플리케이션에 환경변수를
제공
$Spring Boot의 모듈은 대부분 환경변수로 제어가능
Spring Cloud Config 와 결합으로 다수의 시스템의 세세한 컨트롤까지 가능
$Spring Boot의 모듈은 대부분 환경변수로 제어가능
Spring Cloud Config 와 결합으로 다수의 시스템의 세세한 컨트롤까지 가능
$관련 소스
https://github.com/sbcoba/spring-camp-2015-sample
$감사합니다.