TJLim's

Spring Profile 설정 - 간단한 서버 환경별 설정 본문

웹/Spring

Spring Profile 설정 - 간단한 서버 환경별 설정

Tae_jun 2020. 3. 19. 17:32


배포할 때마다 Property 파일의 IP 등등을 수정하고 


빌드를 여러번 하는게 귀찮거나


로컬, 개발, 검증, 운영 이런 구성일 때 한 번의 빌드로 해결하는 방법인데 


톰캣 구동전 VM options 에 값을 넣어주면서 실행합니다. 


저의 환경은 전자정부환경의 Spring, IntelliJ 이며, 서버는 윈도우 톰캣입니다. (Spring Profile 기능은 Spring 3.1 이상부터 지원)




1. 먼저, 기존 db 접속 정보 message-common.properties 파일입니다. - 서버마다 배포할 때 주석 처리/해제 해서 여러번 빌드/배포해야 하니 귀찮습니다....






2. context-common 으로 가면, properties 파일을 참조하는 <util:~~> 부분이 있습니다. 이 부분을 수정하여 개발/운영 환경을 분리시킬겁니다.






3. 일단 환경에 맞게 property 파일을 만듭니다. 저는 개발, 운영 두 개를 만들었습니다.

 

 


- 개발 서버에서는 config_dev.properties, 운영 서버에서는 config_prd.properties 를 참조합니다.

  기존의 message-common.properties 의 db 접속정보는 삭제.

    



4. 다시 context-common 에서 <util:~~> 부분을 하기와 같이 변경합니다. 



- 변경전

<util:properties id="global" location="classpath:egovframework/message/message-common.properties" />


변경후

<util:properties id="global" location="classpath:/egovframework/message/config_#{systemProperties['spring.profiles.active']}.properties" />


==> 톰캣 구동시 ex) -Dspring.profiles.active=dev 를 넣어주면 #{systemProperties['spring.profiles.active']}  에  dev 라는 값이 들어가면서 config_dev.properties 파일을 참조합니다



 

5-1. 로컬일 경우 Run/Debug Configurations 에서 넣어줍니다.





5-2. 윈도우 서버일 경우 우측 하단에 있는 톰캣 더블 클릭 -> Java 탭 -> Java Options 칸에 "-Dspring.profiles.active=xxx" 옵션을 넣어줍니다.




5-3 리눅스일 경우 startup.sh 파일을 열어 export JAVA_OPTS="-Dspring.profiles.active=xxx" 를 추가해줍니다.





6. 톰캣 실행

 



- System.getProperty("spring.profiles.active") 로 해당 환경을 가져와서 특정 환경에만 적용시키는 응용 방법도 생각할 수 있음.






Comments