响应式编程生活案例:

传统形式:

一群人去餐厅吃饭,顾客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. ACM学习历程—Hihocoder 1178 计数(位运算 && set容器)(hihoCoder挑战赛12)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB   描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物,鱼丸. 魔法n能召 ...

  2. poj1195 Mobile phones

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 19786   Accepted: 9133 De ...

  3. 【LeetCode】312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  4. MySql 扩展存储引擎

    MySql 扩展存储引擎 下面介绍几个列式存储引擎(都有两个版本:社区版.商业版): 一:TokuDB TokuDB 是一个高性能.支持事务处理的 MySQL 和 MariaDB 的存储引擎.Toku ...

  5. tyvj1659救援队——最小生成树

    题目:http://www.joyoi.cn/problem/tyvj-1659 想清楚了是非常简单的最小生成树: 1.树中每条边都会被走两边: 2.每个点会走度数遍,起点又多走一遍: 根据以上两条处 ...

  6. Floyd(稠密图,记录路径)

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #i ...

  7. URL shortening service

    Use Cases 1, shortening : take a URL => return a much shorter URL 2, redirection : take a short U ...

  8. win7 x64 eclipse_kepler下编译 hadoop1.1.2-celipse-plugin

    1.编译前准备 a)       下载和解压hadoop-1.1.2.tar.gz b)       下载并配置ant 2.配置 a)       进入hadoop-1.1.2\src\contrib ...

  9. 洛谷P2257 YY的GCD(莫比乌斯反演)

    传送门 原来……莫比乌斯反演是这么用的啊……(虽然仍然不是很明白) 首先,题目所求如下$$\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=prim]$$ 我们设$f(d)$表示$g ...

  10. [Xcode 实际操作]六、媒体与动画-(7)遍历系统提供的所有滤镜

    目录:[Swift]Xcode实际操作 本文将演示系统到底提供了多少滤镜供开发者使用,并了解每个滤镜都有哪些参数需要配置. 在项目导航区,打开视图控制器的代码文件[ViewController.swi ...