[Spring] 스프링 부트 2.4.0 버전 이후 개발 환경 분리 방법

rockstar·2023년 6월 6일
1

Development

목록 보기
1/1

일반적으로 개발을 하고 배포를 하는 과정에서 application.properties 또는 application.yml파일도 같이 포함이 되어 패키징이 된다. 개인 프로젝트 수준의 경우에는 로컬 환경에서 그대로 서버를 켜서 운영까지 하면 되지만, 규모가 큰 프로젝트의 경우에는 환경이 분리가 될 것이고, 그 환경에 맞는 설정값들을 입력을 해줘야 되는 상황이 올텐데 이럴 때는 어떻게 하면 좋을지 한 번 같이 고민을 해보자.

Auto-Configuration

Spring 프로젝트에서 사용하려고 하는 설정 파일이 어떤 방식으로 동작하는지 알아야 한다. application.yml 또는 application.properties에서 작성하는 설정들은 Spring Boot의 자동 구성을 통해 빈(Bean)으로 등록된다.설정 파일에 등록된 설정들은 이 자동 구성 기능에 의해 해석되고, 해당 설정을 기반으로 필요한 빈들이 생성되고 등록되는 것이다.

아래의 이미지를 한 번 보면, 실제로 Spring Boot가 제공하는autoconfigure 패키지가 존재하는 걸 볼 수 있다.



그 중에서도 web패키지 내부를 보면 ServerProperties가 있는데, Port 속성 Address 속성 등 해당 객체의 필드에 입력이 되어 Bean이 생성될 수 있겠구나 유추해볼 수 있다. 설정 값도 결국엔 어느 Bean 객체의 값으로 설정이 되어 Bean의 생명 주기 동안 함께 한다는 것이다. 생각해보면 그럴 것이 아래처럼 원하는 값을 입력하고, @Value를 통해서 객체의 필드에 주입하는 방식으로 application.yml을 작성해본 적이 있을 것이다.



spring.profiles의 경로를 보면 org.springframework.boot.context.config.Profiles로 지정이 되어 있고, 우리는 여기에 값을 주입해주는 것이다.

환경 분리

그렇다면 운영하는 환경에서는 8081이라는 포트를 사용하고 싶지만, 로컬에서는 8000을 사용하고 싶은 경우 등 환경에 따라 다른 값을 주고 싶은 경우가 어떻게 해야 할까? 이 때는, Spring에서 지원하는 기능을 사용해서 분리할 수 있게 된다.

Spring Boot 2.4.0 이전까지 사용되던 spring.profiles.active가 deprecated 되었기 때문에, spring.config.activate.on-profile로 설정하면 된다. 추가적으로 spring.profiles.default를 사용하여 active에 어떤 값도 들어오지 않는 경우 설정한 profile을 실행할 수 있다. application.properties 또는 application.yml과 같은 설정 파일에서는 동일한 속성을 여러 번 사용할 수 없다. 이럴 때는 --- 구분자를 추가해주자 !

빌드를 해보자 !

터미널에서 아래 명령어를 실행해보자 !

현재 jar파일 또는 war파일에는 설정 파일이 모두 패키징되어 포함이 되어있을 것이고, 그 중에서 dev 설정을 사용하고 싶을 때-Dspring.profiles.active=dev옵션을 추가하면 된다.

-Dspring은 JVM의 시스템 속성이며, Spring과 관련된 속성을 설정할 때 사용된다. -Dspring이 설정 파일의 spring과 치환되어, -Dspring.profiles.active에 값을 설정할 수 있게 된다.

여기서 주의해야 할 점은 java -jar Dspring.profiles.active={환경}이 jar 또는 war 파일보다 먼저 와야 한다는 것이다.

본인은 java -jar {파일명}.war Dspring.profiles.active={환경}으로 했다가 적용이 안 돼서 꽤 애를 먹었던 기억이 있다.

java -jar -Dspring.profiles.active=local mvc-0.0.1-SNAPSHOT.war

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.0)

2023-06-04T22:26:18.299+09:00  INFO 71865 --- [           main] com.scvefg.mvc.MvcApplication            : Starting MvcApplication v0.0.1-SNAPSHOT using Java 17.0.7 with PID 71865 (/Users/j/j/app/spring/mvc/build/libs/mvc-0.0.1-SNAPSHOT.war started by j in /Users/j/j/app/spring/mvc/build/libs)
2023-06-04T22:26:18.300+09:00  INFO 71865 --- [           main] com.scvefg.mvc.MvcApplication            : The following 1 profile is active: "local"
2023-06-04T22:26:18.736+09:00  INFO 71865 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8000 (http)
2023-06-04T22:26:18.742+09:00  INFO 71865 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-06-04T22:26:18.742+09:00  INFO 71865 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-06-04T22:26:18.801+09:00  INFO 71865 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-06-04T22:26:18.802+09:00  INFO 71865 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 476 ms
2023-06-04T22:26:18.962+09:00  INFO 71865 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8000 (http) with context path ''
2023-06-04T22:26:18.971+09:00  INFO 71865 --- [           main] com.scvefg.mvc.MvcApplication            : Started MvcApplication in 0.876 seconds (process running for 1.092)

