响应式编程生活案例:

传统形式:

一群人去餐厅吃饭,顾客1找服务员点餐,服务员把订单交给后台厨师,然后服务员等待,

当后台厨师做好饭,交给服务员,经过服务员再交给顾客1,依此类推,该服务员再招待顾客2。

服务员可以理解为服务器,服务器越多,可处理的顾客请求越多

响应式编程:

服务员记住到顾客1的要求,交给后台厨师,再记住顾客2的要求,交给后台厨师,依此类推

当厨师做好顾客1的饭,告知服务员,然后服务员把饭送到顾客1;

当厨师做好顾客2的饭,告知服务员,然后服务员把饭送到顾客2,依此类推

一系列的事件称为流,异步非阻塞,观察者的设计模式

代码案例:

传统:

int b=2;
int c=3
int a=b+c //a被赋值后,b和c的改变不会影响a
b=5;

响应式编程:

int b=2;
int c=3
int a=b+c 
b=5;//此时a变化为8,a会根据b、c的变化而变化

SpringBoot2.x的响应式编程基于Spring5;

而Spring5的响应式编程又基于Reactor和Netty、Spring WebFlux替代Spring MVC

响应式编程最大的核心是非阻塞,即后台的每一步每一段都要做到非阻塞

比如使用MySQL作为数据库,由于MySQL不提供响应式编程,所以会阻塞

因此响应式编程不应采用MySQL,应该使用非阻塞的NoSQL

Spring WebFlux有两种风格:基于功能和基于注解的。基于注解非常接近Spring MVC模型,如以下示例所示:

            @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){
// ...
} }

第二种: 路由配置与请求的实际处理分开

            @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){
// ...
}
}

Spring WebFlux应用程序不严格依赖于Servlet API,因此它们不能作为war文件部署,也不能使用src/main/webapp目录

可以整合多个模板引擎,除了REST外,您还可以使用Spring WebFlux提供动态HTML内容

Spring WebFlux支持各种模板技术,包括Thymeleaf,FreeMarker

简单的实战:

依赖

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

自动生成的SpringBoot项目还会有一个test依赖,可选

        <dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>

简单的Controller:

package org.dreamtech.webflux.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; @RestController
public class TestController {
@GetMapping("/test")
public Mono<String> test() {
return Mono.just("hello world");
}
}

访问http://localhost:8080/test,显示hello world说明成功

这里使用到了Mono,后边还会用到Flux,他们的实现很复杂,但可以简单地理解:

User、List<User>

1)简单业务而言:和其他普通对象差别不大,复杂请求业务,就可以提升性能
2)通俗理解:
Mono 表示的是包含 0 或者 1 个元素的异步序列
mono->单一对象 User
例如从redis根据用户ID查到唯一的用户,然后进行返回Mono<User>

Flux 表示的是包含 0 到 N 个元素的异步序列
flux->数组列表对象 List<User>
例如从redis根据条件:性别为男性的用户进行查找,然后返回Flux<User>
3)Flux 和 Mono 之间可以进行转换

进一步的使用

对User实体类实现增删改查功能:

package org.dreamtech.webflux.domain;

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:

package org.dreamtech.webflux.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.dreamtech.webflux.domain.User;
import org.springframework.stereotype.Service; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; @Service
public class UserService {
// 使用Map模拟数据库
private static final Map<String, User> dataMap = new HashMap<String, User>();
static {
dataMap.put("1", new User("1", "admin"));
dataMap.put("2", new User("2", "John"));
dataMap.put("3", new User("3", "Rose"));
dataMap.put("4", new User("4", "James"));
dataMap.put("5", new User("5", "Bryant"));
} /**
* 返回数据库的所有用户信息
*
* @return
*/
public Flux<User> list() {
Collection<User> list = UserService.dataMap.values();
return Flux.fromIterable(list);
} /**
* 根据用户ID返回用户信息
*
* @param id 用户ID
* @return
*/
public Mono<User> getById(final String id) {
return Mono.justOrEmpty(UserService.dataMap.get(id));
} /**
* 根据用户ID删除用户
*
* @param id 用户ID
* @return
*/
public Mono<User> delete(final String id) {
return Mono.justOrEmpty(UserService.dataMap.remove(id));
}
}

Controller:

package org.dreamtech.webflux.controller;

import org.dreamtech.webflux.domain.User;
import org.dreamtech.webflux.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; @RestController
public class UserController {
private final UserService userService; public UserController(final UserService userService) {
this.userService = userService;
} /**
* 根据ID查找用户
*
* @param id 用户ID
* @return
*/
@GetMapping("/find")
public Mono<User> findById(final String id) {
return userService.getById(id);
} /**
* 获得用户列表
*
* @return
*/
@GetMapping("/list")
public Flux<User> list() {
return userService.list();
} /**
* 根据ID删除用户
*
* @param id 用户ID
* @return
*/
@GetMapping("/delete")
public Mono<User> delete(final String id) {
return userService.delete(id);
}
}

访问定义的三个API,发现和SpringMVC基本没有区别

所以,对返回进行延迟处理:

    @GetMapping("/list")
public Flux<User> list() {
return userService.list().delayElements(Duration.ofSeconds(3));
}

