Spring BOOT ( 基于Kotlin 編程語言) 使用 Spring WebFlux 實(shí)現(xiàn)響應(yīng)式編程

Spring BOOT ( 基于Kotlin 編程語言) 使用 Spring WebFlux 實(shí)現(xiàn)響應(yīng)式編程

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

創(chuàng)建一個(gè)簡單的 UserRepository 和 User DTO 類用來從列表中獲取用戶數(shù)據(jù)。這只是一個(gè)假的 Bean,在實(shí)際過程中你可以從包括關(guān)系數(shù)據(jù)庫、MongoDB 或者是 RestClient 獲取數(shù)據(jù)。

不過需要注意的是,今天我們所用的這些 JDBC 驅(qū)動(dòng)并不是自然支持 Reactive 風(fēng)格編程的。所有任何對數(shù)據(jù)庫的調(diào)用都將導(dǎo)致線程的堵塞。而 MongoDB 提供一個(gè) Reactive 的客戶端驅(qū)動(dòng)程序。

https://coyee.com/article/12086-spring-5-reactive-web

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,256評論 6 342
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,817評論 0 10
  • 2018年1月17日 星期三 小雪 三年三班 楊蕊 新收獲...
    幸福洋寶媽香姐閱讀 225評論 0 0
  • 文/阿不思 事實(shí)上,我身邊的大部分女人都是因?yàn)楫?dāng)初的那一點(diǎn)甜抵擋住后來很多的苦和酸。 當(dāng)時(shí)她沒有看上他。倒也不是因...
    阿不思ss閱讀 1,524評論 0 4

友情鏈接更多精彩內(nèi)容