SpringBoot 2.0 响应式编程

SpringBoot 2.0 已经发布多时,一直不知道它有什么用,只是知道它有个webflux。今天就来学习一下,看一下是否有必要升级到新版本?

1 2.0与1.0版本的区别?

官网图

  • 咱们能够看出来,增长了些新的特性,主要是对响应式编程的支持,底层多了Netty,这样就能够进行非阻塞io的编程,这也是响应式编程的基础。
  • 还有就是2.0对应的java版本必须最低java8,支持java9.若是大家公司使用的仍是1.6,1.7那就不适合升级版本,可能会带来一堆麻烦。
  • 使用webflux并不会提升应用的响应速度,官网也明确指出了。因此不要跟风去使用2.0的版本。
  • 使用webflux能够在有限的资源下提升系统的吞吐量和伸缩性。

2 搭建简单的webflux项目

若是你是使用得STS来建立项目的话将会很简单,直接选择web flux模块就好。SpringBoot选择最新稳定的2.1.4.RELEASE。前端

完整pom文件:java

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mike</groupId>
    <artifactId>flux</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mike-flux</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

建立启动类:(使用STS会自动建立)react

package com.mike;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MikeFluxApplication {

    public static void main(String[] args) {
        SpringApplication.run(MikeFluxApplication.class, args);
    }

}

3 编写路由和处理类

有过前端工做经验的小伙伴,对路由确定不陌生,Vue react中都有统一的路有管理。如今SpringBoot也能够这样来写了。后端的小伙伴能够把它理解为你以前写的controller。
先定义一个处理类:web

package com.mike.handler;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import reactor.core.publisher.Mono;

/**
 * The class HelloHandler
 * 
 */
@Component
public class HelloHandler {
    
    public Mono<ServerResponse> sayHello(ServerRequest req) {
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8)
                .body(BodyInserters.fromObject(req.queryParam("name").get()));
    }
}

Mono定义返回单个结果,定义了响应数据类型以及数据。我是从请求参数中取得name参数进行返回。spring

有了处理类,咱们就须要定义什么样的路由交给它来处理,要将路有何处理程序进行mapping:apache

package com.mike.router;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.mike.handler.HelloHandler;

/**
 * The class HelloRouter
 * 
 */
@Configuration
public class HelloRouter {
    
    @Bean
    public RouterFunction<ServerResponse> hello(HelloHandler handle){
        return RouterFunctions.route(RequestPredicates.GET("/hello")
                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON_UTF8))
                ,handle::sayHello);
    }
}

咱们定义了/hello的请求交给咱们的处理类去处理,这样一次完整的请求就搞定了。启动程序,访问http://localhost:8080/hello?name=mike 就能够看到页面上的结果了。编程

4 总结

  • 和1.0的版本相比,咱们编写的程序返回结果将由MonoFlux来包裹。
  • 你能够选择使用路由的方式来编写代码,而不用写controller
  • 异步响应,有时会带来想不到的麻烦,断点调试再也不有用。
  • 使用webflux 须要根据你本身的项目实际状况来抉择。若是你的项目中没有调用别人的api,就不必使用webflux,用处不大,就像我上面搭建的项目。 若是须要请求别人的api,引入webflux,将会对超时,容错,失败重试有很好的改进,使得你的项目更完善,用户体验更好。

关于webflux若是你有更深的理解,但愿能够回复一块儿交流。后端

若是想要学习更多知识,能够关注下个人公众号:
mike啥都想搞api

相关文章
相关标签/搜索