只是这些设置的话,等待3*list.size秒后全部返回,要突出流的特点,需要进行配置:

    @GetMapping(value = "/list", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<User> list() {
return userService.list().delayElements(Duration.ofSeconds(3));
}

这时候访问,可以发现每过3秒返回一个对象信息

使用WebClient客户端进行测试:

package org.dreamtech.webflux;

import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; public class WebClientTest {
@Test
public void test() {
Mono<String> bodyMono = WebClient.create().get().uri("http://localhost:8080/find?id=3")
.accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(String.class);
System.out.println(bodyMono.block());
}
}

SpringBoot 2.x (14):WebFlux响应式编程的更多相关文章

  1. SpringBoot使用WebFlux响应式编程操作数据库

    这一篇文章介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库. 前言 在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring ...

  2. springboot2 webflux 响应式编程学习路径

    springboot2 已经发布,其中最亮眼的非webflux响应式编程莫属了!响应式的weblfux可以支持高吞吐量,意味着使用相同的资源可以处理更加多的请求,毫无疑问将会成为未来技术的趋势,是必学 ...

  3. [转]springboot2 webflux 响应式编程学习路径

    原文链接 spring官方文档 springboot2 已经发布,其中最亮眼的非webflux响应式编程莫属了!响应式的weblfux可以支持高吞吐量,意味着使用相同的资源可以处理更加多的请求,毫无疑 ...

  4. 07-Spring5 WebFlux响应式编程

    SpringWebFlux介绍 简介 SpringWebFlux是Spring5添加的新模块,用于Web开发,功能和SpringMvc类似的,WebFlux使用当前一种比较流行的响应式编程框架 使用传 ...

  5. springboot(二十三)Springboot2.X响应式编程

    序言 Spring WebFlux是Spring Framework 5.0中引入的新的反应式Web框架与Spring MVC不同,它不需要Servlet API,完全异步和非阻塞,并 通过React ...

  6. SpringBoot实战派读书笔记---响应式编程

    1.什么是WebFlux? WebFlux不需要Servlet API,在完全异步且无阻塞,并通过Reactor项目实现了Reactor Streams规范. WebFlux可以在资源有限的情况下提高 ...

  7. Spring WebFlux 响应式编程学习笔记(一)

    各位Javaer们,大家都在用SpringMVC吧?当我们不亦乐乎的用着SpringMVC框架的时候,Spring5.x又悄(da)无(zhang)声(qi)息(gu)的推出了Spring WebFl ...

  8. 【SpringBoot】SpringBoot2.0响应式编程

    ========================15.高级篇幅之SpringBoot2.0响应式编程 ================================ 1.SprinBoot2.x响应 ...

  9. (转)Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门

    http://www.ityouknow.com/springboot/2019/02/12/spring-boot-webflux.html Spring 5.0 中发布了重量级组件 Webflux ...

随机推荐

  1. [转] 编写高效的 CSS 选择器

    高效的CSS已经不是一个新的话题了,也不是我一个非得重拾的话题,但它却是我在Sky公司工作之时,所感兴趣的,关注已久的话题. 有很多人都忘记了,或在简单的说没有意识到,CSS在我们手中,既能很高效,也 ...

  2. Jenkins安装和配置FindBugs、PMD、CheckStyle等插件

    最近研究Jenkins的常用插件的使用,主要使用FindBugs.PMD.CheckStyle.Violations.Emma等插件,主要参考了http://blog.csdn.net/dc_726/ ...

  3. 分布式环境下的session管理

    一.分布式Session的几种实现方式 1.1.基于cookie 进行session共享 简单.方便,每次通过判断cookie中的用户状态信息判断用户的登录状态:但是用户信息要存在客户端,存在安全隐患 ...

  4. .NET Framework 2.0安装问题

    在.NET Framework 2.0安装的时候,如果提示 system.deployment.dll失败,另一个程序正在使用此文件,进程无法访问.这种情况下,我们可能的解决方案是: 关闭掉杀毒软件在 ...

  5. 在Elasticsearch6.X中如何实现去重

    1.前言 Elasticsearch有没有类似mysql的distinct的去重功能呢? 1)如何去重计数? 类似mysql: select distinct(count(1)) from my_ta ...

  6. MYSQL中coalesce函数的用法

    coalesce():返回参数中的第一个非空表达式(从左向右依次类推): 例如: select coalesce(null,4,5); // 返回4 select coalesce(null,null ...

  7. IDEA如何找到接口的实现类

    如何找到接口的实现类 (IDEA))在ApplicationContext上右击 Diagrams ->show diagram 可以看到继承关系: 在ApplicationContext上右击 ...

  8. yii2之使用ueditor

    代码效果: 1.去github下载yii2高级版ueditor扩展 2.将下载的扩展放入  /common/widgets 中(目录如图所示) 3.在视图中的代码 <?=common\widge ...

  9. ue4 改c++类名

    http://blog.csdn.net/chinahaerbin/article/details/50855135

  10. js中的原型以及原型链

    在js中原型是每个构造函数的属性: 这个算 js 核心概念的一部分 var f1 = new Foo(); 对象 f1 的构造函数就是 Foo , f1的原型 __proto__ 就指向构造函数 Fo ...