[수미수의 개발 브로구]

[WebFlux] Spring WebFlux 원리 본문

Language & Framework/WebFlux

[WebFlux] Spring WebFlux 원리

수미수 2023. 8. 13. 23:59
반응형

들어가기 전

  지난 글에서는 Spring WebFlux가 무엇이며, Spring MVC 와의 차이점 그리고 왜 Spring WebFlux를 써야하는지에 대해서 알아 보았습니다. Spring WebFlux 가 동작 할때 기본적으로 사용되는 서버는 Netty를 사용하고 있으며, Reative 스타일의 어플리케이션 개발을 지원하기 위해 Reactor 프로젝트를 사용하고 있습니다. 이번 글에서는 Netty 서버가 무엇인지 간략하게 알아보고, Reactive Stream Publisher 구현체인 Mono 와 Flux 에대해서 간단히 알아보도록 하겠습니다.

네티란

 Netty is an NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server. 
'Quick and easy' doesn't mean that a resulting application will suffer from a maintainability or a performance issue. Netty has been designed carefully with the experiences earned from the implementation of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.

  Netty는 NIO (Non-blocking IO) 서버 및 클라이언트를 쉽고 바르게 개발 할 수 있는 프레임워크입니다. 빠르고 쉽다는 것은 유지보수나 성능 이슈를 겪는 어플리케이션을 말하지 않습니다. 네티는 FTP, SMTP, HTTP 및 다양한 바이너리 및 텍스트 기반의 프로토콜 구현하여 얻은 경험을 바탕으로 설계 되었고, 그 결과 네티는 개발 용이성, 성능, 언정성 및 유연성을 달성 할 수있는데 성공 했습니다. 라고 네티 공식 문서를 보면 네티를 아래와 같이 정의하고 있으며, 기본 스택은 아래와 같습니다.

  즉, Netty는 비동기 이벤트 기반의 어플리케이션 프레임워크이고, 빠르게 서버 및 클라이언트를 개발하고 높은 성능 낼 수 있게 해주는 프레임워크로 보면 됩니다. (더많은 기능에 대해서는 https://netty.io/ 를 참고)

Mono ? Flux ?

  Spring WebFlux는 Reactive 스타일의 어플리케이션을 개발 할 수 있게 도와주는 프레임워크이며, Reatvice Stream 을 지원하다고 하였습니다. Spring WebFlux 는 이러한 것을 지원하기 위해서  Reactive F/W로 Reactor 와 RxJava 중 Reactor 프로젝트를 사용하여 Reative 스타일의 어플리케이션을 개발을 도와줍니다. 이러한 스타일로 개발을 하다보면 항상 마주치는 것이 Mono 와 Flux 이며 도대체 이게 무엇인지 이해가 되지 않을 겁니다.

Mono

  공식 문서를 참고 하면 Mono 는 아래와 같이 설명하고 있습니다. Reative Stream Publisher 인데, 0 ~ 1 개의 데이터를 전달한다 이렇게 이해하고 넘어 가겠습니다.

A Reactive Streams Publisher with basic rx operators that emits at most one item via the onNext signal then terminates with an onComplete signal (successful Mono, with or without value), or only emits a single onError signal (failed Mono).

 

 

  위 그림은 Mono 의 동작을 마블 다이어그램으로 나타낸 것이며, 0 또는 1개의 데이터를 처리 한다는 것을 다이어그램으로 나타낸 것입니다.

Flux

  공식 문서를 참고 하면 Flux 는 아래와 같이 설명하고 있습니다. Reative Stream Publisher  구현체 인데, 0 ~ N 개의 데이터를 전달하고 Complete 한다. (성공 또는 에러)

A Reactive Streams Publisher with rx operators that emits 0 to N elements, and then completes (successfully or with an error). The recommended way to learn about the Flux API and discover new operators is through the reference documentation, rather than through this javadoc (as opposed to learning more about individual operators). See the "which operator do I need?" appendix.

  위 그림은 Flux 의 마블다이어그램을 나타내고 있으며, 0 ~ N 개의 데이터를 방출 하며, 데이터를 전달 할때 마다 onNext 이벤트를 발생하고, 데이터 전달이 완료 되면 onComplete 이벤트 그리고 오류가 발생하면 onError 이벤트가 발생 합니다.

결론

  Spring WebFlux 는 기본적으로 Tomcat 대신 netty 서버에서 동작 하고 있으며 (Tomcat, Undertow 에서도 가능), Reactor 프로젝트 모듈을 활용하여 Reative 스타일로 개발을 할 수 있게 지원 합니다. 그리고, Reator 의 Publisher 구현체인 Mono 와 Flux 를 이용하여 신호(데이터)를 0 또는 1개, 0 또는 N 개를 전달하여, 클라이언트에 응답 할 수 있게 해줍니다. 

  실제로 Spring WebFlux 를 통해서 개발을 진행 하다 보면, List 형태는 Flux 를 통해서 핸들링하고, Object 는 Mono 를 통해서 핸들링 하고 있습니다. 물론, Flux 형태를 Mono로 변환하거나, Mono를 Flux 로 변환 할 수 있습니다. 이는 어플리케이션 비지니스로직을 어떻게 구현하는냐에 따라서 적절하게 활용 하면 됩니다.

참고

 

Netty: Home

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. Netty is an NIO client server framework which enables quick and easy development of network applications

netty.io

 

Overview (reactor-core 3.5.9)

Reactor Core is a succinct and powerful foundational library for building reactive and efficient applications on the JVM. More detailed documentation is available on the reference guide.

projectreactor.io

 

반응형