ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Reactive Programming and WebFlux
    Framework/SPRING 2020. 7. 5. 18:21

    1. Overview

    Reactive programming is about non-blocking, event-driven applications that scale with a small number of threads, with backpressure being a key ingredient that aims to ensure producers don't overwhelm consumers.

    The primary benefits of reactive programming are:

    • increased utilization of computing resources on multicore and multi-CPU hardware
    • and increased performance by reducing serialization

    Reactive programming is generally event-driven, in contrast to reactive systems, which are message-driven. Thus, using reactive programming does not mean we're building a reactive system, which is an architectural style.

    However, reactive programming may be used as a means to implement reactive systems if we follow the Reactive Manifesto, which is quite vital to understand.

    Based on this, reactive systems have four important characteristics:

    • Responsive: the system should respond in a timely manner
    • Resilient: in case the system faces any failure, it should stay responsive
    • Elastic: reactive systems can react to changes and stay responsive under varying workload
    • Message-driven: reactive systems need to establish a boundary between components by relying on asynchronous message passing

    2. Webflux

    Spring WebFlux is Spring's reactive-stack web framework, and it's an alternative to Spring MVC. In order to achieve this reactive model and be highly scalable, the entire stack is non-blocking.

    2.1 What Are the Mono and Flux Types?

    The WebFlux framework in Spring Framework 5 uses Reactor as its async foundation.

    This project provides two core types: Mono to represent a single async value, and Flux to represent a stream of async values. They both implement the Publisher interface defined in the Reactive Streams specification.

    Mono implements Publisher and returns 0 or 1 elements:

    public abstract class Mono<T> implements Publisher<T> {...}

    Also, Flux implements Publisher and returns N elements:

    public abstract class Flux<T> implements Publisher<T> {...}

    By definition, the two types represent streams, hence they're both lazy, which means nothing is executed until we consume the stream using the subscribe() method. Both types are immutable, therefore calling any method will return a new instance of Flux or Mono.

    2.2 Can We Use Both Web Mvc and Webflux in the Same Application?

    As of now, Spring Boot will only allow either Spring MVC or Spring WebFlux, as Spring Boot tries to auto-configure the context depending on the dependencies that exist in its classpath.

    Also, Spring MVC cannot run on Netty. Moreover, MVC is a blocking paradigm and WebFlux is a non-blocking style, therefore we shouldn't be mixing both together, as they serve different purposes.

    3. What Is the Use of Webclient and Webtestclient?

     

    On the other hand, the WebTestClient is a similar class that we can use in tests. Basically, it's a thin shell around the WebClient. It can connect to any server over an HTTP connection. It can also bind directly to WebFlux applications using mock request and response objects, without the need for an HTTP server.

    4. What Are the Disadvantages of Using Reactive Streams?

     

    • Troubleshooting a Reactive application is a bit difficult; be sure to check out our tutorial on debugging reactive streams for some handy debugging tips
    • There is limited support for reactive data stores, as traditional relational data stores have yet to embrace the reactive paradigm
    • There's an extra learning curve when implementing

    5. Is Spring 5 Compatible With Older Versions of Java?

    In order to take advantage of Java 8 features, the Spring codebase has been revamped. This means older versions of Java cannot be used. Hence, the framework requires a minimum of Java 8.

    6. How Does Spring 5 Integrate With Jdk 9 Modularity

    In Spring 5, everything has been modularized, thus we won't be forced to import jars that may not have the functionalities we're looking for.

    Please have a look at our guide to Java 9 modularity for an in-depth understanding of how this technology works.

    Let's see an example to understand the new module functionality in Java 9 and how to organize a Spring 5 project based on this concept.

    To start, let's create a new class that contains a single method to return a String “HelloWorld”. We'll place this within a new Java project – HelloWorldModule:

    package com.hello;
    
    public class HelloWorld {
    
        public String sayHello(){
    
            return "HelloWorld";
    
        }
    
    }

    Then let's create a new module:

    module com.hello {
    
        export com.hello;
    
    }

    Now, let's create a new Java Project, HelloWorldClient, to consume the above module by defining a module:

    module com.hello.client {
    
        requires com.hello;
    
    }

    The above module will be available for testing now:

    public class HelloWorldClient {
    
        public static void main(String[] args){
    
            HelloWorld helloWorld = new HelloWorld();
    
            log.info(helloWorld.sayHello());
    
        }
    
    }

    7. Spring MVC and Spring WebFlux

    Under JEP-266, Reactive Streams is now part of the Java 9 JDK.

    Now, in Spring MVC and Spring WebFLux, the Reactive stuff is going to be under the right-hand side of the Spring-Webflux. And what has happened inside the Spring Framework is the Servlet Container, the Servlet API, is not reactive, it is blocking. So, Spring Framework guys created a whole new stack, a whole new web framework, with the inside of Spring called Spring-Webflux.

    Spring MVC and Spring WebFlux

    7.1 @Controller/@RequestMapping Vs Router functions

    Now, top of these, we're still gonna be using the @Controller and @RequestMapping annotations or a whole new functional API called Router Functions. You can use either one of these with Spring-MVC or Spring-Webflux. So, this is all handled by the framework. You can use either stack whether you wanna be Reactive or use traditional Servlet and there's really nothing wrong with traditional Servlet. It still works, I mean, it's still a very robust programming API that we have to deal with the web environment. So, it's still inside of Spring Framework. It's not mandatory to use, it's not bad to use but for the Reactive functionality when you are in these high volume streaming environments, we do have this whole new Reactive Framework with Spring WebFlux and we'll be looking at that a lot closer.

    7.2 Spring Reactive Types

    Two new reactive types are introduced with Spring Framework 5.

    • Mono is a publisher with zero or one element in the data stream.
    • Flux is a publisher with zero or MANY elements in the data stream.

    Both types implement the Reactive Streams Publisher interface.

    8. Reference

    https://dzone.com/articles/reactive-programming-with-spring-webflux

    'Framework > SPRING' 카테고리의 다른 글

    Component Scanning  (0) 2020.05.23
    Comparison between Field, Setter, and Constructor Injection  (0) 2020.05.23
    Bean Definition Overriding  (0) 2020.05.23
    Application Context  (0) 2020.04.10
    Portable Service Abstraction (PSA)  (0) 2020.03.06

    댓글

Designed by Tistory.