SpringBoot之响应式编程
一 Spring WebFlux Framework说明
Spring WebFlux 是 Spring Framework 5.0 中引入的新 reactive web framework。与 Spring MVC 不同,它不需要 Servlet API,完全异步和 non-blocking,并通过反应堆项目实现Reactive Streams规范。
Spring WebFlux 有两种版本:功能和 annotation-based。 annotation-based 一个非常接近 Spring MVC model,如下面的示例所示:
@RestController
@RequestMapping("/users")
public class MyRestController {
@GetMapping("/{user}")
public Mono<User> getUser(@PathVariable Long user) {
// ...
}
@GetMapping("/{user}/customers")
public Flux<Customer> getUserCustomers(@PathVariable Long user) {
// ...
}
@DeleteMapping("/{user}")
public Mono<User> deleteUser(@PathVariable Long user) {
// ...
}
}
函数变量“WebFlux.fn”将路由配置与请求的实际处理分开,如下面的示例所示:
@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 是 Spring Framework 的一部分,其详细信息可在reference 文档中找到。
您可以根据需要定义尽可能多的
RouterFunctionbeans 来模块化 router 的定义。如果需要应用优先级,可以订购 Beans。
要开始,请将spring-boot-starter-webflux模块添加到 application。
在 application 中添加
spring-boot-starter-web和spring-boot-starter-webflux模块会导致 Spring Boot auto-configuring Spring MVC,而不是 WebFlux。选择此行为是因为许多 Spring 开发人员将spring-boot-starter-webflux添加到他们的 Spring MVC application 以使用 reactiveWebClient。您仍然可以通过将所选的 application 类型设置为SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)来强制执行您的选择。
Spring WebFlux Auto-configuration
Spring Boot 为 Spring WebFlux 提供 auto-configuration,适用于大多数 applications。
auto-configuration 在 Spring 的默认值之上添加以下 features:
如果你想保留 Spring Boot WebFlux features 并且想要添加额外的WebFlux configuration,你可以添加自己的@Configuration class 类型为WebFluxConfigurer但而不是 @EnableWebFlux。
如果要完全控制 Spring WebFlux,可以添加自己的@Configuration注释@EnableWebFlux。
带有 HttpMessageReaders 和 HttpMessageWriters 的 HTTP 编解码器
Spring WebFlux 使用HttpMessageReader和HttpMessageWriter接口来转换 HTTP 请求和响应。通过查看 classpath 中可用的 libraries,它们配置为CodecConfigurer以具有合理的默认值。
Spring Boot 通过使用CodecCustomizer实例进一步自定义。例如,spring.jackson.* configuration 键应用于 Jackson 编解码器。
如果需要添加或自定义编解码器,可以创建自定义CodecCustomizer component,如下面的示例所示:
import org.springframework.boot.web.codec.CodecCustomizer;
@Configuration
public class MyConfiguration {
@Bean
public CodecCustomizer myCodecCustomizer() {
return codecConfigurer -> {
// ...
}
}
}
你也可以利用Boot 的自定义 JSON 序列化器和反序列化器。
静态内容
默认情况下,Spring Boot 为 classpath 中名为/static(或/public或/resources或/META-INF/resources)的目录提供静态内容。它使用来自 Spring WebFlux 的ResourceWebHandler,以便您可以通过添加自己的WebFluxConfigurer并覆盖addResourceHandlers方法来修改该行为。
默认情况下,资源映射到/**,但您可以通过设置spring.webflux.static-path-pattern property 来调整它。例如,将所有资源重新定位到/resources/**可以实现如下:
spring.webflux.static-path-pattern=/resources/**
您还可以使用spring.resources.static-locations自定义静态资源位置。这样做会将默认值替换为目录位置列表。如果这样做,默认的欢迎页面检测将切换到您的自定义位置。因此,如果您在启动时的任何位置都有index.html,那么它就是 application 的主页。
除了前面列出的“标准”静态资源位置之外,还为Webjars 内容做了一个特例。如果 jar files 包含在 Webjars 格式中,则中包含路径的所有资源都将从 jar files 提供。
Spring WebFlux applications 并不严格依赖于 Servlet API,因此不能将它们部署为 war files 并且不要使用
src/main/webapp目录。
模板引擎
与 REST web services 一样,您也可以使用 Spring WebFlux 来提供动态 HTML 内容。 Spring WebFlux 支持各种模板技术,包括 Thymeleaf,FreeMarker 和 Mustache。
Spring Boot 包括对以下模板引擎的 auto-configuration 支持:
当您使用其中一个模板引擎和默认的 configuration 时,您的模板将从src/main/resources/templates自动获取。
错误处理
Spring Boot 提供WebExceptionHandler,以合理的方式处理所有错误。它在处理 order 中的位置紧接在 WebFlux 提供的处理程序之前,这被认为是最后的。对于机器客户端,它会生成一个 JSON 响应,其中包含错误,HTTP 状态和 exception 消息的详细信息。对于浏览器客户端,有一个“whitelabel”错误处理程序,它以 HTML 格式呈现相同的数据。您还可以提供自己的 HTML 模板来显示错误(请参阅下一节)。
自定义此 feature 的第一个步骤通常涉及使用现有机制,但替换或扩充错误内容。为此,您可以添加ErrorAttributes类型的 bean。
要更改错误处理行为,可以实现ErrorWebExceptionHandler并注册该类型的 bean 定义。因为WebExceptionHandler非常 low-level,所以 Spring Boot 还提供了一个方便的AbstractErrorWebExceptionHandler来让你以 WebFlux 的方式处理错误,如下面的例子所示:
public class CustomErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {
// Define constructor here
@Override
protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
return RouterFunctions
.route(aPredicate, aHandler)
.andRoute(anotherPredicate, anotherHandler);
}
}
要获得更完整的图片,您还可以直接子类化DefaultErrorWebExceptionHandler并覆盖特定方法。
自定义错误页面
如果要为给定状态 code 显示自定义 HTML 错误页面,可以将文件添加到/error文件夹。错误页面可以是静态 HTML(即,添加到任何静态资源文件夹下)或使用模板构建。文件的 name 应该是确切的状态 code 或系列掩码。
例如,要 map 404到静态 HTML 文件,您的文件夹结构如下:
src/
+- main/
+- java/
| + <source code>
+- resources/
+- public/
+- error/
| +- 404.html
+- <other public assets>
要使用 Mustache 模板 map 所有5xx错误,您的文件夹结构如下:
src/
+- main/
+- java/
| + <source code>
+- resources/
+- templates/
+- error/
| +- 5xx.mustache
+- <other templates>
Web 过滤器
Spring WebFlux 提供了一个WebFilter接口,可以实现过滤 HTTP request-response 交换。在 application context 中找到的WebFilter beans 将自动用于过滤每个交换。
如果过滤器的 order 很重要,则可以实现Ordered或使用@Order进行注释。 Spring Boot auto-configuration 可以为您配置 web 过滤器。执行此操作时,将使用以下 table 中显示的订单:
| Web 过滤器 | 订购 |
|---|---|
MetricsWebFilter |
Ordered.HIGHEST_PRECEDENCE + 1 |
WebFilterChainProxy(Spring Security) |
-100 |
HttpTraceWebFilter |
Ordered.LOWEST_PRECEDENCE - 10 |
二 WebClient
Spring Boot 将 auto-detect 用于驱动WebClient,具体取决于 application classpath 上可用的 libraries。目前,支持 Reactor Netty 和 Jetty RS client。
spring-boot-starter-webflux starter 默认依赖于io.projectreactor.netty:reactor-netty,它带来了 server 和 client implementations。如果您选择使用 Jetty 作为 reactive 服务器,则应该在 Jetty Reactive HTTP client library,org.eclipse.jetty:jetty-reactive-httpclient上添加依赖项。对服务器和 client 使用相同的技术具有优势,因为它将自动在 client 和服务器之间共享 HTTP 资源。
开发人员可以通过提供自定义ReactorResourceFactory或JettyResourceFactory bean 来覆盖 Jetty 和 Reactor Netty 的资源 configuration - 这将应用于 clients 和服务器。
如果您希望覆盖 client 的该选项,您可以定义自己的ClientHttpConnector bean 并完全控制 client configuration。
您可以了解有关Spring Framework reference 文档中的 WebClient configuration 选项的更多信息。
WebClient 自定义
WebClient自定义有三种主要方法,具体取决于您希望自定义应用的广泛程度。
要使任何自定义的范围尽可能窄,请 inject auto-configured WebClient.Builder然后根据需要调用其方法。 WebClient.Builder实例是有状态的:构建器上的任何更改都会反映在随后使用它创建的所有 client 中。如果要使用相同的构建器创建多个 client,还可以考虑使用WebClient.Builder other = builder.clone();克隆构建器。
要对所有WebClient.Builder实例进行 application-wide 添加自定义,可以声明WebClientCustomizer beans 并在注入点本地更改WebClient.Builder。
最后,您可以回退到原始 API 并使用WebClient.create()。在这种情况下,不应用 auto-configuration 或WebClientCustomizer
三 代码演示
pom.xml
<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>注:webflux不能和web共存,webflux启动的是netty。
实体类
public class User {
private String id; private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String id, String name) {
super();
this.id = id;
this.name = name;
} }Service层
@Service
public class UserService {
private static final Map<String, User> dataMap = new HashMap<>(); static{
dataMap.put("1", new User("1", "小X老师"));
dataMap.put("2", new User("2", "小D老师"));
dataMap.put("3", new User("3", "小C老师"));
dataMap.put("4", new User("4", "小L老师"));
dataMap.put("5", new User("5", "小A老师"));
dataMap.put("6", new User("6", "小S老师"));
dataMap.put("7", new User("7", "小S老师"));
} /**
* 功能描述:返回用户列表
* @return
*/
public Flux<User> list(){
Collection<User> list = UserService.dataMap.values(); return Flux.fromIterable(list);
} /**
* 功能描述:根据id查找用户
* @param id
* @return
*/
public Mono<User> getById(final String id){
return Mono.justOrEmpty(UserService.dataMap.get(id));
} /**
* 功能描述:根据id删除用户
* @param id
* @return
*/
public Mono<User> del(final String id){
return Mono.justOrEmpty(UserService.dataMap.remove(id));
} }
web层
@RestController
@RequestMapping("/api/v1/user")
public class UserController {
//@Autowired
//private UserService userService; private final UserService userService; public UserController(final UserService userService) {
this.userService = userService;
} @GetMapping("/test")
public Mono<String> test(){
return Mono.just("hello");
}
/**
* 功能描述:根据id找用户
* @param id
* @return
*/
@GetMapping("find")
public Mono<User> findByid(final String id){
return userService.getById(id);
} /**
* 功能描述:删除用户
* @param id
* @return
*/
@GetMapping("del")
public Mono<User> del(final String id){
return userService.del(id);
} /**
* 功能描述:列表
* @return
*/
@GetMapping(value="list",produces=MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<User> list(){
return userService.list().delayElements(Duration.ofSeconds(2));
} }
测试类
//@RunWith(SpringRunner.class)
//@SpringBootTest
public class WebfluxApplicationTests {
@Test
public void testBase(){ Mono<String> bodyMono = WebClient.create().get()
.uri("http://localhost:8080/api/v1/user/find?id=1")
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(String.class); System.out.println(bodyMono.block()); }
@Test
public void testBase2(){ Mono<String> bodyMono = WebClient.create().get()
.uri("http://localhost:8080/api/v1/user/find?id={id}",2)
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(String.class); System.out.println(bodyMono.block()); }
}
注:着重讲一下这个list方法,这个方法里面延迟2秒,会体现出是流一样,每隔2秒出现一条数据。 {"id":"1","name":"小X老师"}
{"id":"2","name":"小D老师"}
{"id":"3","name":"小C老师"}
{"id":"4","name":"小L老师"}
{"id":"5","name":"小A老师"}
{"id":"6","name":"小S老师"}
{"id":"7","name":"小S老师"}
SpringBoot之响应式编程的更多相关文章
- SpringBoot使用WebFlux响应式编程操作数据库
这一篇文章介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库. 前言 在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring ...
- 【SpringBoot】SpringBoot2.0响应式编程
========================15.高级篇幅之SpringBoot2.0响应式编程 ================================ 1.SprinBoot2.x响应 ...
- SpringBoot 2.x (14):WebFlux响应式编程
响应式编程生活案例: 传统形式: 一群人去餐厅吃饭,顾客1找服务员点餐,服务员把订单交给后台厨师,然后服务员等待, 当后台厨师做好饭,交给服务员,经过服务员再交给顾客1,依此类推,该服务员再招待顾客2 ...
- springboot(二十三)Springboot2.X响应式编程
序言 Spring WebFlux是Spring Framework 5.0中引入的新的反应式Web框架与Spring MVC不同,它不需要Servlet API,完全异步和非阻塞,并 通过React ...
- SpringBoot实战派读书笔记---响应式编程
1.什么是WebFlux? WebFlux不需要Servlet API,在完全异步且无阻塞,并通过Reactor项目实现了Reactor Streams规范. WebFlux可以在资源有限的情况下提高 ...
- (转)Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门
http://www.ityouknow.com/springboot/2019/02/12/spring-boot-webflux.html Spring 5.0 中发布了重量级组件 Webflux ...
- spring5响应式编程
1.Spring5新特性 2.响应式编程响应式编程:非阻塞应用程序,借助异步和事件驱动还有少量的线程垂直伸缩,而非横向伸缩(分布式集群)当Http连接缓慢的时候,从数据库到Http数据响应中也会 ...
- Spring Boot (十四): 响应式编程以及 Spring Boot Webflux 快速入门
1. 什么是响应式编程 在计算机中,响应式编程或反应式编程(英语:Reactive programming)是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流 ...
- 07-Spring5 WebFlux响应式编程
SpringWebFlux介绍 简介 SpringWebFlux是Spring5添加的新模块,用于Web开发,功能和SpringMvc类似的,WebFlux使用当前一种比较流行的响应式编程框架 使用传 ...
随机推荐
- Servlet与Tomcat运行示例
Servlet与Tomcat运行示例 本文将写一个servlet,然后将其部署到Tomcat的全过程.本文参考<深入拆解Tomcat_Jetty>内容. 一.基于web.xml开发步骤 下 ...
- 分布式session共享机制分析
使用配置: 1.在pom文件中引入spring-session的jar包 <!--springsession--><dependency><groupId>org. ...
- APP设计与开发(ui篇)
这篇文章是我个人在开发与设计APP的所用的一些设计思路,仅用于和大家分享知识并不是什么设计标准之类的.主要说明App的开发中是如何来开发与组织UI部分. UI模块结构 在项目中建立ui包用于存放ui类 ...
- 阿里云服务器ecs配置之安装jdk
一.安装环境 操作系统:Centos 7.4 JDK版本:1.8 工具:Xshell5.Xftp5 二.安装步骤 第一步:下载安装包 (官网)链接: 下载适合自己系统的jdk版本,如图:我下载的是64 ...
- layui select获取自定义属性值
layui-select写法: <option value='> 我想在点击的时候获取自定义属性data-method的值,其中selectId是该select的id form.on('s ...
- 2017春季_京东_Java后端研发岗面经
纸上得来终觉浅,绝知此事要躬行 ——2017春季Java后端研发工程师面试心得 收获offer:上海汉得+北京中科软+成都百词斩+成都诺基亚研发中心+清华大学计算机研究所等offer.阿里一面猝.京 ...
- 基于Python协同过滤算法的认识
Contents 1. 协同过滤的简介 2. 协同过滤的核心 3. 协同过滤的实现 4. 协同过滤的应用 1. 协同过滤的简介 关于协同过滤的一个最经典的例子就是看电影,有时候 ...
- How to setup Electrum testnet mode and get BTC test coins
For some reason we need to use BTC test coins, but how to set up the Bitcoin testnet wallet and get ...
- 离线服务器安装zabbix
因为机房内的服务器并不是所有都能上外网,所以利用zabbix官方源的安装方法就行不通了,又嫌弃编译安装麻烦,所以这里选择离线RPM包安装zabbix.(如需完整rpm包可以留言与我联系) 下载zabb ...
- C++——指针
目录 一.地址和指针 1.1内存 1.2针和指针变量 二.指针变量 2.1始化 2.2赋值 2.3指针类型算术运算 2.4指针类型关系运算 2.5指向指针的指针 三.指针与数组 3.1指针运算 3.2 ...