浅谈 Spring boot2.0 中 响应式 Spring WebFlux 和 WebClient

 Spring WebFlux是随Spring 5推出的响应式Web框架。Spring webFlux在Spring Boot框架中的位置和功能,如下图。

浅谈 Spring boot2.0 中 响应式 Spring WebFlux 和 WebClient

从这个图就可以看出对支持Spring 5的Spring Boot 2.0来说,新加入的响应式技术栈是其主打核心特性。

具体来说,Spring Boot 2支持的响应式技术栈包括如下:

  • Spring Framework 5提供的非阻塞web框架Spring Webflux;
  • 遵循响应式流规范的兄弟项目Reactor;
  • 支持异步I/O的Netty、Undertow等框架,以及基于Servlet 3.1+的容器(如Tomcat 8.0.23+和Jetty 9.0.4+);
  • 支持响应式的数据访问Spring Data Reactive Repositories;
  • 支持响应式的安全访问控制Spring Security Reactive;
  • 等。

spring-boot-starter-webflux 依赖,是我们核心需要学习 webflux 的包,

里面默认包含了 spring-boot-starter-reactor-netty 、spring 5 webflux 包。也就是说默认是通过 netty 启动的。

如果不想用netty作为启动,想用servlet3.1 作为容器,则可以用下面的pom例子

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-reactor-netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
            <version>5.1.3.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-reactor-netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-reactor-netty</artifactId>
        </dependency>

官方给的一个如何build rest服务的例子为:

http://spring.io/guides/gs/reactive-rest-service/

更多介绍webclient, webflux概念和编程的内容,见如下的连接,就不搬砖了。

【推荐】https://blog.csdn.net/jeffli1993/article/details/79941175

       http://blog.51cto.com/liukang/2090163