========================15、高级篇幅之SpringBoot2.0响应式编程 ================================

1、SprinBoot2.x响应式编程简介
简介:讲解什么是reactive响应式编程和使用的好处

1、基础理解:
依赖于事件,事件驱动(Event-driven)
一系列事件称为“流”
异步
非阻塞

观察者模式

网上的一个例子:
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
SpingBoot2底层是用spring5,开始支持响应式编程,Spring又是基于Reactor试下响应式。

学习资料
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

2、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模型,如以下示例所示:
第一种:
@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 {

公共 Mono <ServerResponse> getUser(ServerRequest请求){
// ...
}

public Mono <ServerResponse> getUserCustomers(ServerRequest request){
// ...
}

公共 Mono <ServerResponse> deleteUser(ServerRequest请求){
// ...
}
}

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

5、可以整合多个模板引擎
除了REST Web服务外,您还可以使用Spring WebFlux提供动态HTML内容。Spring WebFlux支持各种模板技术,包括Thymeleaf,FreeMarker

3、SpringBoot2.x webflux实战
简介:webflux响应式编程实战

1、WebFlux中,请求和响应不再是WebMVC中的(Http)ServletRequest和(Http)ServletResponse,而是ServerRequest和ServerResponse

2、加入依赖,如果同时存在spring-boot-starter-web,则会优先用spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

测试
localhost:8080/api/v1/user/test

3、启动方式默认是Netty,8080端口

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; @RestController
@RequestMapping("/api/v1/user")
public class UserController {
@GetMapping("/test")
public Mono<String> test(){
return Mono.just("hello xiaod");
}
}

controller

4、参考:https://spring.io/blog/2016/04/19/understanding-reactive-types

package com.example.demo.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.springframework.stereotype.Service; import com.example.demo.domain.User; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; @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老师"));
}
public Flux<User> list(){
Collection<User> list=UserService.dataMap.values();
return Flux.fromIterable(list);
}
public Mono<User> getById(final String id){
return Mono.justOrEmpty(UserService.dataMap.get(id));
}
public Mono<User> del(final String id){
return Mono.justOrEmpty(UserService.dataMap.remove(id));
} }

service

package com.example.demo.controller;

import java.time.Duration;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.example.demo.domain.User;
import com.example.demo.service.UserService; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; @RestController
@RequestMapping("/api/v1/user")
public class UserController {
private UserService userService; public UserController(final UserService userService) {
this.userService = userService;
} @GetMapping("/test")
public Mono<String> test(){
return Mono.just("hello xiaod");
}
@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);
}
@GetMapping(value="list",produces=MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<User> list(){
return userService.list().delayElements(Duration.ofSeconds(2));
}
}

controller

可以用于服务推送

4、WebFlux客户端WebClient讲解
简介:讲解SpringBoot2.x WebFlux客户端WebClient的介绍和使用
1、反应式客户端

官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-webclient

【SpringBoot】SpringBoot2.0响应式编程的更多相关文章

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

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

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

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

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

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

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

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

  5. SpringBoot2.0WebFlux响应式编程知识总结

  6. SpringBoot 2.x (14):WebFlux响应式编程

    响应式编程生活案例: 传统形式: 一群人去餐厅吃饭,顾客1找服务员点餐,服务员把订单交给后台厨师,然后服务员等待, 当后台厨师做好饭,交给服务员,经过服务员再交给顾客1,依此类推,该服务员再招待顾客2 ...

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

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

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

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

  9. springboot 使用webflux响应式开发教程(一)

    什么是webFlux 左侧是传统的基于Servlet的Spring Web MVC框架,右侧是5.0版本新引入的基于Reactive Streams的Spring WebFlux框架,从上到下依次是R ...

随机推荐

  1. Navicat Premium 12激活

    大自然的搬运工:https://www.jianshu.com/p/5f693b4c9468 声明:本文所提供的所有软件均来自于互联网,个人存放在此作为备用,以备将来不时之需,同时作为大家的分享和学习 ...

  2. vue--一些预设属性

    Babel=> 转换工具,将ES6转换成ES5的转换工具.有些浏览器不支持ES6语法: router=> 路由管理器: vueX=> 一个仓库,存储状态信息: CSS pre-pro ...

  3. html css js 细节

    细节1 1.Chrome中文界面下会将小于12px的字体默认显示为12px,解决方法:在CSS中加入-webkit-text-size-adjust:none; 2.link可以加载除CSS以外的其他 ...

  4. nodeJs的Buffer操作

    再nodejs里,很多类是引入模块才能使用,Buffer是一个全局类,他不需要require引入 Buffer有三种构造函数 //1.在构造函数传一个数字,规定buffer的长度.默认全是16进制的0 ...

  5. postman(八):使用newman来执行postman脚本

    通过之前的了解,我们知道postman是基于javascript语言编写的,而导出的json格式的postman脚本也无法直接在服务器运行,它需要在newman中执行(可以把newman看做postm ...

  6. 8个让DevOps转型取得成功的关键步骤

    ​​关注嘉为科技,获取运维新知 在数字化时代,企业需要更快更灵活的交付来支持业务运营,这种迫切的需求促成了DevOps的高速发展,成为了企业获得竞争优势的关键.尽管大家都知道DevOps给业务带来的好 ...

  7. PV、PVC和Storeclass等官方内容翻译

    k8s1.13版本 PV apiVersion: v1 kind: PersistentVolume metadata: name: filesystem-pvc spec: capacity: #未 ...

  8. 若依项目分模块集成uflo2

    关于若依分模块创建项目可参考:https://www.cnblogs.com/conswin/p/9766186.html 了解uflo2,uflo2是一套由BSTEK自主研发的基于Java的工作流引 ...

  9. MongoDB AUTH结果验证及开启方法

          创建超级管理员(root)和普通用户(gxpt) #创建超级管理员(root) RS1:PRIMARY> use admin RS1:PRIMARY> db.createUse ...

  10. 单机器搭建 zk 集群

    在一台机器上配置 2 节点的 zk 集群,zk1 和 zk2 的 serverid 分别为 1 和 2,本机 ip 是 192.168.40.1 zk1 相关配置: dataDir=E:/test/z ...