Introduction to WebFlux, a new feature in Spring

Introduction to WebFlux, a new feature in Spring

1 Spring Webflux Description

Introduction to Webflux

Spring5 adds a new module for web development, similar in function to SpringMVC, and Webflux uses a current framework that compares process-responsive programming.

Use traditional web frameworks, such as SpringMVC, which are based on Servlet containers. Webflux is an asynchronous and non-blocking framework. Asynchronous and non-blocking frameworks are only supported after Servlet3.1. The core is implemented based on Reactor-related APIs

About asynchronous non-blocking instructions :

  • Asynchronous and synchronous are aimed at the caller. The caller sends a request. If the caller waits for the other party to respond before doing other things, it is synchronous. If it does not wait for the other party to respond after sending the request, it is asynchronous.
  • Blocking and non-blocking are for the callee. After the callee receives the request, it gives feedback after completing the request task. It is blocking. It is non-blocking to give feedback immediately after receiving the request and then do things.

features

  • Non-blocking: Under limited resources, improve system throughput and scalability, and implement responsive programming based on Reactor

  • Functional programming: The Spring5 framework is based on java8, and Webflux uses Java8 functional programming to implement routing requests

Compared with Spring MVC

  • Both frameworks can use annotations, and both run in containers such as Tomet
  • SpringMVC adopts imperative programming, Webflux adopts asynchronous responsive programming

2 Reactive programming

1 Overview

Reactive programming is a programming paradigm oriented towards data flow and change propagation. This means that static or dynamic data flows can be easily expressed in a programming language, and the associated computational model will automatically propagate changing values ​​through the data flow. A spreadsheet program is an example of reactive programming. Cells can contain literal values ​​or formulas like "=B1+C1", and the value of the cell containing the formula changes based on the value of other cells.

2 cases

Such as the observer mode of the scene, two important classes Observer and Observable

public class ObserverDemo extends Observable {
    
    
 public static void main(String[] args) {
    
    
 ObserverDemo observer = new ObserverDemo();
 // 添加观察者
 observer.addObserver((o,arg)->{
    
    
 System.out.println(" 发生变化 ");
 });
 observer.addObserver((o,arg)->{
    
    
 System.out.println(" 手动被观察者通知,准备改变 ");
 });
 observer.setChanged(); // 数据变化
 observer.notifyObservers(); // 通知
 }
}

Guess you like

Origin blog.csdn.net/ABestRookie/article/details/127329377