网站首页 > java教程 正文
WebFlux
Spring5,响应式编程,新增加的基于Reactive Streams的Spring WebFlux框架,使用WebFlux模块来构建:异步的、非堵塞的、事件驱动的服务,其在伸缩性方面表现非常好。
Spring Boot Webflux是基于Reactor实现的,Spring Boot 2.0包括一个新的spring-webflux模块,该模块包含对响应式HTTP和WebSocket客户端的支持,以及对REST、HTML和WebSocket交互等程序的支持,一般来说,Spring MVC用于同步处理,Spring Webflux用于异步处理。
地址:https://start.spring.io/,https://docs.spring.io/spring-framework/docs/5.2.8.RELEASE/spring-framework-reference/web-reactive.html
Spring WebFlux功能模块
1、Router Functions
对标准的@Controller,@RequestMapping等的Spring MVC注解,提供一套函数式风格的API,用于创建Router、Handler和Filter。
2、WebFlux
核心组件,协调上下游各个组件提供 响应式编程 支持。
3、Reactive Streams
一种支持背压 (Backpressure) 的异步数据流处理标准,主流实现有RxJava和Reactor,Spring WebFlux集成的是Reactor。
Flux & Mono
Mono,A Reactive Streams Publisher with basic rx operators that completes successfully by emitting an element, or with an error.
Mono是响应流 Publisher ,即要么成功发布元素,要么错误。
Flux,A Reactive Streams Publisher with rx operators that emits 0 to N elements, and then completes (successfully or with an error).
Flux是响应流 Publisher ,即要么成功发布 0 到 N 个元素,要么错误。Flux 其实是 Mono 的一个补充。
Flux和Mono属于事件发布者,类似于生产者,对消费者提供订阅接口,当有事件发生的时候,Flux或Mono会回调消费者相应的方法来通知消费者相应的事件。
Mono
1、0-1的非阻塞结果;
2、Reactive Streams JVM API Publisher;
3、非阻塞Optional;
Flux
1、0-N的非阻塞序列;
2、Reactive Streams JVM API Publisher;
3、非阻塞Stream;
Mono常用的方法有:
1、Mono.create():使用 MonoSink 来创建 Mono
2、Mono.justOrEmpty():从一个 Optional 对象或 null 对象中创建 Mono。
3、Mono.error():创建一个只包含错误消息的 Mono
4、Mono.never():创建一个不包含任何消息通知的 Mono
5、Mono.delay():在指定的延迟时间之后,创建一个 Mono,产生数字 0 作为唯一值.
快速上手
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
处理器类Handler:
package com.what21.demo.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;
@Component
public class CityHandler {
public Mono<ServerResponse> helloCity(ServerRequest request) {
return ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello, City!"));
}
}
路由器类Router:
package com.what21.demo.router;
import com.what21.demo.handler.CityHandler;
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;
@Configuration
public class CityRouter {
@Bean
public RouterFunction<ServerResponse> routeCity(CityHandler cityHandler) {
return RouterFunctions
.route(RequestPredicates.GET("/hello")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), cityHandler::helloCity);
}
}
启动类:
package com.what21.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
访问地址:
http://127.0.0.1:8080/hello
- 上一篇: 响应式编程简介之:Reactor
- 下一篇: 一文搞懂Spring响应式编程和背压机制
猜你喜欢
- 2024-12-03 Java,JDK11,发布订阅模式,响应式流(Reactive Streams)及背压
- 2024-12-03 有空就来学Hystrix RPC保护的原理,RPC监控之滑动窗口的实现原理
- 2024-12-03 开发Spring Boot应用并部署到Minikube
- 2024-12-03 SpringWeb服务应用响应式Web开发组件:响应式编程和SpringBoot
- 2024-12-03 Reactor响应式编程 第二篇 Spring Boot 整合 Reactor 简单例子
- 2024-12-03 反应式编程之Spring Web-Flux/Project Reactor
- 2024-12-03 即学即用Kotlin - 协程
- 2024-12-03 并发编程:CompletableFuture异步编程详解
- 2024-12-03 终于有人把安卓程序员必学知识点全整理出来了,有如醍醐灌顶
- 2024-12-03 Kotlin Flow的设计精髓:响应式编程在Android中的实践
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)