前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring BOOT ( 基于Kotlin 编程语言) 使用 Spring WebFlux 实现响应式编程The Spring WebFlux Framework

Spring BOOT ( 基于Kotlin 编程语言) 使用 Spring WebFlux 实现响应式编程The Spring WebFlux Framework

作者头像
一个会写诗的程序员
发布2018-08-17 11:28:48
8540
发布2018-08-17 11:28:48
举报
文章被收录于专栏:一个会写诗的程序员的博客

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-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:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.12.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • The Spring WebFlux Framework
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档