一、SprinBoot2.x响应式编程简介
简介:讲解什么是reactive响应式编程和使用的好处
1、基础理解:
依赖于事件,事件驱动(Event-driven)
一系列事件称为“流”
异步
非阻塞
1)买奶茶的案例(一个一个等待处理)
2)响应式编程
观察者模式:服务员一直观察后台的情况,然后通知客户
网上的一个例子:
int b= 2;
int c=3
int a = b+c //命令式编程后续b和c变化,都不影响a
b=5;
int b= 2;
int c= 3
int a = b+c //响应式编程中,a的变化,会和b、c的变化而变化(事件驱动)
b=5;
2、官网:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-webflux
学习资料
1、reactive-streams学习资料:http://www.reactive-streams.org/
2、web-flux相关资料:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#spring-webflux
总结:响应式编程,必须每一个阶段都不阻塞,才能提升性能
二、SpringBoot2.x响应式编程webflux介绍
简介:讲解SpringBoot2.x响应式编程介绍 Mono、Flux对象和优缺点
1、Spring WebFlux是Spring Framework 5.0中引入的新的反应式Web框架
与Spring MVC不同,它不需要Servlet API,完全异步和非阻塞,并 通过Reactor项目实现Reactive Streams规范。
RxJava
2、Flux和Mono User List<User>
1)简单业务而言:和其他普通对象差别不大,复杂请求业务,就可以提升性能
2)通俗理解:
Mono 表示的是包含 0 或者 1 个元素的异步序列 (序列理解成流)
mono->单一对象 User redis->用户ID-》唯一的用户Mono<User>
Flux 表示的是包含 0 到 N 个元素的异步序列
flux->数组列表对象 List<User> redis->男性用户->Flux<User>
Flux 和 Mono 之间可以进行转换
3、Spring WebFlux有两种风格:基于功能和基于注解的。基于注解非常接近Spring MVC模型,如以下示例所示:
第一种:
4、Spring WebFlux应用程序不严格依赖于Servlet API,因此它们不能作为war文件部署,也不能使用src/main/webapp目录
5、可以整合多个模板引擎
除了REST Web服务外,您还可以使用Spring WebFlux提供动态HTML内容。Spring WebFlux支持各种模板技术,包括Thymeleaf,FreeMarker
三、SpringBoot2.x webflux实战
简介:webflux响应式编程实战
1、WebFlux中,请求和响应不再是WebMVC中的ServletRequest和ServletResponse,而是ServerRequest和ServerResponse
2、加入依赖,如果同时存在sprin g-boot-starter-web,则会优先用spring-boot-starter-web
1 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>com.itcast</groupId> 7 <artifactId>Springboot08_A</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <parent> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-parent</artifactId> 12 <version>2.0.1.RELEASE</version> 13 </parent> 14 <dependencies> 15 <!-- <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-web</artifactId> 18 </dependency> --> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-webflux</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 </dependency> 28 29 </dependencies> 30 <build> 31 <plugins> 32 <plugin> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-maven-plugin</artifactId> 35 </plugin> 36 </plugins> 37 </build> 38 39 </project>
官网说明
测试
1)
2)
3、启动方式默认是Netty,8080端口
Netty:高性能网络框架,异步非阻塞
4、参考:https://spring.io/blog/2016/04/19/understanding-reactive-types
5.模拟数据库实现CRUD
1)service
2)controller
3)访问
4)list
5)del
6)调用list的时候,希望拿到一个值就返回,如何体现这种效果
应该:服务端的推送(像流水一样)
四、WebFlux客户端WebClient讲解
简介:讲解SpringBoot2.x WebFlux客户端WebClient的介绍和使用
1、反应式客户端
官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-webclient
实战
1)在测试
2)先启动主程序,再在方法上点JUnit Test