본문 바로가기

Spring

Spring Web MVC와 Spring WebFlux 같이 사용하기

Spring Boot는 웹 어플리케이션 개발을 위해 embedded Tomcat, Jetty, Undertow, or Netty를 지원하며 이를 이용하여 빠르게 관련 모듈을 구성하기 위해 spring-boot-starter-web와 spring-boot-starter-webflux를 지원한다.

 

각 모듈의 역할 및 기능은 이미 많은 블로그에 포스팅 되어 있기에 관련 추가 설명은 생략한다.

 

토이프로젝트를 진행하며 궁금했던 부분은 우선 spring-boot-starter-web을 사용하면서 spring-boot-starter-webflux 의webclient를 사용할 수 있을까? 였다.

 

여러 블로그를 찾아봐도 명확하게 확인되는 내용이 없어 직접 두 모듈을 적용해 보며 어떤 차이가 있는지 비교, 분석해 보았다.


우선, 간단한 프로젝트를 구성하고 spring-boot-starter-web를 포함하여 어플리케이션을 기동해 보았다.

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
}

 

2021-04-...  INFO 13508 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8020 (http)
2021-04-...  INFO 13508 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-04-...  INFO 13508 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-04-...  INFO 13508 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-04-...  INFO 13508 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 873 ms
2021-04-...  INFO 13508 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-...  INFO 13508 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8020 (http) with context path ''
2021-04-...  INFO 13508 --- [main] com.rara.toy1.OrderSvcApplication        : Started OrderSvcApplication in 2.084 seconds (JVM running for 3.11)

예상대로 tomcat이 default로 적용되어 servlet engine이 start되고 어플리케이션도 8020포트를 열고 정상 기동이 된다.

 

다음은 spring-boot-starter-webflux만 적용하여 기동해본다.


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-webflux'
}
2021-04-...  INFO 10116 --- [main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8020
2021-04-...  INFO 10116 --- [main] com.rara.toy1.OrderSvcApplication        : Started OrderSvcApplication in 3.368 seconds (JVM running for 4.419)

동일하게 예상대로 Netty가 default로 적용되어 8020포트를 열고 어플리케이션이 정상 기동된다.

(왠지 더 가볍게 기동되는거 같다)

 

그럼 마지막으로 web와 webflux를 동시에 적용하여 기동을 해본다.


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-webflux'
}
2021-04-...  INFO 13508 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8020 (http)
2021-04-...  INFO 13508 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-04-...  INFO 13508 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-04-...  INFO 13508 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-04-...  INFO 13508 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 873 ms
2021-04-...  INFO 13508 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-...  INFO 13508 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8020 (http) with context path ''
2021-04-...  INFO 13508 --- [main] com.rara.toy1.OrderSvcApplication        : Started OrderSvcApplication in 2.084 seconds (JVM running for 3.11)

사실 구동에서 오류가 날 것으로 예상했지만 깔끔하게 tomcat이 적용되어 구동이 정상 수행되었다.

 

이 이유를 아래 SpringDoc에서 찾을 수 있었다.

 

docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-webflux

 

Spring Boot Features

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

Adding both spring-boot-starter-web and spring-boot-starter-webflux modules in your application results in Spring Boot auto-configuring Spring MVC, not WebFlux. This behavior has been chosen because many Spring developers add spring-boot-starter-webflux to their Spring MVC application to use the reactive WebClient. You can still enforce your choice by setting the chosen application type to SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE).

역시나 나같은 생각을 한 개발자가 많은지 Spring MVC를 사용하며 Webclient 적용을 위해 동시 사용이 가능하고, 이럴땐 Srring MVC가 자동 구성되도록 구성되어 있었다.

'Spring' 카테고리의 다른 글

Spring MVC에 WebClient 사용해보기  (0) 2021.04.26