天天看點

Spring BOOT ( 基于Kotlin 程式設計語言) 使用 Spring WebFlux 實作響應式程式設計The Spring WebFlux Framework

Spring BOOT ( 基于Kotlin 程式設計語言) 使用 Spring WebFlux 實作響應式程式設計

image.png

參考文檔:

https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#spring-webflux

The original web framework included in the Spring Framework, Spring Web MVC, was purpose built for the Servlet API and Servlet containers. The reactive stack, web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports Reactive Streams

back pressure, and runs on servers such as Netty, Undertow, and Servlet 3.1+ containers.

Both web frameworks mirror the names of their source modules

spring-webmvc and spring-webflux and co-exist side by side in the Spring Framework. Each module is optional. Applications may use one or the other module, or in some cases both — e.g. Spring MVC controllers with the reactive

WebClient

.

The Spring WebFlux Framework

Spring WebFlux is the new reactive web framework introduced in Spring Framework 5.0. Unlike Spring MVC, it does not require the Servlet API, is fully asynchronous and non-blocking, and implements the Reactive Streams specification through the Reactor project.

Spring WebFlux comes in two flavors: functional and annotation-based. The annotation-based one is quite close to the Spring MVC model we know, as shown in the following example:

@RestController
@RequestMapping("/users")
public class MyRestController {

    @GetMapping("/{user}")
    public Mono<User> getUser(@PathVariable Long user) {
        // ...
    }

    @GetMapping("/{user}/customers")
    Flux<Customer> getUserCustomers(@PathVariable Long user) {
        // ...
    }

    @DeleteMapping("/{user}")
    public Mono<User> deleteUser(@PathVariable Long user) {
        // ...
    }

}
           

‘WebFlux.fn’, the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example:

@Configuration
public class RoutingConfiguration {

    @Bean
    public RouterFunction<ServerResponse> monoRouterFunction(UserHandler userHandler) {
        return route(GET("/{user}").and(accept(APPLICATION_JSON)), userHandler::getUser)
                .andRoute(GET("/{user}/customers").and(accept(APPLICATION_JSON)), userHandler::getUserCustomers)
                .andRoute(DELETE("/{user}").and(accept(APPLICATION_JSON)), userHandler::deleteUser);
    }

}

@Component
public class UserHandler {

    public Mono<ServerResponse> getUser(ServerRequest request) {
        // ...
    }

    public Mono<ServerResponse> getUserCustomers(ServerRequest request) {
        // ...
    }

    public Mono<ServerResponse> deleteUser(ServerRequest request) {
        // ...
    }
}
           

WebFlux is part of the Spring Framework. and detailed information is available in its reference documentation.

To get started, add the spring-boot-starter-webflux module to your application.

[Note]

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).

建立一個簡單的 UserRepository 和 User DTO 類用來從清單中擷取使用者資料。這隻是一個假的 Bean,在實際過程中你可以從包括關系資料庫、MongoDB 或者是 RestClient 擷取資料。

不過需要注意的是,今天我們所用的這些 JDBC 驅動并不是自然支援 Reactive 風格程式設計的。所有任何對資料庫的調用都将導緻線程的堵塞。而 MongoDB 提供一個 Reactive 的用戶端驅動程式。
https://coyee.com/article/12086-spring-5-reactive-web