java -jar -Dspring.profiles.active=dev mvc-0.0.1-SNAPSHOT.war

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.0)

2023-06-04T22:26:25.801+09:00  INFO 72033 --- [           main] com.scvefg.mvc.MvcApplication            : Starting MvcApplication v0.0.1-SNAPSHOT using Java 17.0.7 with PID 72033 (/Users/j/j/app/spring/mvc/build/libs/mvc-0.0.1-SNAPSHOT.war started by j in /Users/j/j/app/spring/mvc/build/libs)
2023-06-04T22:26:25.802+09:00  INFO 72033 --- [           main] com.scvefg.mvc.MvcApplication            : The following 1 profile is active: "dev"
2023-06-04T22:26:26.231+09:00  INFO 72033 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-06-04T22:26:26.237+09:00  INFO 72033 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-06-04T22:26:26.237+09:00  INFO 72033 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-06-04T22:26:26.292+09:00  INFO 72033 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-06-04T22:26:26.293+09:00  INFO 72033 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 466 ms
2023-06-04T22:26:26.455+09:00  INFO 72033 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-06-04T22:26:26.469+09:00  INFO 72033 --- [           main] com.scvefg.mvc.MvcApplication            : Started MvcApplication in 0.869 seconds (process running for 1.08)

java -jar -Dspring.profiles.active=prod mvc-0.0.1-SNAPSHOT.war

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.0)

2023-06-04T22:26:32.918+09:00  INFO 72197 --- [           main] com.scvefg.mvc.MvcApplication            : Starting MvcApplication v0.0.1-SNAPSHOT using Java 17.0.7 with PID 72197 (/Users/j/j/app/spring/mvc/build/libs/mvc-0.0.1-SNAPSHOT.war started by j in /Users/j/j/app/spring/mvc/build/libs)
2023-06-04T22:26:32.920+09:00  INFO 72197 --- [           main] com.scvefg.mvc.MvcApplication            : The following 1 profile is active: "prod"
2023-06-04T22:26:33.354+09:00  INFO 72197 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2023-06-04T22:26:33.361+09:00  INFO 72197 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-06-04T22:26:33.361+09:00  INFO 72197 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-06-04T22:26:33.419+09:00  INFO 72197 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-06-04T22:26:33.420+09:00  INFO 72197 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 469 ms
2023-06-04T22:26:33.589+09:00  INFO 72197 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2023-06-04T22:26:33.598+09:00  INFO 72197 --- [           main] com.scvefg.mvc.MvcApplication            : Started MvcApplication in 0.887 seconds (process running for 1.085)

java -jar mvc-0.0.1-SNAPSHOT.war

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.0)

2023-06-04T22:30:26.943+09:00  INFO 76972 --- [           main] com.scvefg.mvc.MvcApplication            : Starting MvcApplication v0.0.1-SNAPSHOT using Java 17.0.7 with PID 76972 (/Users/j/j/app/spring/mvc/build/libs/mvc-0.0.1-SNAPSHOT.war started by j in /Users/j/j/app/spring/mvc/build/libs)
2023-06-04T22:30:26.945+09:00  INFO 76972 --- [           main] com.scvefg.mvc.MvcApplication            : No active profile set, falling back to 1 default profile: "local"
2023-06-04T22:30:27.426+09:00  INFO 76972 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8000 (http)
2023-06-04T22:30:27.435+09:00  INFO 76972 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-06-04T22:30:27.435+09:00  INFO 76972 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-06-04T22:30:27.507+09:00  INFO 76972 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-06-04T22:30:27.508+09:00  INFO 76972 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 527 ms
2023-06-04T22:30:27.682+09:00  INFO 76972 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8000 (http) with context path ''
2023-06-04T22:30:27.691+09:00  INFO 76972 --- [           main] com.scvefg.mvc.MvcApplication            : Started MvcApplication in 0.98 seconds (process running for 1.265)

출력 결과를 보면 환경 별로 Port가 다르게 적용된 걸 볼 수 있고, active를 따로 설정하지 않았을 때는 local profile이 실행되는 걸 보여준다.

참고로 application-{profile}.properties 또는 application-{profile}.yml 처럼 파일을 만들어 관리할 수 있다. 아마 속성들이 많아지면 자연스럽게 분리하지 않을까 싶다.




Intellij 설정

intellij의 Edit Configurations에서도 profile을 설정하여, 코드를 실행할 때 해당 profile을 적용시켜 주기 때문에 편하게 실행할 수 있다.

8080 포트가 잘 적용이 됐다.


잘못된 정보는 지적해주시면 감사하겠습니다.

0개의 댓글