spring 5

Spring 5 是流行的 Spring 框架的下一个重大的版本升级。Spring 5 中最重要改动是把反应式编程的思想应用到了框架的各个方面,Spring 5 的反应式编程以 Reactor 库为基础。

spring boot 2之webflux实识别_.net

WebFlux

WebFlux 模块的名称是 spring-webflux,名称中的 Flux 来源于 Reactor 中的类 Flux。该模块中包含了对反应式 HTTP、服务器推送事件和 WebSocket 的客户端和服务器端的支持。对于开发人员来说,比较重要的是服务器端的开发,

在服务器端,WebFlux 支持两种不同的编程模型:第一种是 Spring MVC 中使用的基于 Java 注解的方式;第二种是基于 Java 8 的 lambda 表达式的函数式编程模型。这两种编程模型只是在代码编写方式上存在不同。它们运行在同样的反应式底层架构之上,因此在运行时是相同的。WebFlux 需要底层提供运行时的支持,WebFlux 可以运行在支持 Servlet 3.1 非阻塞 IO API 的 Servlet 容器上,或是其他异步运行时环境,如 Netty 和 Undertow。

示例

新建工程

spring boot 2之webflux实识别_spring_02

加入webflux依赖:

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

新建类BasicController,加入如下代码:

/**
* @author zhangfeng
*
*/@RestControllerpublic class BasicController { @GetMapping("/hello_world")
public Mono<String> sayHelloWorld() { return Mono.just("Hello World");
}
}

BasicController 是 REST API 的控制器,通过@RestController 注解来声明。在 BasicController 中声明了一个 URI 为/hello_world 的映射。其对应的方法 sayHelloWorld()的返回值是 Mono<String>类型,其中包含的字符串"Hello World"会作为 HTTP 的响应内容。

测试

启动应用:

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)2018-04-16 17:32:36.993 INFO 10492 --- [ main] com.cloud.skyme.Websocket1Application : Starting Websocket1Application on DESKTOP-E3I9LR5 with PID 10492 (C:\java\workspace\weixin\websocket-1\target\classes started by zhangfeng in C:\java\workspace\weixin\websocket-1)2018-04-16 17:32:37.006 INFO 10492 --- [ main] com.cloud.skyme.Websocket1Application : No active profile set, falling back to default profiles: default2018-04-16 17:32:37.089 INFO 10492 --- [ main] onfigReactiveWebServerApplicationContext : Refreshing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@7a52f2a2: startup date [Mon Apr 16 17:32:37 CST 2018]; root of context hierarchy2018-04-16 17:32:38.609 INFO 10492 --- [ main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "{[/hello_world],methods=[GET]}" onto public reactor.core.publisher.Mono<java.lang.String> com.cloud.skyme.BasicController.sayHelloWorld()2018-04-16 17:32:38.704 INFO 10492 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]2018-04-16 17:32:38.704 INFO 10492 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]2018-04-16 17:32:38.885 INFO 10492 --- [ main] o.s.w.r.r.m.a.ControllerMethodResolver : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@7a52f2a2: startup date [Mon Apr 16 17:32:37 CST 2018]; root of context hierarchy2018-04-16 17:32:39.382 INFO 10492 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-04-16 17:32:40.184 INFO 10492 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:80802018-04-16 17:32:40.184 INFO 10492 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 80802018-04-16 17:32:40.191 INFO 10492 --- [ main] com.cloud.skyme.Websocket1Application : Started Websocket1Application in 3.672 seconds (JVM running for 4.772)2018-04-16 17:32:54.559 WARN 10492 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/helloworld]: Response status 4042018-04-16 17:32:54.693 WARN 10492 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/favicon.ico]: Response status 404

打开浏览器输入 http://localhost:8080/hello_world 可以看到返回结果。

总结

使用 WebFlux 与 Spring MVC 的不同在于,WebFlux 所使用的类型是与反应式编程相关的 Flux 和 Mono 等,而不是简单的对象。对于简单的 Hello World 示例来说,这两者之间并没有什么太大的差别。对于复杂的应用来说,反应式编程和负压的优势会体现出来,可以带来整体的性能的提升。